今天使用node的npm安装插件的时候遇到一个问题,那就是在项目目录里面安装插件的时候,必须使用超级用户(sudo)执行才会安装成功,否则会报如下错误:
以安装 gulp-uglify 为例
$ npm install gulp-uglify --save-dev npm WARN locking Error: EACCES, open '/Users/zyy/.npm/_locks/gulp-uglify-dadf3a03924005c9.lock' npm WARN locking /Users/zyy/.npm/_locks/gulp-uglify-dadf3a03924005c9.lock failed { [Error: EACCES, open '/Users/zyy/.npm/_locks/gulp-uglify-dadf3a03924005c9.lock'] npm WARN locking errno: 3, npm WARN locking code: 'EACCES', npm WARN locking path: '/Users/zyy/.npm/_locks/gulp-uglify-dadf3a03924005c9.lock' } npm ERR! Darwin 13.4.0 npm ERR! argv "node" "/usr/local/bin/npm" "install" "gulp-uglify" "--save-dev" npm ERR! node v0.10.32 npm ERR! npm v2.1.7 npm ERR! Attempt to unlock /Users/zyy/Sites/gulp-p/node_modules/gulp-uglify, which hasn't been locked npm ERR! npm ERR! If you need help, you may report this error at: npm ERR! <http://github.com/npm/npm/issues> npm ERR! Please include the following file with any support request: npm ERR! /Users/zyy/Sites/gulp-p/npm-debug.log
网上查了下,好像是 ~/.npm 权限的问题,那么我们就先查看下 ~/.npm 的权限:
$ ls -al ~/ | grep .npm drwxr-xr-x 264 zyy staff 8976 Nov 18 09:45 .npm
接下来我们再更改 .npm 的权限:
$ sudo chown -R zyy /Users/zyy/.npm 或: $ sudo chown -R zyy ~/.npm
执行该权限修改命令后,只要不是安装插件到全局,就不需要使用sudo。但是有一个奇怪到事就是,我刚修改到 .npm 的权限没有改变。而现在再当前目录安装插件却不要再使用 sudo 超级用户权限了。