NPM is the default package manager for Node.js. It’s been the sidekick for Node.js for the past 10 years and comes pre-bundled with it. At the time of writing, npm is the world’s largest software registry with over 800,000 packages published and used by developers. However, npm is not just a place to publish Node packages but also provides developers with a feature-rich command line tool to install, manage and update from its hundreds and thousands of Node packages in its registry.
In this article, we’ll be differentiating between two npm commands namely npm install and npm update, and learn how they are helpful in developing Node.js applications.
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.
npm install command without parameter
If we use the npm install command without any parameter at the root of your Node project, npm downloads and installs all our mentioned dependencies in the package.json file. While installing npm creates a node_modules folder if not already present in the root of our project and stores all the files related to our dependencies inside it.
In the following example, we have created a Node project and have the following package.json file. We must note that we do not have any files or folders inside our node_modules folder.
Now go to the root of your project and run the following command in the terminal to install the dependencies:
npm install command with parameter
npm install can accept parameters such as a package name, save type option, global flag, etc. On specifying a package name, npm tries to locate whether the package is already present in package.json and installed in the directory and then downloads and installs it from the npm registry if it’s not present in package.json while creating 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.
npm install [-g] <package_name>
npm install [--save-type] <package_name>
Learn more about the different –save options available for npm install command here.
In this example, we’ll use the same Node project and install a new package from the npm registry in our project. You may take reference of the package.json file from the above example.
Now run the command
npm install --save-dev eslint
Our eslint package gets installed successfully along with 88 more packages on which eslint is dependent. Secondly, as we mentioned eslint would be a dev dependency using the –save-dev option, eslint is referenced inside the devDependencies object in package.json.
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.
npm update command 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. npm update command 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. It must be noted that globally installed packages are treated as if they are installed with a caret Semver range specified. To update to the latest version npm update needs to be used with parameters, that is, the package name must be specified.
npm update [-g]
Use the command npm list -g to get the list of all globally installed packages. Here I have deliberately installed some older versions of modules such as the ipfs node module. You can follow along by installing an older version of ipfs using the command npm install -g [email protected]
Now run the command
npm update -g
After the update is done, again the following command to get a list of the globally installed packages along with their version number
npm list -g
Take a closer look at the version number of the ipfs package. The version number has changed.
npm update command 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.
npm update [-g] <package_name[@version]>
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.
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 following same command to check the local version of the typescript module.
Due to Semver caret constraint, typescript updates only up to version 4.9.5 and is not able to get the latest version
In this article, we discussed the npm install and the npm update commands using some examples and also categorized the outputs for both commands based on the parameter we pass. Hope you find this article useful and helped you better understand the difference between the two npm commands.
Wondering what to learn next in npm? Learn how to uninstall npm packages in depth here.