
48

npm 遇到 write access的问题怎么办 - 简书
source link: https://www.jianshu.com/p/31744aa44824?
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

npm 遇到 write access的问题怎么办
22018.02.09 10:59:40字数 479阅读 34,975
编程语言和框架的模块化使得我们更加的懒惰。
今天在使用npm install -g
语法的时候,总是出错。日志文件显示对npm-modules下的子文件夹没有写的权限(在安装modules的时候会创建这个模块的文件夹)。情况如下:
npm WARN checkPermissions Missing write access to /usr/local/lib/node_modules/webpack/node_modules/loader-utils
npm WARN checkPermissions Missing write access to /usr/local/lib/node_modules/webpack/node_modules/mkdirp
npm WARN checkPermissions Missing write access to /usr/local/lib/node_modules/webpack/node_modules/node-libs-browser
npm WARN checkPermissions Missing write access to /usr/local/lib/node_modules/webpack/node_modules/supports-color
npm WARN checkPermissions Missing write access to /usr/local/lib/node_modules/webpack/node_modules/uglifyjs-webpack-plugin
npm WARN checkPermissions Missing write access to /usr/local/lib/node_modules/webpack/node_modules/watchpack
npm ERR! path /usr/local/lib/node_modules/webpack/node_modules/camelcase
npm ERR! code ENOENT
npm ERR! errno -2
npm ERR! syscall access
npm ERR! enoent ENOENT: no such file or directory, access '/usr/local/lib/node_modules/webpack/node_modules/camelcase'
npm ERR! enoent This is related to npm not being able to find a file.
npm ERR! enoent
npm ERR! A complete log of this run can be found in:
npm ERR! /Users/xxx/.npm/_logs/2018-02-09T02_33_46_372Z-debug.log
然后使用了chmod命令更改了这个文件夹的权限是777
之后仍然不行。最后使用sudo命令来安装,神奇的情况是尽然也是显示同样的权限问题。最后只好求助于谷歌,正好翻到node开发的issue的追踪,里面详细的说明了怎么巧妙的解决这个问题。
官方给出的一个解决办法是给npm的global安装位置换个地方,因为默认的安装位置是/usr/local/lib所在的文件夹,这是系统的文件夹所在地,所以可能会出现一些读写问题。将module的安装根目录设置在一般的文件夹下则没有这么多问题,下面是官方方法:
# 第一步:在你的用户文件下新建一个文件夹,这个.npm-global 名字可以用你自己喜欢的名字替换,推荐直接使用这个名字。
mkdir ~/.npm-global
#第二步:更改node的安装连接
npm config set prefix '~/.npm-global'
#第三步:在用户的profile下增加path,为的是系统能够找到可执行文件的目录
export PATH=~/.npm-global/bin:$PATH
#第四步:update profile。使其生效
source ~/.profile
经过上面的四步骤,就可以使用npm安装了,效果如下:
/Users/zyc/.npm-global/bin/webpack -> /Users/xxx/.npm-global/lib/node_modules/webpack/bin/webpack.js
> [email protected] install /Users/xxx/.npm-global/lib/node_modules/webpack/node_modules/fsevents
> node install
[fsevents] Success: "/Users/xxx/.npm-global/lib/node_modules/webpack/node_modules/fsevents/lib/binding/Release/node-v59-darwin-x64/fse.node" is installed via remote
> [email protected] postinstall /Users/zyc/.npm-global/lib/node_modules/webpack/node_modules/uglifyjs-webpack-plugin
> node lib/post_install.js
+ [email protected]
added 367 packages in 31.471s
我们可以看到上面的安装命令中已经显示,新安装的webpack已经安装到用户下的.npm-global
文件夹下面了。使用这个命令需要注意的是,可能你之前安装在默认位置的一些模块没法使用,解决的办法就是重新安装或是将原来位置的模块拷贝到新的文件位置,或是在profile文件中增添原来文件的path位置,为了不出错,还是直接重新安装吧。
</article
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK