首页 > 解决方案 > 使用 npm 安装节点失败并出现 EACCES 错误

问题描述

我正在尝试使用以下命令在 MacOS 上安装节点:

sudo npm install -g node

这会产生以下输出:

> node@12.6.0 preinstall /usr/local/lib/node_modules/node-darwin-x64/lib/node_modules/node-darwin-x64/lib/node_modules/node
> node installArchSpecificPackage

npm WARN registry Unexpected warning for https://registry.npmjs.org/: Miscellaneous Warning EACCES: EACCES: permission denied, open '/Users/bernhard/.npm/_cacache/tmp/5e859640'
npm WARN registry Using stale data from https://registry.npmjs.org/ due to a request error during revalidation.
npm ERR! path /Users/bernhard/.npm/_cacache/tmp/ef2c2113
npm ERR! code EACCES
npm ERR! errno -13
npm ERR! syscall open
npm ERR! Error: EACCES: permission denied, open '/Users/bernhard/.npm/_cacache/tmp/ef2c2113'
npm ERR!  [OperationalError: EACCES: permission denied, open '/Users/bernhard/.npm/_cacache/tmp/ef2c2113'] {
npm ERR!   cause: [Error: EACCES: permission denied, open '/Users/bernhard/.npm/_cacache/tmp/ef2c2113'] {
npm ERR!     errno: -13,
npm ERR!     code: 'EACCES',
npm ERR!     syscall: 'open',
npm ERR!     path: '/Users/bernhard/.npm/_cacache/tmp/ef2c2113'
npm ERR!   },
npm ERR!   isOperational: true,
npm ERR!   stack: "Error: EACCES: permission denied, open '/Users/bernhard/.npm/_cacache/tmp/ef2c2113'",
npm ERR!   errno: -13,
npm ERR!   code: 'EACCES',
npm ERR!   syscall: 'open',
npm ERR!   path: '/Users/bernhard/.npm/_cacache/tmp/ef2c2113'
npm ERR! }
npm ERR! 
npm ERR! The operation was rejected by your operating system.
npm ERR! It is likely you do not have the permissions to access this file as the current user
npm ERR! 
npm ERR! If you believe this might be a permissions issue, please double-check the
npm ERR! permissions of the file and its containing directories, or try running
npm ERR! the command again as root/Administrator (though this is not recommended).
/usr/local/lib/node_modules/node-darwin-x64/lib/node_modules/node-darwin-x64/lib/node_modules/node/node_modules/node-bin-setup/index.js:27
        throw e;
        ^

Error: EACCES: permission denied, mkdir '/usr/local/lib/node_modules/node-darwin-x64/lib/node_modules/node-darwin-x64/lib/node_modules/node/bin'
    at Object.mkdirSync (fs.js:764:3)
    at ChildProcess.<anonymous> (/usr/local/lib/node_modules/node-darwin-x64/lib/node_modules/node-darwin-x64/lib/node_modules/node/node_modules/node-bin-setup/index.js:24:10)
    at ChildProcess.emit (events.js:203:13)
    at maybeClose (internal/child_process.js:1021:16)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:283:5) {
  errno: -13,
  syscall: 'mkdir',
  code: 'EACCES',
  path: '/usr/local/lib/node_modules/node-darwin-x64/lib/node_modules/node-darwin-x64/lib/node_modules/node/bin'
}
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! node@12.6.0 preinstall: `node installArchSpecificPackage`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the node@12.6.0 preinstall script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/bernhard/.npm/_logs/2019-07-21T16_20_19_759Z-debug.log

提前跑sudo npm cache clean并没有什么不同。我检查了_cacache文件夹的权限,我的用户对该文件夹具有完全权限。

关于可能导致此问题的任何想法?

标签: node.jsnpm

解决方案


正如其他用户所提到的,npm 本质上是 Node.js 的一部分(并将npm install -g node安装一个名为“node”的全局模块,它与 Node.jsnode可执行文件不同)。

在我不得不更新节点之后,我的麻烦开始了,当(错误的)npm 命令失败时,我 npm-uninstalled 节点,以便我可以重新安装它。实际上,此时 Node.js 已被卸载(至少部分卸载 - 要么,要么坏了)。

通过运行从 Node.js 网站下载的安装程序重新安装 Node.js 解决了该问题。将来,我将使用推荐的方法之一来更新 Node.js,就像这篇文章中描述的那样。


推荐阅读