npm throws error without sudo

前端 未结 30 1880
清酒与你
清酒与你 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   # 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

提交回复
热议问题