NPM /错误:EACCES:权限被拒绝,scandir

euh*_*hjs 16 javascript node.js npm angularjs

当我输入“npm run build:prod”时

\n
build:prod\n    npm run build -- --configuration production --aot --optimization=false\n
Run Code Online (Sandbox Code Playgroud)\n

我收到此错误:

\n
> fancy-name@0.0.0 build:prod\n> npm run build -- --configuration production --aot --optimization=false\n\nglob error [Error: EACCES: permission denied, scandir '/root/.npm/_logs'] {\n errno: -13,\n code: 'EACCES',\n syscall: 'scandir',\n path: '/root/.npm/_logs'\n}\nnpm WARN logfile Error: EACCES: permission denied, scandir '/root/.npm/_logs'\nnpm WARN logfile  error cleaning log files [Error: EACCES: permission denied, scandir '/root/.npm/_logs'] {\nnpm WARN logfile   errno: -13,\nnpm WARN logfile   code: 'EACCES',\nnpm WARN logfile   syscall: 'scandir',\nnpm WARN logfile   path: '/root/.npm/_logs'\nnpm WARN logfile }\n\xe2\xb8\xa8\xe2\xa0\x82\xe2\xa0\x82\xe2\xa0\x82\xe2\xa0\x82\xe2\xa0\x82\xe2\xa0\x82\xe2\xa0\x82\xe2\xa0\x82\xe2\xa0\x82\xe2\xa0\x82\xe2\xa0\x82\xe2\xa0\x82\xe2\xa0\x82\xe2\xa0\x82\xe2\xa0\x82\xe2\xa0\x82\xe2\xa0\x82\xe2\xa0\x82\xe2\xb8\xa9 \xe2\xa0\x99 : WARN logfile Error: EACCES: permission denied, scandir '/root/.npm/_logs'\n> fancy-name@0.0.0 build\n> ng build "--configuration" "production" "--aot" "--optimization=false"\n\nNode.js version v17.4.0 detected.file Error: EACCES: permission denied, scandir '/root/.npm/_logs'\nOdd numbered Node.js versions will not enter LTS status and should not be used for production. For more information, please see https://nodejs.org/en/about/releases/.\nThis version of CLI is only compatible with Angular versions ^13.0.0-next || >=13.0.0 <14.0.0,ogs'\nbut Angular version 12.1.3 was found instead.\n\nPlease visit the link below to find instructions on how to update Angular.\nhttps://update.angular.io/\n
Run Code Online (Sandbox Code Playgroud)\n

我已经尝试删除 node_module & package.json 并NPM install再次运行,但它不起作用\n(Linux 4.19.0-16-cloud-amd64)

\n

编辑:解决了chown -R root /path/of/your/project

\n

oli*_*ren 27

更新:这个问题和下面的答案仅适用于 NPM 7 和 8(更多信息)。

此快速修复应该通过安装 NPM 版本 >= 9 来解决该问题。

npm install -g npm@latest
Run Code Online (Sandbox Code Playgroud)

原始答案(适用于 Node 15-19 附带的 NPM 7 和 8)

这是一个令人困惑的错误,这是可以理解的。您npm以 root 身份运行,但它说您无权访问这些文件?这是有充分理由的:)这些问题本质上是关于同一件事的:

我的回答很长,详细介绍了正在发生的事情,但本质是这样的:

(NPM) ...使用这段代码来确定以谁的身份运行:

const { uid, gid } = isRoot ? inferOwner.sync(cwd) : {}

正如 infer-owner 模块的文档所述: 根据最近的现有父级的所有者推断路径的所有者

因此,如果您以 root 身份运行 NPM,则还需要将当前目录的所有者更改为 root:

chown -R root /path/of/your/project
Run Code Online (Sandbox Code Playgroud)

但所有这些麻烦应该为您指明真正的解决方案:不要以 root 身份运行 NPM。您不知道哪种脚本可以作为postinstall触发器或类似脚本运行。注意安全; 仅在必要时使用 root 帐户使用sudo.

附录:尝试 NPM 版本 6 或 >= 9

正如链接答案中提到的:

此行为仅适用于 NPM 版本 7 和 8。NPM 版本 < 7(即 Node 12-14 附带的版本)在以 root 身份运行时工作得非常好。

所以一些快速修复是

  • npm install -g npm@latest # Use the latest version of NPM
  • npm install -g nvm && nvm use 14 # Note: End of Life on May 2023
  • npm install -g nvm && nvm use 18 # Uses NPM 9, too new for some deps?

  • 该死,我执行 chown 命令错误,错误不再存在,谢谢! (3认同)