Sunday, October 9, 2022

[SOLVED] What is the difference between npm install and sudo npm install?

Issue

I am new to linux. I am trying to install typescript so I executed this command: npm i typescript The command completed without errors but when I typed tsc --version to verify the installation, I got the error -bash: /usr/bin/tsc: No such file or directory.

I then tried to install typescript again but this time using the command sudo npm i -g typescript. The command completed successfully again but this time tsc --version worked as expected and returned the typescript compiler version number.

My question is:

  • What is the difference between npm i typescript and sudo npm i -g typescript?
  • Did the first command actually install anything? If yes, where? and why then did tsc --version fail?
  • What happens with sudo and without sudo? when should I use sudo and when not?

Thanks!


Solution

The -g flag causes npm to install a module globally instead of in the current project. It isn't recommended. You should probably use npx instead.

Did the first command actually install anything? If yes, where? and why then did tsc --version fail?

In the node_modules directory for your current project, which isn't in your $PATH environment variable (which is why you should use npx tsc --version instead).

sudo is a program that makes a command run as root instead of as your normal user. You might need it to install global modules if you are using a version of Node.js installed system wide (as opposed to a private one such as one managed by nvm because ordinary users don't have permission to write to the system-wide collection of installed modules.



Answered By - Quentin
Answer Checked By - David Marino (WPSolving Volunteer)