EACCES: permission denied, open '/root/.npmrc'

Linux系统中运行npm install时报EACCES: permission denied

配置完nodejs环境变量后,node -v查看版本信息没问题,但是当我是用npm install时报如下错误:


> mirror-config-china@2.5.1 preinstall /dssp/dssp-web/node_modules/mirror-config-china
> node -e "require('util').promisify.call" || npm i util.promisify


> mirror-config-china@2.5.1 install /dssp/dssp-web/node_modules/mirror-config-china
> node lib/install

EACCES: permission denied, open '/root/.npmrc'
Please try running this command again as root/Administrator.
(node:10878) UnhandledPromiseRejectionWarning: Error: EACCES: permission denied, open '/root/.bash_profile'
(node:10878) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:10878) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.4 (node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.4: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! mirror-config-china@2.5.1 install: `node lib/install`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the mirror-config-china@2.5.1 install 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!     /root/.npm/_logs/2018-08-24T08_17_26_334Z-debug.log

引起原因
根据错误信息,知道这是权限问题,那么要么是文件没有执行权限,要么是用户不正确,所以查看用户的所有者和文件权限,发现文件所有者非当前登录用户(root)。
EACCES: permission denied, open '/root/.npmrc'_第1张图片
问题发现了,接下来就好办了,有两个方法:1、更改文件所有者为root或者切换到500用户,2、用sudo指定当前登录用户来执行。
解决方法1
在node的文件外,运行chown -R root:root node-v8.11.4-linux-x64,把node-v8.11.4-linux-x64文件及子文件用户和组更改为root。让后再运行npm install就没有问题了。
EACCES: permission denied, open '/root/.npmrc'_第2张图片
解决方法2
通过sudo指定当前登录用户来运行当前命令

sudo npm install

但是,因为我是自定义nodejs的文件目录的,所以sudo npm命令是无效的,报如下错误。

sudo: npm: command not found

出现个这个错误,是在/usr/bin下没有npm的链接文件或者链接文件路径不正确,所以我重新建立链接文件,运行如下命令即可:

sudo ln -s /app/node-v8.11.4-linux-x64/bin/node /usr/bin/node
sudo ln -s /app/node-v8.11.4-linux-x64/lib/node /usr/lib/node
sudo ln -s /app/node-v8.11.4-linux-x64/bin/npm /usr/bin/npm
sudo ln -s /app/node-v8.11.4-linux-x64/bin/node-waf /usr/bin/node-waf

添加链接文件后,可以在/usr/bin下通过ll命令查看链接文件地址是否正确(即->后的地址)
EACCES: permission denied, open '/root/.npmrc'_第3张图片
链接文件地址正确后再运行sudo npm install即可

你可能感兴趣的:(EACCES: permission denied, open '/root/.npmrc')