npm权限调试小记

Posted by

今天安装javascript的lovefield插件,github地址为:https://github.com/google/lovefield/blob/master/demos/todo/README.md

根据这个教程,首先是要安装gulp等插件的。

我将npm install -g gulp·输入终端,结果出现了这样的错误。

npm ERR! code EACCES
npm ERR! syscall mkdir
npm ERR! path /usr/lib/node_modules/gulp
npm ERR! errno -13
npm ERR! Error: EACCES: permission denied, mkdir '/usr/lib/node_modules/gulp'
npm ERR!  [Error: EACCES: permission denied, mkdir '/usr/lib/node_modules/gulp'] {
npm ERR!   errno: -13,
npm ERR!   code: 'EACCES',
npm ERR!   syscall: 'mkdir',
npm ERR!   path: '/usr/lib/node_modules/gulp'
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/scheng/.npm/_logs/2021-08-26T14_32_18_031Z-debug.log

起先以为是权限问题,于是就很粗糙地加了个sudo,也就是sudo npm install -g gulp

结果就出现了一个类似代理问题的东西:

npm ERR! code ENOTFOUND
npm ERR! syscall getaddrinfo
npm ERR! errno ENOTFOUND
npm ERR! network request to https://registry.npm.taobao.org+/bower failed, reason: getaddrinfo ENOTFOUND registry.npm.taobao.org+
npm ERR! network This is a problem related to network connectivity.
npm ERR! network In most cases you are behind a proxy or have bad network settings.
npm ERR! network 
npm ERR! network If you are behind a proxy, please make sure that the
npm ERR! network 'proxy' config is set properly.  See: 'npm help config'

npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2021-08-26T14_12_05_523Z-debug.log

首先是解决这个问题,查找一番以为是npm设置了代理,但是代理并没有设置正确,就导致了无法正常下载。
那么先npm config list检查一下配置,发现是没有设置代理的。

; "user" config from /home/scheng/.npmrc

proxy = false 
registry = "https://registry.npmjs.org/" 

; node bin location = /usr/bin/node
; cwd = /home/scheng/eaf-rss-reader
; HOME = /home/scheng
; Run `npm config ls -l` to show all defaults.

然后就突发奇想,要不设置个代理?于是就npm config set proxy http://x.xxx.xxx.xxx:xxx,然后重新install,结果还是无法正常下载,就默默地把这一行配置有删掉了…

然后回头解决最初的那个问题,也就是权限的问题。
可以根据这个网页的内容来操作:https://stackoverflow.com/questions/48910876/error-eacces-permission-denied-access-usr-local-lib-node-modules

先是
ls -la /usr/local/lib/node_modules查看一下node_modules的权限,但是终端给我报错说并没有/usr/local/lib/node_modules这个目录, 那么就whereis node_modules查看一下它在哪里,原来我的arch把它装在了/usr/lib/node_modules这个目录…那么后续就是ls -la /usr/lib/node_modules 查看权限,如下:

drwxr-xr-x   8 root root   4096  7月 22 15:11 .
drwxr-xr-x 251 root root 270336  8月 26 00:54 ..
drwxr-xr-x   5 root root   4096  5月 28 21:23 cnpm
drwxr-xr-x   9 root root   4096  7月 25 16:58 node-gyp
drwxr-xr-x   5 root root   4096  7月 22 15:11 nopt
drwxr-xr-x   6 root root   4096  8月  4 12:13 npm
drwxr-xr-x   8 root root   4096  5月 27 10:26 semver
drwxr-xr-x   7 root root   4096  5月 28 21:24 vue-cli

这里普通用户是需要root权限的,链接里面提供了三种方法,这里不再赘述。我使用的是:sudo chown -R ownerName: /usr/lib/node_modules

然后再去跑最初的那个安装命令,就能完美安装啦。

Leave a Reply

邮箱地址不会被公开。 必填项已用*标注