npm install vs. npm update difference

While both npm install and npm update are used to manage a Node.js project’s packages and dependencies, they have different objectives:

npm install

A project’s new packages and their dependencies are installed using the npm install command. When you issue this command, npm inspects the package.json file for the project and installs the packages and versions stated there. By providing the names and versions of particular packages as arguments, this command can also be used to install a specified set of them. For instance, running npm install lodash@4.18.1 will set up the package’s 4.18.1 version.

npm update

A project’s packages and dependencies are updated to the most recent versions using npm update. When you issue this command, npm examines the packages that are already installed in the project and determines whether there are any updated versions. Npm will update them to the most recent version if there are any. By providing the names of certain packages as arguments, you can also use this command to update particular packages. For instance, npm update lodash will update the package to the most recent release.

When you are creating a new project and wish to install packages for the first time, npm install is more frequently utilized.

It’s important to remember that package.json’s version can also be updated with npm update.
Using npm update frequently to keep your project’s dependencies current is a good idea because new versions of packages can come with bug fixes, security upgrades, or new features. It’s crucial to test your application after executing npm update because updating packages could also break your code if they contain breaking changes.

When you wish to update packages that are already installed in your project, you use npm update.

It’s crucial to remember that while npm update changes the installed packages to the most recent versions, npm install installs the packages and versions specified in your package.json file.

Here is an illustration of how to use the package.json file’s matching code along with the npm update command in a project.

You want to make sure that all of the dependencies for a project that was started some time ago remain current. To perform the npm update command, navigate to the project’s root directory.

npm update

This command would verify that every package that is presently installed in the project is up to date.

Example of package .json:

{
  "name": "your-project",
  "dependencies": {
    "lodash": "^4.15.1",
    "moment": "^2.28.1",
  }
}

Let’s imagine you wish to upgrade the “lodash” package for an existing project to the most recent version. The npm update lodash command should be entered in the project’s root directory.

npm update lodash

This command would examine the project’s current installation of the “lodash” package and upgrade it to the most recent version. The updated package.json may look like this after the update:

{
  "name": "your-project",
  "dependencies": {
    "lodash": "^4.15.8",
    "moment": "^2.28.1",
  }
}

i.e it has updated the loadash version from 4.15.1 to 4.15.8 with the help of npm update command.