首页 > 解决方案 > 为什么 node 和 npm 版本不同?

问题描述

看,我安装了最新的节点版本

node -v   // I get v14.15.0

然后我跑npm init了一个小项目。我还创建了一个 index.js 文件,代码如下

console.lo("Hello") 

显然它有一个错字,应该是 console.log("Hello"),但我这样做是为了获取项目使用的 node 和 npm 版本。

npm ERR! Linux 5.4.0-81-generic
npm ERR! argv "/usr/bin/node" "/usr/bin/npm" "install" "-g" "npm@next"
npm ERR! node v8.10.0
npm ERR! npm  v3.5.2
npm ERR! path /usr/local/lib/node_modules/.staging/@gar/promisify-ebaab35a
npm ERR! code ENOENT
npm ERR! errno -2
npm ERR! syscall rename

它使用的是旧版本,但我想要最新的。我没有被迫使用旧版本所以我该怎么做,npm init但是使用最新版本的 node 和 npm。

此外,当我尝试使用 更新 npm 时sudo npm install -g npm@next,我收到此错误

npm ERR! enoent ENOENT: no such file or directory, rename '/usr/local/lib/node_modules/.staging/@gar/promisify-ebaab35a' -> '/usr/local/lib/node_modules/npm/node_modules/@gar/promisify'
npm ERR! enoent ENOENT: no such file or directory, rename '/usr/local/lib/node_modules/.staging/@gar/promisify-ebaab35a' -> '/usr/local/lib/node_modules/npm/node_modules/@gar/promisify'
npm ERR! enoent This is most likely not a problem with npm itself
npm ERR! enoent and is related to npm not being able to find a file.
npm ERR! enoent 

npm ERR! Please include the following file with any support request:
npm ERR!     /home/braulio/npm-debug.log
npm ERR! code 1

我认为 npm 正在强制节点使用 v8.10,所以也许在我不更新 npm 之前,节点不会。我怎样才能解决这个问题?

标签: node.jsnpm

解决方案


您可以下载 node.js linux tarball 并解压缩它们,而不是将 node 安装到 /usr/bin/node 并将 npm 安装到 /usr/bin/npm,然后 node/npm 二进制文件将在 bin 目录中可用。然后将当前终端的 $PATH 修改为以下格式:

export $PATH=/opt/node-vxx.xx.x/bin/:$PATH
which node
node -v
which npm 
npm -v

推荐阅读