Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I run an npm command with a different version of node using nvm?

Tags:

node.js

nvm

Tried these:

✗ nvm run 9 npm start 
Running node v9.10.1 (npm v5.8.0)
module.js:545
    throw err;
    ^

Error: Cannot find module '/Users/foo/work/grid-styled/npm'
    at Function.Module._resolveFilename (module.js:543:15)



✗ nvm run 9 npm -- x0 dev docs/App.js
Running node v9.10.1 (npm v5.8.0)
module.js:545
    throw err;
    ^

Error: Cannot find module '/Users/foo/work/grid-styled/npm'

I can get it to run by calling the bin directly:

nvm run 9 node_modules/.bin/x0 dev docs/App.js

But it would be better to just call npm. Seems possible but the nvm docs don't address this use case.

like image 275
jcollum Avatar asked Apr 30 '18 17:04

jcollum


People also ask

How do I change npm to specific version?

If you want to downgrade npm to a specific version, you can use the following command: npm install -g npm@[version. number] where the number can be like 4.9. 1 or 8 or v6.

How do I change Node version in npm?

If you want to switch to the different version of Node, just type n in the terminal and you should see the Node versions listed. Use arrow keys to choose the version and press enter.

Does NVM manage npm version?

nvm manages node. js and npm versions. It's designed to be installed per-user and invoked per-shell. nvm works on any POSIX-compliant shell (sh, dash, ksh, zsh, bash), in particular on these platforms: unix, macOS, and windows WSL.


1 Answers

If you use nvm run you are executing node on a specific version, so: nvm run 9 npm start is equivalent to node npm start (with version 9 of node). That's the reason for the error.

You should use nvm exec instead, that is for executing a command on specific version, for instance:

$ nvm exec 10 npm -v
Running node v10.0.0 (npm v6.0.0)
6.0.0
$ nvm exec 6 npm -v
Running node v6.10.3 (npm v3.10.10)
3.10.10
like image 52
4ndt3s Avatar answered Oct 05 '22 07:10

4ndt3s