Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute 'npm run' command programmatically?

I have some custom testing script, which I can run with npm run test command, which executes some Node script for starting e2e/unit tests. But before it I must start webpack dev server with npm run dev (it's a some custom Node script too, details doesn't matter) in other terminal window. So, I want to omit npm run dev manually executing and move it to custom npm run test script, i.e. I want to execute webpack dev server programmatically in Node script. How can I execute npm run dev programmatically using Node script and stop it then? Thanks in advance!

"dev": "node node_modules/webpack-dev-server/bin/webpack-dev-server.js --host 0.0.0.0 --history-api-fallback --debug --inline --progress --config config/config.js" 
like image 940
malcoauri Avatar asked Jun 25 '16 19:06

malcoauri


People also ask

What is npm Run command?

npm run sets the NODE environment variable to the node executable with which npm is executed. If you try to run a script without having a node_modules directory and it fails, you will be given a warning to run npm install , just in case you've forgotten.

What script does npm start run?

So npm start runs the node script that is listed under start in the package. json.


2 Answers

You can use exec to run from script

import {series} from 'async'; const {exec} = require('child_process');  series([  () => exec('npm run dev'),  () => exec('npm run test') ]);  
like image 111
Foysal Osmany Avatar answered Oct 05 '22 23:10

Foysal Osmany


Just install npm:

npm install npm 

Then in your program:

npm.commands.run('dev', (err) => { ... }); 

See the source for more info. The npm.command object is an unofficial API for npm. Note that using exec or spawn to execute npm is safer, since the API is unofficial.

like image 31
mzedeler Avatar answered Oct 05 '22 23:10

mzedeler