wenincode
wenincode

Reputation: 396

Best Approach to installing Node.js/npm without sudo

I've been looking around for the best/most appropriate way to install node.js/npm in such a way that using commands like npm install -g bower does not require sudo, as using sudo for such a command can cause issues later on. Initially I followed this answer: Installing with nvm but this installs it into the users home directory which I read may not be a good a idea in production to have node installed in your home directory so I followed an expansion on above tutorial with this: Installing with NVM (digital ocean) however this left me still requiring sudo.

On a side note - on my macbook I installed node with homebrew, is this a good idea or is there a more standard approach.

Thanks for all your help, feel free to ask for clarifications.

Upvotes: 4

Views: 3810

Answers (1)

alandarev
alandarev

Reputation: 8635

Sudo gives you permissions to change/add/remove files not owned by your user. Those files are as a rule everything except /home/YOU (in MacOS: /Users/YOU)

Your desire is to have Node installed as appropriate (system wide, rather than your home directory), that is good. And as you guessed you need sudo to initially install it on a system path.

But then you wish to have modules installed without sudo, meaning you want modules to be located in a directory, where your user has write access to. That would be available by default if Node was installed in your home.

To enforce your wish on a system path, you will need to give write permission to the folder where modules are located, that is change write permissions or ownership of:

  1. /usr/local/share/npm/lib/node_modules, so that modules can be saved on your disk.
  2. /usr/local/share/npm/bin, to allow modules executables be reachable.

You might have to alter few other folders as well.


That answers your question, but I strongly recommend you not doing so. Instead I suggest you stick to default methodologies. Everyone here without doubt will say it is absolutely safe approach to use sudo when you are installing modules globally, it is even safer to not have write permissions to global infrastructure of your install without super privileges.

Upvotes: 5

Related Questions