npm安装突然因各种错误而失败,包括在Ubuntu上的权限被拒绝(使用sudo和不使用sudo)

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

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

没有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!   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,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参数?

chener888 回答:npm安装突然因各种错误而失败,包括在Ubuntu上的权限被拒绝(使用sudo和不使用sudo)

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

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

请注意错误消息列出的文件夹。如果它是   有所不同,请相应地更新chown命令。

让我们分解一下:

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

chown是我们用来更改文件或文件夹所有者的命令。我们设置-R选项以递归方式更改所有者,因此我们还可以使所有者访问其中已经包含的所有文件。

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

最后一块是文件夹路径。

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

本文链接:https://www.f2er.com/2988622.html

大家都在问