npm throws error without sudo

前端 未结 30 1918
清酒与你
清酒与你 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:18

    ISSUE: You (the user) don't have the right set of permissions for the directory.

    The instant way out is to run the npm install using sudo, but this may give you the same error, or improper installation.

    AND changing directory ownership is not a good option, a temporary patch.


    Solution/Suggestion: Change npm's Default Directory (from official docs)

    Back-up your computer before moving forward.

    (optional) In case you have a erroneous installation, first uninstall it:

    npm uninstall <package-name>  # use sudo if you used it while installation
    npm cache verify  # or, npm cache clean for npm version below 5.x.x 
    
    1. Make a directory for global installations:

      mkdir ~/.npm-global

    2. Configure npm to use the new directory path:

      npm config set prefix '~/.npm-global'

    3. Open or create a ~/.profile or ~/.bash_profile file and add this line:

      export PATH=~/.npm-global/bin:$PATH

    4. Back on the command line, update your system variables, or restart the terminal:

      source ~/.profile

    5. (optional) Test: Download a package globally without using sudo.

      npm install -g jshint

    0 讨论(0)
  • 2020-11-21 08:19

    I Solve it by changing the owner from root to my user-name

    sudo chown -R me:me /home/me/.config/configstore/

    change me with your user-name and group .

    0 讨论(0)
  • I like to use ubuntu groups to achieve this instead of changing owner. Its quite simple.

    1. First install nodejs and npm using apt-get

      sudo apt-get update && sudo apt-get install nodejs npm

    2. Figure out who is logged in i.e username, run following command to see it in terminal

      whoami

    3. You can see the list of groups you are assigned by using a very simple command, normally the first group is your username itself

      groups

    4. Run following to allow access to logged in user

      sudo chmod 777 -R /usr/local && sudo chgrp $(whoami) -R /usr/local

    5. Update npm and nodejs

      npm install -g npm

    You are allset, your user can run npm commands without sudo

    You can also refer to this link https://askubuntu.com/a/1115373/687804

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-21 08:23

    For Mac (adopted from Christoper Will's answer)

    Mac OS X 10.9.4

    1. System Preference > Users & Groups > (unlock) > press + :

      New Account > "Group"
      Account Name : nodegrp

      After creating the group, tick the user to be included in this group

    2. sudo chgrp -R nodegrp /usr/local/lib/node_modules/
      sudo chgrp nodegrp /usr/bin/node
      sudo chgrp nodegrp /usr/bin/npm
      sudo chown -R $(whoami):nodegrp ~/.npm

    0 讨论(0)
  • 2020-11-21 08:23

    Problem: You do not have permission to write to the directories that npm uses to store global packages and commands.

    Solution: Allow permission for npm.

    Open a terminal:

    command + spacebar then type 'terminal'

    Enter this command:

    sudo chown -R $(whoami) $(npm config get prefix)/{lib/node_modules,bin,share}
    
    • Note: this will require your password.

    This solution allows permission to ONLY the directories needed, keeping the other directories nice and safe.

    0 讨论(0)
提交回复
热议问题