返回介绍

How to Prevent Permissions Errors

发布于 2019-05-30 13:06:22 字数 2110 浏览 876 评论 0 收藏 0

If you see an EACCES error when you try to install a package globally, read this chapter. This error can often be avoided if you change the directory where npm is installed. To do this, either:

  1. Reinstall npm with a version manager (recommended),

    or

  2. Change npm's default directory manually.

Option One: Reinstall with a Node Version Manager

This is the best way to avoid permissions issues. This is described in Option Two: Change npm's Default Directory

This section does not apply to Microsoft Windows. This chapter will be updated to address Windows in the near future.

To minimize the chance of permissions errors, you can configure npm to use a different directory. In this example, it will be a hidden directory on your home folder.

  1. Back-up your computer before you start.

  2. Make a directory for global installations:

     mkdir ~/.npm-global
    
  3. Configure npm to use the new directory path:

     npm config set prefix '~/.npm-global'
    
  4. Open or create a ~/.profile file and add this line:

     export PATH=~/.npm-global/bin:$PATH
    
  5. Back on the command line, update your system variables:

     source ~/.profile
    

Test: Download a package globally without using sudo.

    npm install -g jshint

Instead of steps 2-4, you can use the corresponding ENV variable (e.g. if you don't want to modify ~/.profile):

    NPM_CONFIG_PREFIX=~/.npm-global

Tip: Consider npx

If you are using npm version 5.2 or greater, explore npx as an alternative way to run global commands, especially if you just need a command occassionally. Click here to read an excellent article about npx.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文