Skip to content
Related Articles

Related Articles

Difference between npm install and npm update in Node.js
  • Difficulty Level : Expert
  • Last Updated : 25 Jan, 2021

NPM is like a powerhouse for Node.js  that contains all the necessary modules for the smooth running of the node.js application. It gets installed on our machine when we install Node.js on our Windows, Linux or MAC OS. 

How to install Node on the machine? Refer to this article. 

NPM has 580096 registered packages. The average rate of growth of this number is 291/day that means the growth of different kind of packages increasing drastically, so we have to update our node every time on our machine ? The answer is No! NPM allows us to install third-party modules on our machine according to the need of our work. 

Another reason is predefined modules cannot be able to fulfill the need for big projects for e.g. HTTP modules cannot differentiate the multiple kinds of requests, so we have to install externally another popular module. i.e express module. 

We can access third party modules using some predefined commands provided by Node Package Manager are given below:



Initial Project Structure :

npm install command: This npm command is used for installing the third party modules in our current directory. There are two different ways to use this command:

  1. Without Parameter
  2. With Parameter
  • Without Parameter: When we use the npm command without a parameter this command automatically downloads all the dependencies that are written in the package.json file in our directory.

Package.json: create a package.json file in the directory and mention express dependencies in this file.

{
  "name": "gfg",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.17.1"
  }
}

Run npm install command:

npm i 
or
npm install

Updated Project Structure: 

package-lock.json file and node_modules created package-lock.json file contains all the necessary information of downloaded extra dependencies and node_modules folder contains all the different types of packages that installed along with our specified module in the package.json. 



  • With Parameter: We can use the npm install command by specifying the name of the third-party module that we want to install for particular work. For e.g. let’s download MongoDB module for Node.js

 Parameter: Parameter can be the name of the module that we want to install or folder name in which we want to install all third-party modules in the directory. By default, the folder is node_modules that contains all the installed modules. This folder is automatically generated when we install any external module the first time.

Syntax:

npm install [-g] [<package>..]
  • Syntax for installing module: Module will get install in the node_modules folder in the present directory.
npm install <module-name>
  • Syntax for installing any module globally: Globally installing means we can access the module without installing that module in a particular directory. For eg Nodemon module etc.
npm install -g <module-name>
  •  Syntax for change the directory path of modules: This command will change the installing path of the external modules from node_modules to <dirname> folder in the working directory.
npm install <dirname>

Explanation: After installing any new module new packages are added to the node_modules folder and dependencies are updated to the package.json file.

  1. Installing module using npm command:
npm install mongodb

package.json file:

npm update command: This npm command is used for updating the dependencies that are mention in the package.json file as well as install all the missing packages in the directory and also used for updating the current node version on the machine.This command used in two different ways:

  1. Without Parameter
  2. With Parameter
  • Without Parameter: npm update without parameter works on all globally installed packages and update all the versions of the globally packages available on our machine.

Syntax:

npm update -g

Updating nodemon module that was installed globally:

  • With parameter: npm update command takes the second parameter as a dependency name that we want to update the next version or latest version. We can also restrict the updating of the dependencies to the latest version with the help of some reserved symbols. If we install dependencies simply by mention its name, the latest patch of the dependencies will get install but it might create some problem because when we’re working on a project and want nearly equal to the current version dependence it. We cannot be able to come to install that particular dependency we will use reserved symbols to convert the updating track of the dependency

There are mainly types of dependencies used in Node.js:

    1. Caret Dependencies: When dependencies present in the package.json or package.lock.json file with ^ called Caret Symbol is called Caret Dependencies.These dependencies are updated to the latest version compatible to that version.

"dependencies": {
  "dep11": "^2.2.2"
}

This npm update command will update to 2.3.3 (consider this version exists) and 2.3.3 satisfy previous version

      2.Tilde Dependencies: npm update command will update these dependencies to the highest sorted version. These dependencies use ~ symbol.

"dependencies": {
  "dep11": "^2.2.2"
}

If we update this dependency, it will get updated to 2.2.3 version in this case.

Difference:

  • The npm install installs all modules that are listed on package.json file and their dependencies.
  • npm update updates all packages in the node_modules directory and their dependencies.
My Personal Notes arrow_drop_up
Recommended Articles
Page :