Dependencies vs devDependencies vs peerDependencies in Nodejs
- Admin
- Dec 31, 2023
- Nodejs Javascript
Nodejs Dependency types
In Nodejs projects, npm is the default package manager to manage the dependencies. In every application development in 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, that contains the name and version declared in the package.json file You can also check other posts on npm command deprecate option is deprecated Dependency types
In nodejs applications, 1 is defined in package.json. The following are dependencies types using npm
- normal dependencies
- devDependencies
- peerDependencies
- optionalDependencies
- bundledDependencies
Dependencies can be installed in 2 ways
One way is to add a dependency object in the dependency type section in package.json and run the npm install command
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"
},
bundledDependencies{
"packagesix": "^1.7.0"
},
}
Every application has code for runs in production code and code related to bundling and unit testing an application in-development code.
normal dependencies of the production
These are dependencies configured and required to execute the application smoothly at run time. These are also called production dependencies. This can be installed using the npm install command. These install a dependency as well as tree transitive dependencies,
one-- > two-- > three;
if the application installs “one” dependency, two and three are also installed.
An example is Angular or React dependencies. It 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 the 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.
devDependencies section contains all development dependencies in the package. json.
For example, All your bundlers like webpack, bower, grunt and unit testing libraries such as jasmine, karma, protector, linter library are dev dependencies. devDependencies are installed using the 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 are also not installed.
Peerdependencies in the package.json
This is also used as production dependencies, but used in a different context to publish libraries.
For example, you are writing your UI library like material using the 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 whoever using your library must include exact angular CDK dependency.
These will be included in the peerDependencies section of package.json.
Optional dependencies
optionalDependencies is called optional dependencies if you declared dependencies in the optionalDependencies section of package.json in your application. These applications are not able to install this, but the result shows success.
This will be useful when dependencies are dependent on the environment, these will result in failure installation on some of the environments.
optionalDependencies is a section that has to declare in package.json.
bundled and normal dependencies look for a remote npm registry and install it whereas bundled dependencies will not look for a remote npm registry, instead, it looks for dependencies inside an application.
This will be created using the npm pack or yarn pack command which creates the 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.
Conclusion
To sum up, Learned different dependency types in package.json with examples.
