npm install 突然失败并出现各种错误,包括 Ubuntu 上的权限被拒绝(使用和不使用 sudo)

npm install suddenly fails with various errors, including permission denied (with & without sudo) on Ubuntu

今天,当我尝试使用 npm i 安装所有软件包时,我的权限被拒绝,无论是否提供 sudo 命令。使用 sudo 还会产生其他错误。我想我昨晚在调整和试验应用程序时搞砸了一些权限;它以前工作得很好。我尝试根据以前类似问题发布的解决方案更改权限,但这些都不起作用。

安装指定的软件包有效;只有当我尝试安装所有软件包时,命令才会中断。我使用 --allow-root 和 --unsafe-perm=true 作为让命令工作的 hack,但我希望有一个更安全和系统的解决方案。以下是错误:

没有 sudo:

npm WARN checkPermissions Missing write access to /home/user/project/node_modules
npm WARN tsutils@3.17.1 requires a peer of typescript@>=2.8.0 || >= 3.2.0-dev || >= 3.3.0-dev || >= 3.4.0-dev || >= 3.5.0-dev || >= 3.6.0-dev || >= 3.6.0-beta || >= 3.7.0-dev || >= 3.7.0-beta but none is installed. You must install peer dependencies yourself.
npm ERR! code EACCES
npm ERR! syscall access
npm ERR! path /home/user/project/node_modules
npm ERR! errno -13
npm ERR! Error: EACCES: permission denied, access '/home/user/project/node_modules'
npm ERR!  [Error: EACCES: permission denied, access '/home/user/project/node_modules'] {
npm ERR!   stack: "Error: EACCES: permission denied, access '/home/user/project/node_modules'",
npm ERR!   errno: -13,
npm ERR!   code: 'EACCES',
npm ERR!   syscall: 'access',
npm ERR!   path: '/home/user/project/node_modules'
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.
npm ERR! A complete log of this run can be found in:
npm ERR!     /home/user/.npm/_logs/2019-12-03T04_30_10_304Z-debug.log

使用 sudo:

> node@8.10.0 preinstall /home/user/project/node_modules/node
> node installArchSpecificPackage

npm WARN checkPermissions Missing write access to /home/user/project/node_modules/node
npm ERR! code EACCES
npm ERR! syscall access
npm ERR! path /home/user/project/node_modules/node
npm ERR! errno -13
npm ERR! Error: EACCES: permission denied, access '/home/user/project/node_modules/node'
npm ERR!  [Error: EACCES: permission denied, access '/home/user/project/node_modules/node'] {
npm ERR!   stack: "Error: EACCES: permission denied, access '/home/user/project/node_modules/node'",
npm ERR!   errno: -13,
npm ERR!   code: 'EACCES',
npm ERR!   syscall: 'access',
npm ERR!   path: '/home/user/project/node_modules/node'
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.

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/user/.npm/_logs/2019-12-03T04_30_23_864Z-debug.log
internal/modules/cjs/loader.js:895
    throw err;
    ^

Error: Cannot find module 'node-linux-x64/package.json'
Require stack:
- /home/user/project/node_modules/node/installArchSpecificPackage.js
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:892:15)
    at Function.resolve (internal/modules/cjs/helpers.js:80:19)
    at ChildProcess.<anonymous> (/home/user/project/node_modules/node-bin-setup/index.js:18:27)
    at ChildProcess.emit (events.js:210:5)
    at maybeClose (internal/child_process.js:1028:16)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:283:5) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    '/home/user/project/node_modules/node/installArchSpecificPackage.js'
  ]
}
npm WARN tsutils@3.17.1 requires a peer of typescript@>=2.8.0 || >= 3.2.0-dev || >= 3.3.0-dev || >= 3.4.0-dev || >= 3.5.0-dev || >= 3.6.0-dev || >= 3.6.0-beta || >= 3.7.0-dev || >= 3.7.0-beta but none is installed. You must install peer dependencies yourself.

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! node@8.10.0 preinstall: `node installArchSpecificPackage`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the node@8.10.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!     /home/user/.npm/_logs/2019-12-03T04_30_24_450Z-debug.log

有没有更好的方法来解决这个问题而不依赖于不安全的 perm 参数?

这是解决此错误的方法。 运行 这个命令:

sudo chown -R $USER /home/user/project/node_modules/

Pay attention to the folder listed by the error message. If it’s different, update the chown command accordingly.

让我们分解一下:

sudo 表示我们运行以系统超级用户身份执行此命令。这是因为我们没有写入该文件夹的权限,但 root 将能够修复任何权限。此命令还意味着系统将要求您输入密码以进行确认。

chown 是我们用来更改文件或文件夹的所有者的命令。我们设置 -R 选项以递归地更改所有者,因此我们也获得了所有者对其中已包含的所有文件的访问权限。

$USER 是自动设置为您的用户名的环境变量。

最后一块是文件夹路径。

运行选择此路径将使该文件夹成为您的文件夹,因此您可以安全地 运行 您的 npm install -g <package> 命令!