Dependencies and DevDependencies in Node.js
In Node.js, dependencies are external packages used by an application to perform its functionality. NPM helps developers install and manage these packages through the package.json file.
NPM provides two important categories of packages:
dependencies and devDependencies.
Understanding the difference helps developers organize projects,
install the required packages, and prepare applications for deployment.
1. What Is a Dependency?
A dependency is a package that an application relies on to perform a particular task. Instead of writing every feature from scratch, developers can install existing packages and use their functionality.
For example:
- Express helps build web servers and REST APIs.
- Mongoose helps applications work with MongoDB.
- Axios provides an API for making HTTP requests.
When an application requires a package while running, that
package is normally recorded under dependencies.
Application features and business logic
Packages required by the application
The application uses installed packages
2. What Is a DevDependency?
A development dependency is a package mainly used while developing, testing, formatting, or building an application. These tools are generally not needed by the application to perform its normal production functionality.
Common examples include:
- Nodemon — restarts a Node.js application when watched files change.
- ESLint — checks JavaScript code for problems and style issues.
- Prettier — formats source code.
- Jest — provides tools for automated testing.
These packages are normally recorded under
devDependencies in package.json.
Writes and tests application code
Testing, linting, formatting, and development tools
Helps improve code quality and productivity
3. Difference Between Dependencies and DevDependencies
| Dependencies | DevDependencies |
|---|---|
| Packages required by the application during normal runtime. | Packages mainly used during development and testing. |
Declared under dependencies. |
Declared under devDependencies. |
| Example: Express in a web server. | Example: Nodemon for development. |
Installed using npm install package-name. |
Installed using npm install package-name --save-dev. |
| Usually required in a production installation. | Can be omitted when installing only production dependencies. |
4. Install a Dependency
To install a package as a normal dependency, use the
npm install command.
Syntax
npm install package-name
Example: Install Express
npm install express
This command normally:
- Downloads Express and its required dependencies.
- Creates or updates the
node_modulesfolder. - Adds Express to
dependenciesin package.json. - Updates package-lock.json.
Example package.json
{
"name": "studentapp",
"version": "1.0.0",
"dependencies": {
"express": "^4.21.0"
}
}
The Express version shown here is illustrative. NPM records the version range corresponding to the package version installed.
5. Install a DevDependency
To install a package mainly used for development, use the
--save-dev option.
Syntax
npm install package-name --save-dev
Example: Install Nodemon
npm install nodemon --save-dev
Nodemon monitors specified files and restarts a Node.js process when relevant changes are detected. It is commonly used during development.
Example package.json
{
"name": "studentapp",
"version": "1.0.0",
"devDependencies": {
"nodemon": "^3.0.0"
}
}
The version above is an example. Your installed version may differ.
6. Understand Both Properties in One Project
A project can have both dependencies and devDependencies. Each category is stored separately in package.json.
Install both packages
npm install express
npm install nodemon --save-dev
Resulting package.json structure
{
"name": "studentapp",
"version": "1.0.0",
"dependencies": {
"express": "^4.21.0"
},
"devDependencies": {
"nodemon": "^3.0.0"
}
}
Your actual package.json may contain additional fields such as scripts and the exact package versions installed by NPM.
7. Practical Example: Express with Nodemon
Let's create a small application that uses Express as a dependency and Nodemon as a development tool.
Step 1: Create the project
mkdir StudentApp
cd StudentApp
npm init -y
Step 2: Install the packages
npm install express
npm install nodemon --save-dev
Step 3: Create app.js
const express = require("express");
const app = express();
app.get("/", (req, res) => {
res.send("Welcome to StudentApp!");
});
app.get("/students", (req, res) => {
res.json([
{ id: 1, name: "Samadhan" },
{ id: 2, name: "Pradnya" }
]);
});
app.listen(3000, () => {
console.log("StudentApp running on port 3000");
});
Step 4: Add scripts in package.json
Keep the dependencies installed by NPM and add the following
entries inside the existing scripts property:
"scripts": {
"start": "node app.js",
"dev": "nodemon app.js"
}
If a scripts property already exists, replace or update its contents rather than adding a second scripts property.
Step 5: Run in development mode
npm run dev
Expected terminal message:
StudentApp running on port 3000
Nodemon keeps the application running and restarts it when watched files change. The terminal may also show additional Nodemon messages.
Step 6: Run normally
npm start
This executes node app.js directly without Nodemon.
Expected API responses
GET request to the root route:
Welcome to StudentApp!
GET request to the students route:
[
{
"id": 1,
"name": "Samadhan"
},
{
"id": 2,
"name": "Pradnya"
}
]
8. Install Only Production Dependencies
When deploying an application, development tools may not be required. NPM supports installing only production dependencies.
npm install --omit=dev
This command omits packages declared in
devDependencies from the installation.
Packages declared in dependencies remain included.
9. How NPM Manages Package Installation
npm install express
Declare the dependency category
Record resolved package versions
Store installed packages in node_modules
10. Common Mistakes
| Mistake | Why It Is a Problem | Correct Approach |
|---|---|---|
| Installing Nodemon as a production dependency without a reason | It is usually a development tool. | Use --save-dev when appropriate. |
| Forgetting to install Express | The application cannot resolve the required package. | Run npm install express. |
| Writing two scripts properties | Duplicate JSON property names can cause confusing configuration. | Keep one scripts object and add all commands inside it. |
| Deleting package-lock.json without a reason | Dependency resolution may change when reinstalling. | Keep the lockfile under version control for applications. |
| Assuming devDependencies are never used in production workflows | Build tools may be required during deployment. | Understand the project's build and deployment process. |
11. Practice Tasks
- Create a project named EmployeeApp.
- Install Express as a dependency.
- Install Nodemon as a devDependency.
- Check package.json and identify both categories.
- Create a simple Express route that returns employee data.
- Add start and dev scripts.
- Run the application using
npm start. - Run the application using
npm run dev.
Summary
Dependencies are packages normally required by an application at runtime, while devDependencies are mainly used for development, testing, formatting, and build tasks. The npm install command installs regular dependencies, and npm install package-name --save-dev installs development dependencies. NPM records these categories in package.json and tracks resolved versions in package-lock.json. Understanding both categories helps developers manage packages, run applications, and prepare Node.js projects for deployment at CIIT TRAINING INSTITUTE.