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?
2 Answers
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"
});
1 Comment
user2912900
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.
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.
spawncommand under thechild_processmodule in nodejs docs.