top of page

npm install <optional>package name</optional>

​

This command is somewhat deceptive. You might think it is used to install a package. Well, it only sort of does that. We are used to thinking of something that is "installed" as being available from anywhere, at least for your user. That is not what this is for. It installs a package "locally". You might think "locally" means only for your user. You would be wrong. In this case, "locally" refers to the current directory. More specifically, it installs stuff in the node_modules directory of the current directory.

​

"I sort of understand, but how do you use it, and what would using it actually do for specific cases?" Let's make that "sort of" even deeper. Basically, there are two scenarios where you want to use this command. You can use it to add a dependency to the project you are working on (the current directory), or you can use it to retrieve the dependencies for the project you are working on, two very different things. Personally, I don't normally use it to add dependencies. I prefer to add them to the package.json file manually, and then install them all or any missing ones by running "npm install".

​

"Ok, I understand the concept now, but how do you actually use it?" If you don't provide any parameters, this will assume the current directory is a node project and it will attempt to install any missing dependencies in the local node_modules directory. If you do provide a package to install, it will add that project as a dependency for the current directory by downloading it into the node_modules directory of the current directory. Here are some examples:

​

npm install

npm install express

​

npm install <optional>package name</optional> -g

​

This is just like npm install without the -g (read about that above), except instead of installing things into the node_modules directory of the current directory, it installs them into your some user specific path that should make it part of your PATH. This means that for your user, you should then be able to use that package anywhere. This might be a good option if you are using npm as a way of installing a program. This does not install things for all users, just for your user.

npm commands

bottom of page