admin管理员组

文章数量:1357220

When running npm run dev I'd like to have npm use a specific version of Node. In other words, I'd like to configure this script to use a specific Node path.

To make things even more complicated the npm run dev actually invokes Angular's ng CLI. So it's important this works too with the respective Node executable.

I tried setting NODE_PATH env var before ng serve, as well as looking for some conduit to put in .npmrc, but couldn't figure out a way to achieve what I am looking for.

When running npm run dev I'd like to have npm use a specific version of Node. In other words, I'd like to configure this script to use a specific Node path.

To make things even more complicated the npm run dev actually invokes Angular's ng CLI. So it's important this works too with the respective Node executable.

I tried setting NODE_PATH env var before ng serve, as well as looking for some conduit to put in .npmrc, but couldn't figure out a way to achieve what I am looking for.

Share Improve this question edited Mar 30 at 7:40 jonrsharpe 122k30 gold badges268 silver badges475 bronze badges asked Mar 30 at 6:45 LIvanovLIvanov 1,3282 gold badges14 silver badges32 bronze badges 3
  • Consider explaining your case in details. Notice that even if you use a different node executable to run a script, it's not guaranteed to be used if a script executes node processes internally – Estus Flask Commented Mar 30 at 8:36
  • There's many details around what I want to achieve, but essentially it can be summed up as a self-contained project - i.e. a developer doesn't need to have pre-installed node, nvm, ng, what not ... Just downloads a project and does make start which in term will download npm and put it in .node subfolder of the project. Then, make uses the npm executable that was downloaded, to run all npm scripts. What I achieve, is a user should not worry about if they have npm installed locally and what version it is. I know nvm somehow "solves" this, but I don't like it too much. – LIvanov Commented Mar 30 at 8:41
  • It's faulty by design. You can patch node_modules/.bin/ to use specific node executable for package bins like "ng", or call them with their full paths like my-node ./node_modules/@angular/cli/bin/ng.js. This doesn't guarantee that the same "node" will be used through the process, but there's a chance it will work. nvm can affect "node" system-wide, that's its purpose, which this may be undesirable for a user. This is basically a case for containers – Estus Flask Commented Mar 30 at 8:49
Add a comment  | 

1 Answer 1

Reset to default 1

npm always uses the system-installed Node.js version.

If you want to install and use multiple versions, you need to use a version manager like nvm.

NODE_PATH isn't relevant in this situation, as it is used to maintain node_modules search paths.

本文标签: Choose Nodejs path when executing npm scriptsStack Overflow