We install packages to add features to our project. Some packages are needed when the project runs, others only when we build or test it.
0
0
Installing packages (dependencies vs devDependencies) in Node.js
Introduction
When you need a package to run your app in production, like a web server.
When you want tools to help write or test code, but not needed in production.
When sharing your project, so others know which packages are essential to run it.
When you want to keep your project size smaller by not including unnecessary tools in production.
Syntax
Node.js
npm install <package-name> # installs as a dependency npm install <package-name> --save-dev # installs as a devDependency
Dependencies are packages your app needs to work when running.
DevDependencies are packages only needed during development, like testing or building tools.
Examples
This installs the
express package as a dependency because your app needs it to run.Node.js
npm install express
This installs
jest as a devDependency because it is only used for testing during development.Node.js
npm install jest --save-dev
lodash is installed as a dependency by default, useful for utility functions your app uses.Node.js
npm install lodash
eslint is a tool to check code style, so it is installed as a devDependency.Node.js
npm install eslint --save-dev
Sample Program
This package.json file shows express under dependencies because it is needed to run the app. jest is under devDependencies because it is only used for testing during development.
Node.js
/* package.json example showing dependencies and devDependencies */ { "name": "my-app", "version": "1.0.0", "dependencies": { "express": "^4.18.2" }, "devDependencies": { "jest": "^29.5.0" } }
OutputSuccess
Important Notes
Use npm install without flags to add runtime packages.
Use --save-dev to add packages only needed during development.
Keeping devDependencies separate helps keep production installs smaller and faster.
Summary
Dependencies are needed to run your app.
DevDependencies are only needed during development.
Use npm install and npm install --save-dev to manage them correctly.