7

My unbuild node.js project is on a file server and I want to execute the npm script "build" which should build the application. But I can't find a way to call this command through code. Does anyone know if this should be possible? When I try to search for a result I keep finding tutorials on how npm script work, which is not what I want to know. Is it possble to run an npm script (command?) from code?

1
  • You may want to look at the spawn command under the child_process module in nodejs docs. Commented Jan 7, 2020 at 13:49

2 Answers 2

8

You can use child_process.exec to execute a script:

var exec = require('child_process').exec;

var cmd = exec("npm build", function(err, stdout, stderr) {
  if (err) {
    // handle error
  }
  console.log(stdout);
});

dir.on('exit', function (code) {
    // return value from "npm build"
});
Sign up to request clarification or add additional context in comments.

1 Comment

Unfortunately I can't seem to use the child_process package, in my code completion it shows all its funtions but when I run it I get an error that the module cannot be found. Yet I think this should work if child_process could be found.
1

you can use concurrently npm package, it allow managing your npm scripts and provides API to call npm-scripts from inside your Node app project, check programmatic-usage section.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.