NVM:npm install -g 一直使用错误的节点版本

Posted

技术标签:

【中文标题】NVM:npm install -g 一直使用错误的节点版本【英文标题】:NVM: npm install -g keeps using wrong version of node 【发布时间】:2019-11-23 02:51:30 【问题描述】:

我正在尝试安装一个名为 expo-cli 的软件包,但由于 npm 一直使用错误的节点版本而失败。

我将节点版本设置为 v12.4.0,我确认已设置。但是当我运行npm install -g expo-cli 时,它一直在使用v9.10.0

➜  ~ nvm current
v12.4.0
➜  ~ node -v
v12.4.0
➜  ~ npm install -g expo-cli
npm WARN deprecated joi@14.0.4: This version has been deprecated in accordance with the hapi support policy (hapi.im/support). Please upgrade to the latest version to get the best features, bug fixes, and security patches. If you are unable to upgrade at this time, paid support is available for older versions (hapi.im/commercial).
npm WARN deprecated hoek@6.1.3: This module has moved and is now available at @hapi/hoek. Please update your dependencies as this version is no longer maintained an may contain bugs and security issues.
npm WARN deprecated topo@3.0.3: This module has moved and is now available at @hapi/topo. Please update your dependencies as this version is no longer maintained an may contain bugs and security issues.

> @expo/traveling-fastlane-darwin@1.9.9 preinstall /Users/edmundmai/.nvm/versions/node/v9.10.0/lib/node_modules/expo-cli/node_modules/@expo/traveling-fastlane-darwin
> node platform.js

/Users/edmundmai/.nvm/versions/node/v9.10.0/bin/expo-cli -> /Users/edmundmai/.nvm/versions/node/v9.10.0/lib/node_modules/expo-cli/bin/expo.js
/Users/edmundmai/.nvm/versions/node/v9.10.0/bin/expo -> /Users/edmundmai/.nvm/versions/node/v9.10.0/lib/node_modules/expo-cli/bin/expo.js

> fsevents@1.2.9 install /Users/edmundmai/.nvm/versions/node/v9.10.0/lib/node_modules/expo-cli/node_modules/fsevents
> node install

如何更改 npm 以使用 node v12.4.0?

没有正确版本的节点,Expo-cli 失败。

➜  ~ expo init my-app
zsh: command not found: expo
➜  ~ nvm list
         v8.0.0
        v9.10.0
        v10.8.0
->      v12.4.0
default -> v12.4.0
node -> stable (-> v12.4.0) (default)
stable -> 12.4 (-> v12.4.0) (default)
iojs -> N/A (default)
lts/* -> lts/dubnium (-> N/A)
lts/argon -> v4.9.1 (-> N/A)
lts/boron -> v6.17.1 (-> N/A)
lts/carbon -> v8.16.0 (-> N/A)
lts/dubnium -> v10.16.0 (-> N/A)
➜  ~ nvm use v9.10.0
Now using node v9.10.0 (npm v6.9.0)
➜  ~ expo init my-app
/Users/edmundmai/.nvm/versions/node/v9.10.0/lib/node_modules/expo-cli/bin/expo.js:12
  throw new Error('expo-cli supports Node versions 8.x.x, 10.x.x and newer.');
  ^

Error: expo-cli supports Node versions 8.x.x, 10.x.x and newer.
    at Object.<anonymous> (/Users/edmundmai/.nvm/versions/node/v9.10.0/lib/node_modules/expo-cli/bin/expo.js:12:9)
    at Module._compile (module.js:649:30)
    at Object.Module._extensions..js (module.js:660:10)
    at Module.load (module.js:561:32)
    at tryModuleLoad (module.js:501:12)
    at Function.Module._load (module.js:493:3)
    at Function.Module.runMain (module.js:690:10)
    at startup (bootstrap_node.js:194:16)
    at bootstrap_node.js:666:3

更新:

This is what happens when I try to scope the node version:

➜  ~ nvm run 12.4.0 npm install --latest-npm
Running node v12.4.0 (npm v6.9.0)
internal/modules/cjs/loader.js:626
    throw err;
    ^

Error: Cannot find module '/Users/edmundmai/npm'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:623:15)
    at Function.Module._load (internal/modules/cjs/loader.js:527:27)
    at Function.Module.runMain (internal/modules/cjs/loader.js:837:10)
    at internal/main/run_main_module.js:17:11 
  code: 'MODULE_NOT_FOUND',
  requireStack: []

➜  ~ nvm exec 12.4.0 npm install -g expo-cli
Running node v12.4.0 (npm v6.9.0)
npm WARN deprecated joi@14.0.4: This version has been deprecated in accordance with the hapi support policy (hapi.im/support). Please upgrade to the latest version to get the best features, bug fixes, and security patches. If you are unable to upgrade at this time, paid support is available for older versions (hapi.im/commercial).
npm WARN deprecated hoek@6.1.3: This module has moved and is now available at @hapi/hoek. Please update your dependencies as this version is no longer maintained an may contain bugs and security issues.
npm WARN deprecated topo@3.0.3: This module has moved and is now available at @hapi/topo. Please update your dependencies as this version is no longer maintained an may contain bugs and security issues.

> @expo/traveling-fastlane-darwin@1.9.9 preinstall /Users/edmundmai/.nvm/versions/node/v9.10.0/lib/node_modules/expo-cli/node_modules/@expo/traveling-fastlane-darwin
> node platform.js

/Users/edmundmai/.nvm/versions/node/v9.10.0/bin/expo -> /Users/edmundmai/.nvm/versions/node/v9.10.0/lib/node_modules/expo-cli/bin/expo.js
/Users/edmundmai/.nvm/versions/node/v9.10.0/bin/expo-cli -> /Users/edmundmai/.nvm/versions/node/v9.10.0/lib/node_modules/expo-cli/bin/expo.js

> fsevents@1.2.9 install /Users/edmundmai/.nvm/versions/node/v9.10.0/lib/node_modules/expo-cli/node_modules/fsevents
> node install

【问题讨论】:

【参考方案1】:

你可以试试这个:$ nvm run 12.4.0 npm install -g expo-cli

或者这个:$ nvm exec 12.4.0 npm install -g expo-cli

供参考:https://github.com/nvm-sh/nvm#usage

【讨论】:

嗨 Diego,我试过了,但没有用,我用结果更新了问题 你可以试试这个:***.com/a/32835618/11734350 问题是你必须重新安装 nvm 但似乎是解决问题的最短路径【参考方案2】:

检查 ~/.npmrc 以获取指向 v9.10.0 模块安装路径的前缀设置。这会导致 npm 在使用其他版本的 node 时将全局包安装在错误的位置。

见:https://docs.npmjs.com/using-npm/config#prefix

【讨论】:

您好,欢迎来到 Stack Overflow。请提供有关为什么这是答案的上下文以及您在哪里找到的。 How to write a good answer @ArielGro 谢谢,我已根据您的建议编辑了我的答案。

以上是关于NVM:npm install -g 一直使用错误的节点版本的主要内容,如果未能解决你的问题,请参考以下文章

npm install 问题解决(nvm安装与使用)

linux Install nvm, node.js, and npm

使用nvm管理多个不同版本的nodeJS之安装成功nodeJs之后使用npm报错的问题

设置 nvm 以正确使用 npm 和 grunt

nvm npm node

Linux安装nodejs和npm