Difference Between npm install and npm update in Node.js

NPM (Node Package Manager) is the default package manager for Node.js. It has been the sidekick for Node.js for over a decade and comes pre-bundled with it. Click here to read a detailed guide on NPM.

While NPM provides many useful commands, the most useful ones are npm install and npm update. In this article, we will learn about these two commands, their usage, and the difference between them.

Installing NPM

Generally, npm is preinstalled when we installed node. But if you want to install or update npm to the latest version, here are some quick steps:

1. By Installing Latest Node.js

We can download the latest Node.js version from the official website to get the npm latest version. After installation, we can check the installed node version and npm version using the following commands:

node -v
npm -v

2. By Installing NPM Separately

We can also manually update npm by running the command:

npm install npm@latest

This will update npm to the latest version available.

npm install Command

npm install command performs the most basic operation of the npm CLI, i.e. as its name suggests it helps in installing third-party node modules. This feature has a slight variance depending on whether this command is used with or without any parameter.

1. Without Parameter

If we use the npm install command without any parameter at the root of your Node project, npm will download and install the specified version of our mentioned dependencies in the package.json file. This will also create a node_modules folder if not already present at the root of our project and store all the files related to our dependencies inside it.

Syntax:

npm install

Example:

In this example, we created a Node project with a package.json file. We must note that we do not have any files or folders inside our node_modules folder.

Npm Install Without Parameter Example

Now go to the root of your project and run the following command in the terminal to install the dependencies:

npm install
Npm Install Without Parameter Output

2. With Parameter

npm install accepts parameters such as a package name, –save option, flags, etc.

  • On specifying a package name, it will download and install its latest version from the npm registry and create a reference to that package in the package.json file.
  • On using the -g flag with the package name, npm downloads and installs the mentioned package globally on the system.

Syntax:

npm install packageName

Example:

npm install --save-dev eslint
Npm Install With Parameter Output

The eslint package gets installed successfully along with 88 more packages on which eslint is dependent. Also, eslint would be a dev dependency as we used the –save-dev option so it is present inside the devDependencies object.

npm update Command

npm update as the name suggests helps in updating different node modules or packages inside the system or Node project. Similar to the npm install command, the npm update command also accepts different flags, options, and parameters to customize the command according to our needs.

1. Without Parameter

Simply using npm update at the root of any Node project allows npm to check the package.json file and update the dependency packages mentioned in it to the next latest version following the Semver constraints. It also downloads and installs all the missing packages in the project.

On using the -g flag, npm updates all globally installed packages in the system.

Syntax:

npm update -g

Example:

Use the command npm list -g to get the list of all globally installed packages.

Npm Update Without Parameter Example

Now let’s run the command:

npm update -g

After the update is done, again list all globally installed packages:

npm list -g
Npm Update Without Parameter Output

Take a closer look at the version number of the ipfs package. The version number has changed to the latest one.

2. With Parameter

Using the npm update command along with the package name inside the root of any Node project updates the mentioned package if found in the package.json file.

  • It is to be noted that the npm update command only updates the package if it is mentioned in the package.json file, the next update is available, and if the update follows the Semver constraint.
  • To update a package to a specific version unfollowing the Semver constraint, we can mention the version or add the keyword latest after the package name followed by @ symbol.

We can also update any globally installed package by using the -g flag along with the package name.

Syntax:

npm update -g packageName@version

Example:

In the following example, we have an older version of typescript installed in our project as you can see in the package.json file. At the time of writing, version 5.0.2 is the latest version.

Use the following command to get the local version of the typescript module:

node_modules\.bin\tsc -v
Npm Update With Parameter Example

Now run the following command to update the typescript module to the latest updateable version:

npm update typescript

After the update is successful use the same command to check the local version of the typescript module:

Npm Update With Parameter Output

Due to Semver caret constraint, typescript updates only up to version 4.9.5 and is not able to get the latest version.

Conclusion

In Node.js, the npm install command installs all the packages specified in the package.json file while npm update updates existing packages to their latest versions. We can also pass a package name as a parameter to install or update that specific package. We can also use the “-g” flag to do these things globally and “–save-dev” to install a package as a development dependency and that’s it, npm is that simple.

References

Devdeep Ghosh
Devdeep Ghosh
Articles: 14