npm install -g node-gyp error -13

错误内容

复制代码
npm install -g node-gyp
npm ERR! code EACCES
npm ERR! syscall mkdir
npm ERR! path /usr/local/lib/node_modules/node-gyp
npm ERR! errno -13
npm ERR! Error: EACCES: permission denied, mkdir '/usr/local/lib/node_modules/node-gyp'
npm ERR!  [Error: EACCES: permission denied, mkdir '/usr/local/lib/node_modules/node-gyp'] {
npm ERR!   errno: -13,
npm ERR!   code: 'EACCES',
npm ERR!   syscall: 'mkdir',
npm ERR!   path: '/usr/local/lib/node_modules/node-gyp'
npm ERR! }
npm ERR! 
npm ERR! The operation was rejected by your operating system.
npm ERR! It is likely you do not have the permissions to access this file as the current user
npm ERR! 
npm ERR! If you believe this might be a permissions issue, please double-check the
npm ERR! permissions of the file and its containing directories, or try running
npm ERR! the command again as root/Administrator.

解决办法

这个错误表明您在尝试全局安装 node-gyp 时遇到了权限问题。这可能是因为您没有足够的权限将模块安装到全局目录。

有几种方法可以解决这个问题:

1. 使用 sudo(需要管理员权限)

使用管理员权限运行 npm install 命令。请注意,这样做可能会导致一些权限问题,因此请确保您知道自己在做什么。

复制代码
sudo npm install -g node-gyp

2. 使用 Node 版本管理器(推荐)

使用 Node 版本管理器(如 nvm 或 n)来安装 Node.js 和 npm,并确保您有足够的权限来安装全局模块。这样可以避免以管理员身份运行 npm。

首先,安装 Node 版本管理器。以下是使用 nvm 的示例:

复制代码
curl -o- <https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh> | bash

然后,关闭并重新打开终端或运行 source ~/.bashrcsource ~/.zshrc,以加载 nvm。

接下来,使用 nvm 安装 Node.js:

复制代码
nvm install node

最后,您可以全局安装 node-gyp

复制代码
npm install -g node-gyp

这样可以确保您在用户目录下具有足够的权限,而无需使用 sudo

注意:

  • 如果您使用 sudo 安装,请小心操作,并确保您了解潜在的风险。
  • 在尝试解决问题之前,请备份重要数据。
  • 使用 Node 版本管理器可以更方便地管理不同版本的 Node.js,并避免权限问题。
相关推荐
10年前端老司机2 小时前
10道js经典面试题助你找到好工作
前端·javascript
小小小小宇7 小时前
TS泛型笔记
前端
小小小小宇7 小时前
前端canvas手动实现复杂动画示例
前端
codingandsleeping7 小时前
重读《你不知道的JavaScript》(上)- 作用域和闭包
前端·javascript
小小小小宇8 小时前
前端PerformanceObserver使用
前端
zhangxingchao9 小时前
Flutter中的页面跳转
前端
烛阴9 小时前
Puppeteer入门指南:掌控浏览器,开启自动化新时代
前端·javascript
全宝10 小时前
🖲️一行代码实现鼠标换肤
前端·css·html
小小小小宇11 小时前
前端模拟一个setTimeout
前端
萌萌哒草头将军11 小时前
🚀🚀🚀 不要只知道 Vite 了,可以看看 Farm ,Rust 编写的快速且一致的打包工具
前端·vue.js·react.js