0

I want to run my node app at port 80 in ubuntu 16.04. But the moment I'm trying to run my app with npm start its throwing error Port in already in use whereas it's not in use. According to Github Reply, We need root privileges to run on ports below 1048 which is correct. So the moment I'm trying to run my app like this sudo npm install it's throwing sudo: npm: command not found

So can anyone suggest me, how to run npm start with root privileges on ubuntu. I did googling but couldn't find anything.

Raj Bhatia
  • 1,049
  • 4
  • 18
  • 37

2 Answers2

2

Both sudo and npm(including node) should be same directory. For example sudo is always available in /usr/bin/ so after installing npm and node should also go in /usr/bin/. earlier I was using NVM to install node and npm. I removed .nvm directory and referred node installation link which placed both npm and node in /usr/bin/. And then now I'm able to run npm with sudo.

Raj Bhatia
  • 1,049
  • 4
  • 18
  • 37
1

First run which npm to get the full path to npm.

Then run sudo FULL_PATH_TO_NPM start.

Or to make a one-liner:

sudo $(which npm) start
John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • I tried this "sudo /home/ubuntu/.nvm/versions/node/v8.11.4/bin/npm start" but it thrown the error like "/usr/bin/env: 'node': No such file or directory" whereas I have kept "/home/ubuntu/.nvm/versions/node/v8.11.4/" environment variables path. I can see version for both using node -v --> v8.11.4 and npm -v --> 6.4.1 – Raj Bhatia Sep 09 '18 at 07:40
  • OK so your problem is your `node` and `npm` are not in root's `$PATH`. No surprise, see here: https://unix.stackexchange.com/questions/83191/how-to-make-sudo-preserve-path - you need to set `$PATH` inside the root environment, there are various solutions there. – John Zwinck Sep 09 '18 at 10:27
  • Somehow this leads me to the solution, writing answer below. Thank you very much @John. – Raj Bhatia Sep 09 '18 at 18:18