Recently, I encountered a node sass package installation problem in my own project. The problem is as follows. When I sudo NPMI
get the following error message, I tried to modify the permissions and the version of the package, but I still couldn’t solve it. Finally, Google finally solved it. Here, I write down the cause of the problem and the solution
gyp verb build dir attempting to create "build" dir: /Study/github/react-view/node_modules/node-sass/build
gyp ERR! configure error
gyp ERR! stack Error: EACCES: permission denied, mkdir '/Study/github/react-view/node_modules/node-sass/build'
gyp ERR! System Darwin 17.7.0
gyp ERR! command "/usr/local/bin/node" "/Study/github/react-view/node_modules/node-gyp/bin/node-gyp.js" "rebuild" "--verbose" "--libsass_ext=" "--libsass_cflags=" "--libsass_ldflags=" "--libsass_library="
gyp ERR! cwd /Study/github/react-view/node_modules/node-sass
gyp ERR! node -v v10.15.3
gyp ERR! node-gyp -v v3.8.0
gyp ERR! not ok
First of all, the reason for this problem is that NPM will have a life cycle, and a package will have a life cycle to execute something. For security reasons, it will be automatically degraded, resulting in no permission to perform some operations. Use the — unsafe perm parameter to unlock the restriction. Before the NPM lifecycle command is executed, it will judge that the configuration of unsafe perm is true before continuing, otherwise it will exit ahead of time
// lib/utils/lifecycle.js
unsafe = unsafe || npm.config.get('unsafe-perm')
if ((wd.indexOf(npm.dir) !== 0 || _incorrectWorkingDirectory(wd, pkg)) && !unsafe && pkg.scripts[stage]) {
log.warn('lifecycle', logid(pkg, stage), 'cannot run in wd',
'%s %s (wd=%s)', pkg._id, pkg.scripts[stage], wd
)
return cb()
}
For the reading order of configuration, please refer to NPM config, that is, CLI – > env -> npmrc -> default。
The initialization of unsafe perm in default is as follows:
// lib/config/defaults.js
'unsafe-perm': process.platform === 'win32' ||
process.platform === 'cygwin' ||
!(process.getuid && process.setuid &&
process.getgid && process.setgid) ||
process.getuid() !== 0
For UNIX platform, the default value of NPM command executed by root user will be false
solutions :
Temporarily modify NPM configuration during installation sudo NPM install — unsafe perm
Set the default configuration of NPM in the project NPM config set unsafe perm = true
Create. Npmrc in the project to override the default configuration
// .npmrc
unsafe-perm = true
Global setting NPM config – G set unsafe perm
Here, you need to change the permission of the node directory to root
chown root:root node
Solutions provided by NPM
## create .npm-global folder
mkdir ~/.npm-global
## set
npm config set prefix '~/.npm-global'
export PATH=~/.npm-global/bin:$PATH
source ~/.profile