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 install gyp WARN install got an error, rolling back install gyp 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.0 gyp 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/fsevents gyp ERR! node -v v10.15.3 gyp ERR! node-gyp -v v3.8.0 gyp 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.<anonymous> (/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.0 node-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/fsevents node-pre-gyp ERR! node -v v10.15.3 node-pre-gyp ERR! node-pre-gyp -v v0.12.0 node-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 otherwise
Type: Boolean
Set 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.