npm throws error without sudo

前端 未结 30 1992
清酒与你
清酒与你 2020-11-21 07:43

I just installed node and npm through the package on nodejs.org and whenever I try to search or install something with npm it throws the following error, unless I sudo the c

30条回答
  •  感情败类
    2020-11-21 08:22

    As if we need more answers here, but anyway..

    Sindre Sorus has a guide Install npm packages globally without sudo on OS X and Linux outlining how to cleanly install without messing with permissions:

    Here is a way to install packages globally for a given user.

    1. Create a directory for your global packages

      mkdir "${HOME}/.npm-packages"
      
    2. Reference this directory for future usage in your .bashrc/.zshrc:

      NPM_PACKAGES="${HOME}/.npm-packages"
      
    3. Indicate to npm where to store your globally installed package. In your $HOME/.npmrc file add:

      prefix=${HOME}/.npm-packages
      
    4. Ensure node will find them. Add the following to your .bashrc/.zshrc:

      NODE_PATH="$NPM_PACKAGES/lib/node_modules:$NODE_PATH"
      
    5. Ensure you'll find installed binaries and man pages. Add the following to your .bashrc/.zshrc:

      PATH="$NPM_PACKAGES/bin:$PATH"
      # Unset manpath so we can inherit from /etc/manpath via the `manpath`
      # command
      unset MANPATH # delete if you already modified MANPATH elsewhere in your config
      MANPATH="$NPM_PACKAGES/share/man:$(manpath)"
      

    Check out npm-g_nosudo for doing the above steps automagically

    Checkout the source of this guide for the latest updates.

提交回复
热议问题