npm安装报错:Error: EACCES: permission denied

报错如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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:

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、建立全局文件夹配置

1
2
3
mkdir ~/npm-global
  
npm config set prefix '~/npm-global'

  ps:在mac下不要建以点(.)开头的配置文件,否则还是解决不了。

2、修改环境变量

1
vi /etc/profile

  如果文件是只读的,改一下文件的读写权限

1
sudo chmod 777 /etc/profile

 在/etc/profile追加

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

3、激活环境变量配置

1
source /etc/profile

4、再次按装

1
2
3
4
5
6
7
8
9
10
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 发现

1
zsh: command not found: webpack

  那添加到环境变量就可以了。

执行以下操作:

1
vi .bash_profile  

 在.bash_profile添加

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

  执行以下命令,让环境变量生效。

1
source .bash_profile

 

OK,完美解决权限不够的问题。

参考:  

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

  

 

posted @   地铁程序员  阅读(2391)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
点击右上角即可分享
微信分享提示