Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is it possible to run npm script from Javascript

Tags:

node.js

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?

like image 749
user2912900 Avatar asked Jan 07 '20 13:01

user2912900


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"
});
like image 62
Lord Elrond Avatar answered Nov 03 '22 18:11

Lord Elrond


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.

like image 36
ROOT Avatar answered Nov 03 '22 18:11

ROOT