4

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.

  • I forgot to say, the machine I am planning on installing this on is running XUbuntu 14.04. (also I have my macbook running mavericks - but this is just an addition)
Community
  • 1
  • 1
wenincode
  • 376
  • 5
  • 10
  • 20
  • Do you wish to have Node installed in root owned location, but not use sudo to add modules? – alandarev Jul 28 '14 at 08:26
  • I suppose that's what I am looking for. You see I am relatively new to using node, but I hear it's best to have it installed globally but at the same time it's bad to use sudo when using it. So I guess what I am looking for is as you mentioned @alandarev – wenincode Jul 28 '14 at 08:29

1 Answers1

5

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.

alandarev
  • 8,349
  • 2
  • 34
  • 43
  • Thanks for the insight - this is what I thought was the case. Everywhere I looked suggested overriding the permissions in root directories and that just seemed like the wrong approach to me. What is wrong with installing node in your home directory? Thanks for the clear and concise answer to my above question. – wenincode Jul 28 '14 at 14:22
  • 1
    Nothing is wrong if it is development setup, and you do not want any other user to have access to your node.js setup. If it is production server then best practice would be to restrict write access to server executables from the user running your node.js application. – alandarev Jul 28 '14 at 14:44