Node modules have 755 permissions, what permissions should I set so that npm don't require sudo?

6,025

You don't need to alter permission of npm to be able to use it without sudo. TO set up npm to run without sudo follow these steps:

Option 1:

  1. Get path of npm directory:

    npm config get prefix
    
  2. If you got /usr go to option 2 else change owner of npm directories to yours:

     sudo chown -R $(whoami) $(npm config get prefix)/{lib/node_modules,bin,share}
    

Option 2:

  1. Create directory for global installations:

    mkdir ~/.npm-global
    
  2. Configure to use that directory:

    npm config set prefix '~/.npm-global'
    
  3. Open or create ~/.profile and add this:

    export PATH=~/.npm-global/bin:$PATH
    
  4. Add this also to your ~/.bashrc file (optional) :

    export PATH=/home/username/.npm-global/bin:$PATH
    export NODE_PATH=/usr/lib/nodejs:/usr/lib/node_modules:/usr/share/javascript:/home/username/.npm-global/lib/node_modules
    
  5. Refresh the files:

    source ~/.bashrc
    
  6. Test by running:

    npm install -g jshint
    

source:

https://docs.npmjs.com/getting-started/fixing-npm-permissions

http://www.competa.com/blog/how-to-run-npm-without-sudo/

Share:
6,025

Related videos on Youtube

Indu Pillai
Author by

Indu Pillai

Updated on September 18, 2022

Comments

  • Indu Pillai
    Indu Pillai over 1 year

    I'm using a framework which runs npm and webpack automatically, the directory /usr/lib/node_modules have 755 permissions which require to run npm through sudo. The framework I use doesn't allow adding sudo to the command.

    Now I need to install npm packages in global form (e.g., npm install webpack -g) without using sudo.

    What permissions will be OK for /usr/lib/node_modules? If I set 777 permissions will that be OK and secure?

    Thank You!

  • Indu Pillai
    Indu Pillai over 7 years
    A nice detailed answer, let me try it!
  • George Udosen
    George Udosen over 7 years
    That's how I use npm on my system.
  • Indu Pillai
    Indu Pillai over 7 years
    I'm using Zshell, so I need to add the stuff to ~/.zshrc in the step 4 ?
  • Indu Pillai
    Indu Pillai over 7 years
    And do I need to change it for Zshell, or it will work as such?
  • George Udosen
    George Udosen over 7 years
    sure that is right.
  • George Udosen
    George Udosen over 7 years
    Add it to that shell too.
  • George Udosen
    George Udosen over 7 years
    Please remember to source those files after adding.
  • Indu Pillai
    Indu Pillai over 7 years
    what do you mean by "to source those files" ?
  • Indu Pillai
    Indu Pillai over 7 years
    You meant keep the backup of the dot files ?
  • George Udosen
    George Udosen over 7 years
    Is just to refresh the file like this source ~/.bashrc or source ~/.zshrc...
  • Indu Pillai
    Indu Pillai over 7 years
    Oh. :) I restarted the terminal and it worked.
  • George Udosen
    George Udosen over 7 years
    ok same thing then cheers :).
  • Ilmari Karonen
    Ilmari Karonen over 7 years
    You really should replace "if you got /usr" with "if you got anything that isn't under your home directory". And, since anything under your home directory is almost certainly owned by you anyway, you might as well leave option 1 out entirely, and just skip straight to option 2.
  • Brady Dean
    Brady Dean over 7 years
    Should the path be set in both files?
  • Ilmari Karonen
    Ilmari Karonen over 7 years
    Also, step 4 (modifying .bashrc) in your second option should be unnecessary: your .profile will be run automatically when you first log in, and any changes to PATH made in it will be inherited by subshells, so repeating the changes in .bashrc is redundant and will just needlessly bloat your PATH with duplicate entries. (You probably do want to add something like export NODE_PATH=~/.npm-global/lib/node_modules:$NODE_PATH to .profile, though.)
  • George Udosen
    George Udosen over 7 years
    @ilmariKaronen yes step 4 is not needed, updated answer to reflect that.
  • George Udosen
    George Udosen over 7 years
    @BradyDean no it should not, updated OP to reflect that.