npm安裝報錯:Error: EACCES: permission denied


報錯如下:

sudo npm i webpack -g
/Users/xesfe/.npm-global/bin/webpack -> /Users/xesfe/.npm-global/lib/node_modules/webpack/bin/webpack.js
# System-wide .profile for sh(1)

> fsevents@1.2.11 install /Users/xesfe/.npm-global/lib/node_modules/webpack/node_modules/fsevents
> node-gyp rebuild

gyp ERR! configure error
gyp ERR! stack Error: EACCES: permission denied, mkdir '/Users/xesfe/.npm-global/lib/node_modules/webpack/node_modules/fsevents/build'
gyp ERR! System Darwin 19.2.0
gyp ERR! command "/Users/xesfe/.nvm/versions/node/v10.16.2/bin/node" "/Users/xesfe/.nvm/versions/node/v10.16.2/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "rebuild"
gyp ERR! cwd /Users/xesfe/.npm-global/lib/node_modules/webpack/node_modules/fsevents
gyp ERR! node -v v10.16.2
gyp ERR! node-gyp -v v3.8.0
gyp ERR! not ok
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.11 (node_modules/webpack/node_modules/fsevents):
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.11 install: `node-gyp rebuild`
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: Exit status 1

+ webpack@4.41.4
updated 1 package in 22.243s

方案1:

 npm install --unsafe-perm=true --allow-root -g nodeppt

  在安裝的時候一直報錯nodeppt,通過查資料添加--unsafe-perm=true --allow-root,可以解決問題,git上issue:https://github.com/npm/npm/issues/17268,這方案貌似不是那么的優雅,因此尋找第二種方案。

方案2

1、建立全局文件夾配置

mkdir ~/npm-global
 
npm config set prefix '~/npm-global'

  ps:在mac下不要建以點(.)開頭的配置文件,否則還是解決不了。

2、修改環境變量

vi /etc/profile 

  如果文件是只讀的,改一下文件的讀寫權限

sudo chmod 777 /etc/profile

 在/etc/profile追加

# npm配置
export PATH=~/npm-global/bin:$PATH

3、激活環境變量配置

source /etc/profile

4、再次按裝

i webpack -g
/Users/xesfe/npm-config/bin/webpack -> /Users/xesfe/npm-config/lib/node_modules/webpack/bin/webpack.js

> fsevents@1.2.11 install /Users/xesfe/npm-config/lib/node_modules/webpack/node_modules/fsevents
> node-gyp rebuild

  SOLINK_MODULE(target) Release/.node
  CXX(target) Release/obj.target/fse/fsevents.o
  SOLINK_MODULE(target) Release/fse.node
+ webpack@4.41.4

5、如果你執行webpack 發現

zsh: command not found: webpack

  那添加到環境變量就可以了。

執行以下操作:

vi .bash_profile  

 在.bash_profile添加

export NPM_CONFIG=~/npm-config
export PATH=$PATH:$NPM_CONFIG/bin

  執行以下命令,讓環境變量生效。

source .bash_profile

 

OK,完美解決權限不夠的問題。

參考:  

https://www.jianshu.com/p/7192b336c6cd

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM