sudo npm install --global gulp 报错
node-pre-gyp WARN Using needle for node-pre-gyp https download node-pre-gyp WARN Pre-built binaries not installable for fsevents@1.2.8 and node@10.15.3 (node-v64 ABI, unknown) (falling back to source compile with node-gyp) node-pre-gyp WARN Hit error EACCES: permission denied, mkdir '/usr/local/lib/node_modules/gulp/node_modules/fsevents/lib' gyp WARN EACCES user "root" does not have permission to access the dev dir "/Users/liruinan/.node-gyp/10.15.3"gyp WARN EACCES attempting to reinstall using temporary dev dir "/usr/local/lib/node_modules/gulp/node_modules/fsevents/.node-gyp"gyp WARN install got an error, rolling back installgyp WARN install got an error, rolling back installgyp ERR! configure error gyp ERR! stack Error: EACCES: permission denied, mkdir '/usr/local/lib/node_modules/gulp/node_modules/fsevents/.node-gyp'gyp ERR! System Darwin 16.1.0gyp ERR! command "/usr/local/bin/node" "/usr/local/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "configure" "--fallback-to-build" "--module=/usr/local/lib/node_modules/gulp/node_modules/fsevents/lib/binding/Release/node-v64-darwin-x64/fse.node" "--module_name=fse" "--module_path=/usr/local/lib/node_modules/gulp/node_modules/fsevents/lib/binding/Release/node-v64-darwin-x64" "--napi_version=3" "--node_abi_napi=napi" "--napi_build_version=0" "--node_napi_label=node-v64"gyp ERR! cwd /usr/local/lib/node_modules/gulp/node_modules/fseventsgyp ERR! node -v v10.15.3gyp ERR! node-gyp -v v3.8.0gyp ERR! not ok node-pre-gyp ERR! build error node-pre-gyp ERR! stack Error: Failed to execute '/usr/local/bin/node /usr/local/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js configure --fallback-to-build --module=/usr/local/lib/node_modules/gulp/node_modules/fsevents/lib/binding/Release/node-v64-darwin-x64/fse.node --module_name=fse --module_path=/usr/local/lib/node_modules/gulp/node_modules/fsevents/lib/binding/Release/node-v64-darwin-x64 --napi_version=3 --node_abi_napi=napi --napi_build_version=0 --node_napi_label=node-v64' (1)node-pre-gyp ERR! stack at ChildProcess.(/usr/local/lib/node_modules/gulp/node_modules/fsevents/node_modules/node-pre-gyp/lib/util/compile.js:83:29)node-pre-gyp ERR! stack at ChildProcess.emit (events.js:189:13)node-pre-gyp ERR! stack at maybeClose (internal/child_process.js:970:16)node-pre-gyp ERR! stack at Process.ChildProcess._handle.onexit (internal/child_process.js:259:5)node-pre-gyp ERR! System Darwin 16.1.0node-pre-gyp ERR! command "/usr/local/bin/node" "/usr/local/lib/node_modules/gulp/node_modules/fsevents/node_modules/node-pre-gyp/bin/node-pre-gyp" "install" "--fallback-to-build"node-pre-gyp ERR! cwd /usr/local/lib/node_modules/gulp/node_modules/fseventsnode-pre-gyp ERR! node -v v10.15.3node-pre-gyp ERR! node-pre-gyp -v v0.12.0node-pre-gyp ERR! not ok
解决方法
npm install 添加参数 --unsafe-perm
sudo npm install --global gulp --unsafe-perm
理由
If npm detects it is running as root it drops to a non-privileged user which then doesn‘t have permissions to write to /root/.node-gyp
.
##如果NPM检测到它以根用户身份运行,它将下降到一个没有权限写入/root/.node gyp的非特权用户。
The --unsafe-perm option stops it from changing user.
nvm doesn‘t have this problem when not using sudo because it stores everything under the current users‘ home directory.
##NVM在不使用sudo时没有这个问题,因为它将所有内容存储在当前用户的主目录下。
看官方文档的解释
unsafe-perm
Default: false if running as root, true otherwiseType: BooleanSet to true to suppress the UID/GID switching when running package scripts.##设置为true可在运行包脚本时抑制uid/gid切换。
If set explicitly to false, then installing as a non-root user will fail.