What are the different types of dependencies in Node.js ?
Last Updated :
12 Feb, 2022
NPM (Node Package Manager) is a package manager for the Node JavaScript platform. It consists of an npm registry that enables Open-Source developers to publish and share their code. You might need to install a few packages to streamline your project. Packages contain code written by other developers. Our root project has a package.json file that keeps track of all the packages we've installed. This file contains information about the project and defines the attributes that npm uses for installing dependencies, running scripts, and identifying the entry point for the package.
The different types of dependencies are:
- Production Dependencies
- Development Dependencies
- Peer Dependencies
- Optional Dependencies
- Bundled Dependencies
Create Package json file: Run the following command from the root directory of the project to create the package.json file.
npm init
Note: Npm init prompts you for information about your project such as package name, version, test command, git repository, keywords, author and license.
Creating a package.json file Adding Package: Following is the syntax to install the package:
npm install <package_name>
Example: Run the following command to install express package:
npm install express
Installing Express (Express is a node.js framework)Output: After installing the package, our package.json file will look like this.
package.json file Besides the details you entered about the project, you'll notice the dependencies section. Dependencies are libraries on which the project relies to function effectively.
1. PRODUCTION DEPENDENCIES:
Production dependencies are fundamental dependencies that are required to complete the project. Package.json specifies these dependencies under the key "dependencies". These are the packages used in the project code. If a package doesn't already exist in the node_modules directory, then it is automatically added. These are the libraries you need when you run your code. For example, To run a react project, you need react-dom. In our production dependencies, you will find the express package we just installed.
2. DEVELOPMENT DEPENDENCIES:
As a developer, you may want to install dependencies to build and test your website. The packages required by a developer during development are called devDependencies. Dependencies that you might need at some point in the development process, but not during execution. They are not included in the production version and will not be downloaded to end user's browser caches. For example, Nodemon, lodash, Babel etc.
Note: Nodemon helps developers create node.js applications by automatically restarting the node application when a file change is detected in the directory.
Adding dev dependency: By using the following command, let's add nodemon as a dev dependency:
Syntax: npm install <package_name> --save-dev
Example: npm install nodemon --save-dev
Installing Nodemon Output: Let's look at our package.json file. The package.json file now includes the nodemon package under dev dependencies.
package.json file after installing nodemon As you install a package, npm will automatically install the package's dependencies and dev dependencies. In addition to these dependencies, we also have peer dependencies.
3. PEER DEPENDENCIES:
The only time you would encounter peer dependencies is if you were publishing your own package ie, when you develop a code that will be used by other programs. Libraries use peerDependencies to tell developers which other libraries with the exact version they will need to install in their own websites to use your library. Therefore, peer dependencies also express compatibility. The peerDependencies ensure that the code is compatible with the version of the package installed. peerDependencies are not automatically installed. You need to manually modify your package.json file in order to add a Peer Dependency.
For packages like react, this will ensure that there is a single copy of react-dom which is also used by the person installing the package. Even though the package relies on React, it has no direct dependency. Things such as React and React-dom are required, but not installed.
4. OPTIONAL DEPENDENCIES:
As the name implies, optional dependencies are those that won't cause a failure during the installation of the dependencies for an application or project because npm will ignore them if they fail. Regardless of whether these dependencies are present or not, the application will still run just fine. Adding a dependency as optional can speed up the installation process for Node projects. However, not all dependencies can really be optional.
Adding dependency as optional: We can use the following command to make a dependency optional:
npm i package_name --save-optional
Example: For instance, we will install the chalk package as an optional dependency. Using the chalk library, we can easily enforce colors and styles on our terminal output.
npm i chalk --save-optional
Installing chalk You will find your package under the "optionalDependencies" key in package.json.
Chalk package under optionalDependencies 5. BUNDLED DEPENDENCIES:
When publishing the package, these dependencies are bundled with it. NPM packages are preserved locally or can be downloaded from a single file. For example, express, request packages can be bundled.
BundledDependencies are listed as an array, without versions. Bundled Dependencies have the same functionality as normal dependencies. When normal dependencies are insufficient, bundled dependencies can be useful. They are rarely used.
Similar Reads
What is the difference between StrongNode and Node.js ?
StrongLoop Node is a packaged distribution of Node.js, NPM, and the slc. The slc is a command-line utility and a set of supported npm modules that comes with StrongLoop Node for building and managing applications. Some tools and modules that come with the StrongLoop Node are Express, Connect, Passpo
2 min read
Differentiate between worker threads and clusters in Node JS.
In this article, we will learn about worker threads & clusters, along with discussing the significant distinction that differentiates between worker threads & clusters. Table of Content What is Worker Thread in Node.JS?What is clusters in Node.JS?Difference between Worker threads and Cluster
4 min read
What are Optional Dependencies and when should we use them ?
Optional dependencies in Node.js offer a flexible way to enhance the functionality of a package or module by providing additional features or optimizations without being mandatory for its core functionality. In this article, we'll delve into what optional dependencies are, how they work, and when to
6 min read
What are the Key Features of Node.js ?
Node.js has gained immense popularity among developers for its ability to handle server-side operations efficiently and effectively. Built on Chrome's V8 JavaScript engine, Node.js is designed to build scalable and high-performance applications. Here, we explore the key features that make Node.js a
5 min read
Types of API functions in Node.js
Node.js, known for its asynchronous and event-driven architecture, is a popular choice for building scalable server-side applications. One of its primary uses is to create and manage APIs (Application Programming Interfaces). APIs allow different software systems to communicate and share data with e
6 min read
What is the use of next() function in Express.js ?
Express.js is a powerful framework for node.js. One of the main advantages of this framework is defining different routes or middleware to handle the client's different incoming requests. In this article, we will discuss, the use of the next() function in every middleware of the express.js.There are
2 min read
Difference between npm i and npm ci in Node.js
npm i or npm install is used to install, modify, and update the dependencies in a project along with updating the dependencies in package-lock.json while npm ci only reinstalls all the packages mentioned in the package-lock.json with the specified versions and can't modify the lock packages.Let's di
2 min read
The Pros and Cons of Node.JS in Web Development
Node.js is a platform that allows developers to build highly scalable applications using JavaScript. It has become the most popular language for developing real-time apps, especially in the enterprise space where it can handle thousands of simultaneous connections while maintaining low latency and h
10 min read
Difference between Node.js and React.js
Node.js and React.js are two powerful technologies widely used in the development of modern web applications. While both are based on JavaScript, they serve entirely different purposes and are used at different stages of the web development process. This article provides a detailed comparison betwee
5 min read
What are the differences between HTTP module and Express.js module ?
HTTP and Express both are used in NodeJS for development. In this article, we'll go through HTTP and express modules separatelyHTTP: It is an in-build module which is pre-installed along with NodeJS. It is used to create server and set up connections. Using this connection, data sending and receivin
2 min read