The command npm run
will run the command which I set in the package.json, and I think it does not new a child process
to run the command. What does npm do to run the command without new a child process
?
Npm run has nothing to do with the node child process, if that is what you are asking. Npm run is a command provided by the npm CLI which allows you to instantiate a shell and execute the command provided in the package.json file of your project.
Consider this is your package.json :
{
"name": "my-awesome-package",
"version": "1.0.0",
"scripts" : { "test" : "mocha ./test/unit/mytest.js" }
}
Now if you execute npm run test
, npm will simply go and check in package.json scripts
section for the 'test' key and execute that command in shell or cmd.exe based on your operating system.
If you have not installed mocha globally the command will show error in the console itself, OR if the file mytest.js
does not exist the CLI will throw an error which is similar to just typing mocha ./tests/unit/mytest.json
This paragraph from the npm docs is pretty self-explanatory.
The actual shell your script is run within is platform dependent. By default, on Unix-like systems it is the /bin/sh command, on Windows it is the cmd.exe. The actual shell referred to by /bin/sh also depends on the system. As of npm@5.1.0, you can customize the shell with the script-shell configuration.
Update : As per the response in comment, if you want to execute CLI commands via node without using child_process
api you can try exec
or execsync(cmd)
as a simple workaround. This will simply execute your shell cmd and return to your code if no errors were found.