5

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!

2
10

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/

17
  • A nice detailed answer, let me try it! Jan 7 '17 at 19:54
  • 1
    That's how I use npm on my system. Jan 7 '17 at 19:56
  • I'm using Zshell, so I need to add the stuff to ~/.zshrc in the step 4 ? Jan 7 '17 at 19:57
  • And do I need to change it for Zshell, or it will work as such? Jan 7 '17 at 19:57
  • 1
    ok same thing then cheers :). Jan 7 '17 at 20:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.