Linux环境下,root账户,安装某些npm包的时候报下面的错误,例如安装grunt-contrib-imagemin时:
Error: EACCES, mkdir '/usr/local/lib/node_modules/coffee-script' npm ERR! { [Error: EACCES, mkdir '/usr/local/lib/node_modules/coffee-script'] npm ERR! errno: 3, npm ERR! code: 'EACCES', npm ERR! path: '/usr/local/lib/node_modules/coffee-script', npm ERR! fstream_type: 'Directory', npm ERR! fstream_path: '/usr/local/lib/node_modules/coffee-script', npm ERR! fstream_class: 'DirWriter', npm ERR! fstream_stack: npm ERR! [ 'DirWriter._create (/usr/local/lib/node_modules/npm/node_modules/fstream/lib/dir-writer.js:36:23)', npm ERR! '/usr/local/lib/node_modules/npm/node_modules/mkdirp/index.js:37:53', npm ERR! 'Object.oncomplete (fs.js:297:15)' ] } npm ERR! npm ERR! Please try running this command again as root/Administrator.
解决办法如下:
- 执行npm install命令时加上--unsafe-perm选项:
[sudo] npm install --unsafe-perm
- 或者将unsafe-perm选项添加到package.json文件中:
"config": { "unsafe-perm":true }
- 不要使用preinstall脚本来安装全局模块,你可以单独安装它们然后在非root账户下安装其它模块,就像下面这样:
sudo npm install -g coffee-script node-gyp npm install
具体内容可以看下面这两个页面的介绍:
https://docs.npmjs.com/misc/scripts#user
https://docs.npmjs.com/getting-started/fixing-npm-permissions
大致意思就是说:如果npm是在root账户下执行的话,它会将uid改成当前账户,或者uid的值从user配置文件中获取,而默认情况下uid的值为nobody。所以在root账户下运行npm install时需要将unsafe-perm选项加上。
本文转自Jaxu博客园博客,原文链接:http://www.cnblogs.com/jaxu/p/5864134.html,如需转载请自行联系原作者