7

What happens when I type npm run <command>? Is the <command> passed to the sh shell, like:

sh -c <command>

with additional local variables added to the shell, ie a path to the node_modules/.bin?

I thought npm works only in JavaScript universe by using node, but it seems there's a lot more going on out there. How & and && are handled? Is globstar safe to use?

marzelin
  • 10,790
  • 2
  • 30
  • 49

1 Answers1

5

npm run-script <command> uses sh -c to run the command, and cmd /d /s /c on windows. (source) It is run by child_process.spawn.

npm env lists the environment that npm uses when it runs commands. The documentation lists some of the things it adds to the environment. This includes everything from package.json, the PATH additions you mentioned and more.

&and && are handled by the shell, but spawn is waiting for the process to end, even when it is put in the background by &. && is working as you are used to in sh.

globstar will work as you expect, except on Windows where you have wildcards instead.

bolav
  • 6,938
  • 2
  • 18
  • 42
  • Sidenote regarding ´node_modules/.bin´: Under unix, the folder contains symlink to the modules .js-scripts. Under Windows, they contain .cmd files which cal the .js-scripts with node. – chris Dec 01 '16 at 15:02
  • This it outdated. At least for `npm 8.0.0.` doesn't have "env" command. And link is broken. – pmiranda Apr 22 '22 at 13:24