Gulp 安装权限问题

Gulp installation permission issue

我想在 Mac 上安装 Gulp 4.0.2,但由于缺少写入权限而失败。

$ npm i -S gulp -g

npm WARN checkPermissions Missing write access to /usr/local/lib/node_modules
npm ERR! path /usr/local/lib/node_modules
npm ERR! code EACCES
npm ERR! errno -13
npm ERR! syscall access
npm ERR! Error: EACCES: permission denied, access '/usr/local/lib/node_modules'
npm ERR!  { [Error: EACCES: permission denied, access '/usr/local/lib/node_modules']
npm ERR!   stack:
npm ERR!    'Error: EACCES: permission denied, access \'/usr/local/lib/node_modules\'',
npm ERR!   errno: -13,
npm ERR!   code: 'EACCES',
npm ERR!   syscall: 'access',
npm ERR!   path: '/usr/local/lib/node_modules' }
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 (though this is not recommended).

似乎Gulp安装需要root权限,但不推荐。我不想以管理员身份安装 gulp :(

是否可以创建另一个用户 w/o root 权限并从那里安装 Gulp?如果是,该怎么做?

任何建议将不胜感激,谢谢!

您可以将 gulp 安装到您的项目目录本地而不是全局:

npm install gulp

然后您可以 运行 使用 npx 的本地版本:

npx gulp

注意:npx 与 NPM 5.2+ 版本捆绑在一起


您还可以在 package.json:

中使用脚本
"scripts": {
  "gulp": "gulp"
}
npm run gulp

避免这些权限问题的官方文档...

https://docs.npmjs.com/resolving-eacces-permissions-errors-when-installing-packages-globally

如果您使用的是较新的 mac,请务必尝试 touch ~/.zshrc