A dependency is a piece of code (a library, a module, or a package) that a project needs to function correctly.
In Node.js, dependencies and devDependencies are categories of packages that you can define in your package.json file to manage your project’s dependencies. Discover their differences and learn how to use them in your projects.
Regular dependencies in an npm project
Regular dependencies, or simply dependencies, are packages that your application needs to work as intended at runtime and in production environments.
Dependencies can be:
- External libraries that provide out-of-the-box functionality, such as lodash.
- Frameworks that provide a foundation or structure for building applications, such as Express or React.
- Database drivers, such as MongoDB or sqlite3.
- Packages that help with network-related tasks, such as Axios or Socket.io.
For example, if you are building a CRUD API with Express.js, emphatically will be one of your dependencies because your server needs it to function.
To install the runtime dependencies, you can use a package manager such as npm, the Node Package Manager, or Yarn.
There are dependencies under the dependencies object in a package.json file. The dependencies object stores the names and versions of the packages.
Run the command below to install a package as a dependency using npm:
npm install <package-name>
The above command installs the package and registers it as a dependency in your project package.json:
When you clone a project from a web-based repository, you can install the dependencies by running the command below:
When you run the command above, your package manager will read it package.json file and installs the specified dependencies and devDependencies from the package registry.
Development dependencies and how to use them
Development dependencies, or devDepenendencies, are packages that your application only needs during the development process. In other words, they are not needed in production.
devDependencies can be:
- Test frameworks, such as Jest or Mocha.
- Test runners, like Chai.
- Linters and formatters, such as Eslint.
- Documentation tools, such as Swagger.
A common example of a devDependency is the Nodemon package, which reboots your server when it detects changes to your scripts.
You can define devDependencies under the devDependencies object in one package.json file. This object stores the names and versions of the packages.
You can install a package as a devDependency by running this command:
npm install <package-name> --save-dev
You can also install a package as a devDependency by running this command:
npm install -D <package-name>
The above command installs and registers the package in your package.json file, like so:
When you clone a project from a web-based repository, you run it install npm command will also devDependencies.
However, running the command with the production flag only installs the dependencies.
npm install --production
When you run the command above, your package manager will read it package.json file and installs your application’s dependencies from the package registry.
Alternatively, you can use the NODE_ENV variable to “production” to achieve the same results.
Other types of dependencies
In addition to dependencies and devDependencies, Node.js also categorizes packages as peerDepenecies And optionaldependencies.
Dependencies on colleagues
Peer dependencies are dependencies that a package uses when it expects a specific version or a compatible version of another package to be present in the application it uses.
The purpose of peer dependencies is to ensure that a package works correctly with its dependencies and to avoid conflicts or compatibility issues.
For npm versions prior to v7, peer dependencies are not installed automatically. Rather, the code containing the package should contain it as a dependency. If the package doesn’t contain it, npm issues a warning.
They exist under the peerDependencies object in the package.json file:
Optional dependencies are packages that are not essential to a project’s core functionality, but can enhance its capabilities when available.
Since optional dependencies are not required for the project to work correctly, you can omit them during installation.
Running the command below will skip optional dependencies during the installation process:
npm install --omit=optional
They exist under the optional dependencies object in the package.json file:
Categories make it easier to manage your dependencies
Effectively understanding, managing, and categorizing dependencies ensures a clear separation between the packages needed for production and those needed only during development.
This distinction helps efficient dependency management, proper bundling, and streamlined implementation of your project.