Beginner Guide to npm dependency types with examples - Dependencies vs devDependencies vs peerDependencies in Nodejs

Define dependency in nodejs application.

dependency  types in nodejs application.

Ā In Nodejs projects, npm is the default package manager to manage the dependencies. In every application development on any language, dependencies are required to build and start running the application. Every application has a dependency tree that contains all direct and indirect dependencies. Dependency is a module or library code that is required to execute the application. each dependency is also called a package, which can be declared in a package with each object contains the name and version.
Dependency types
In nodejs applications, dependencies are defined in package.json Following are dependencies types using npm

  1. normal dependenciesĀ 
  2. devDependenciesĀ 
  3. peerDependenciesĀ 
  4. optionalDependenciesĀ 
  5. bundledDependencies

Dependencies can be installed in 2 ways

add dependency object in the dependency type section in package.json and npm install

Another way is using npm install dependency

package.json contains all required dependencies as follows

  "name": "DepedencyTypes",  
  "version": "1.0.0",   
  "dependencies": {  
    "packageone": "^1.1.0"  
  "devDependencies": {  
    "packagetwo": "^1.4.0"  
  "peerDependencies": {  
    "packagethree": "^1.2.1"  
  "optionalDependencies": {  
    "packagefive": "^1.7.0"  
    "packagesix": "^1.7.0"  

Every application has code that runs in production code and also code related to bundling and unit testing mani application in-development code.

normal dependencies of the production

Ā These are dependencies configured and required to execute the application smoothly at run time. These also called production dependencies. This can be installed using npm install command. These will use and install a dependency tree using transitive dependencies,

one --> two--> three  

if the application installs ā€œoneā€ dependency, two and three also installed.

An example is Angular or React dependencies. This can be included in the dependencies section of package.json. npm install command do the installation of dependencies and devDependencies. To install only dependencies, not development dependencies, use below command

npm install --production  

This can also save using npm install library ā€“save-prod

Development dependencies.Ā 

Ā These are required to build and test applications during the development stage. These are called development dependencies as this is not required at production runtime. Devdependent section contains all development dependencies in package.json ALl your bundlers like webpack, bower, grunt and unit testing libraries like jasmine, karma, protector, linter library are dev dependencies devDependencies are installed using npm install command, which also installs dependencies. To install development dependencies

npm install --dev  

This can also be saved and do the installation using npm install library ā€“save-dev
Ā These installations are not transitive dependencies

one --> two--> --three>  

if the application installs ā€œoneā€ dependency, two and three also not installed.

Peerdependencies in the package.json

Ā This is also production dependencies but used in a different context to publish libraries. For example, you are writing your own UI library like material using Angular/CDK library, which means your library depends on Angular CDK. During publishing your library, the angular/CDK library must be declared as peer dependencies. so that whoever using your library must include exact angular CDK dependency. These will be included in peerDependencies section of package.json.

Ā Optional dependenciesĀ 

optionalDependencies are optional dependencies if you declared optionalDependencies in your application, application not able to install this, the result shows successfully. This will be useful when dependencies are dependent on the environment, these will result in failure installation on some of the environment.
Ā optionalDependencies is a section that has to declare in package.json.
bundled and normal dependencies look for remote npm registry and install it whereas bundled dependencies will not look for remote npm registry, instead, it looks dependencies inside an application. This will be created using npm pack or yarn pack command which creates giz of an application. This will be useful when you are simultaneously working on a library and an application for debugging the code issues, will zip a library using npm pack and declare this dependency in the application as peerDepedencies.

Similar Posts