In the Linux environment, the root account, when installing certain NPM packages, reported the following errors, such as when installing 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 the This command again as Root/administrator.
The solution is as follows:
- Add the --unsafe-perm option when executing the NPM install command:
[sudoinstall --unsafe-perm
- Or add the unsafe-perm option to the Package.json file:
"Config": { "unsafe-perm":true}
- Do not use preinstall scripts to install the global modules, you can install them separately and then install other modules under the non-root account, as follows:
sudo Install -G coffee-scriptnode-Install
The specific content can be seen in the following two pages of the introduction:
Https://docs.npmjs.com/misc/scripts#user
Https://docs.npmjs.com/getting-started/fixing-npm-permissions
The general meaning is: If NPM is executed under the root account, it will change the UID to the current account, or the value of the UID is taken from the user profile, and the UID value is nobody by default. So the unsafe-perm option needs to be added when running npm install under the root account.
NPM install failed with "cannot run in WD"