1

When should I use sudo npm install and when to use npm install? What are the pros and cons (if any)?

StackOverflow and Nitin have pointed out that its a possible duplicate of the npm install vs sudo npm install -g, but if you see the questions is emphasising on the usage of -g but I'm looking for details about usage of npm install with and without sudo.

I hope the question is valid. Please correct me if its not.

Furqan Rahamath
  • 2,034
  • 1
  • 19
  • 29
  • Possible duplicate of [npm install vs sudo npm install -g](https://stackoverflow.com/questions/20264157/npm-install-vs-sudo-npm-install-g) – Nitin Bisht May 03 '18 at 07:31
  • Nitin I see that the conversation there is mostly revolving around usage of -g (global flag). But thats not my question about. – Furqan Rahamath May 03 '18 at 07:34
  • https://medium.com/@ExplosionPills/dont-use-sudo-with-npm-still-66e609f5f92 discusses why `sudo` should never be used with `npm`. It addresses the security risk, and the problem of the forced install not belonging to the user. – Jason Aller Sep 07 '19 at 03:21

1 Answers1

2

You use "sudo" when you need admin privileges.

Alex Delgado
  • 984
  • 11
  • 19
  • Thanks for the quick response Alex :) I have the basic idea that its for admin privileges, but, given a situation where npm install works without sudo, I was wondering if its going to cause issues if you use sudo (?) I wanted to understand it in detail – Furqan Rahamath May 03 '18 at 06:57
  • 1
    it all depends on your current user... if it has enough privileges to handle system folders or files... normally, when you do npm install, the folder you are installing npm dependencies it´s the folder of your project and your user will have enough privileges so you dont need sudo. – Alex Delgado May 03 '18 at 07:07
  • 1
    lets say I'm installing a bunch of packages with npm install, and lets say I created the folder in desktop in which I will install. Also, any user can use it. In that case, is it likely that there will we one or more packages which need some permissions from the admin user before installing? Can that happen? – Furqan Rahamath May 03 '18 at 07:30
  • 1
    in case you need to install CLI packages yes as these are installed in a global folder in the OS directories. – Alex Delgado May 04 '18 at 06:21
  • Thanks Alex. That was helpful. – Furqan Rahamath May 04 '18 at 06:43
  • If my answer was helpful, please, accept my answer as the valid one! :D – Alex Delgado Nov 14 '19 at 08:23