今天下載wepy-cli的時候報錯了
sudo npm i wepy-cli -g
gyp ERR! configure error gyp ERR! stack Error: EACCES: permission denied, mkdir '/usr/local/lib/node_modules/wepy-cli/node_modules/fsevents/.node-gyp' gyp ERR! System Darwin 19.2.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/wepy-cli/node_modules/fsevents/lib/binding/Release/node-v72-darwin-x64/fse.node" "--module_name=fse" "--module_path=/usr/local/lib/node_modules/wepy-cli/node_modules/fsevents/lib/binding/Release/node-v72-darwin-x64" "--napi_version=5" "--node_abi_napi=napi" "--napi_build_version=0" "--node_napi_label=node-v72" gyp ERR! cwd /usr/local/lib/node_modules/wepy-cli/node_modules/fsevents gyp ERR! node -v v12.13.1 gyp ERR! node-gyp -v v5.0.5 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/wepy-cli/node_modules/fsevents/lib/binding/Release/node-v72-darwin-x64/fse.node --module_name=fse --module_path=/usr/local/lib/node_modules/wepy-cli/node_modules/fsevents/lib/binding/Release/node-v72-darwin-x64 --napi_version=5 --node_abi_napi=napi --napi_build_version=0 --node_napi_label=node-v72' (1) node-pre-gyp ERR! stack at ChildProcess.<anonymous> (/usr/local/lib/node_modules/wepy-cli/node_modules/fsevents/node_modules/node-pre-gyp/lib/util/compile.js:83:29) node-pre-gyp ERR! stack at ChildProcess.emit (events.js:210:5) node-pre-gyp ERR! stack at maybeClose (internal/child_process.js:1021:16)
說沒有權限,怎么會,我加sudo了呀。
折騰了半天查到一個命令
sudo npm install --unsafe-perm -g wepy-cli
這樣就可以了。
為什么呢?
答:就是說 npm 出於安全考慮不支持以 root 用戶運行,即使你用 root 用戶身份運行了,npm 會自動轉成一個叫 nobody 的用戶來運行,而這個用戶幾乎沒有任何權限。這樣的話如果你腳本里有一些需要權限的操作,比如寫文件(尤其是寫 /root/.node-gyp),就會崩掉了。
為了避免這種情況,要么按照 npm 的規矩來,專門建一個用於運行 npm 的高權限用戶;要么加 --unsafe-perm 參數,這樣就不會切換到 nobody 上,運行時是哪個用戶就是哪個用戶,即使是 root。