简体   繁体   中英

How do I run an npm command with a different version of node using 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM