DEV Community

Wakeup Flower
Wakeup Flower

Posted on

When use --save-dev

The flag --save-dev is used with package managers like npm (Node Package Manager) and yarn to install a package as a development dependency.

Here's a breakdown of what that means:

Dependency: In the context of software development, a dependency is another piece of software (a library, framework, tool, etc.) that your project relies on to function correctly.

Development Dependency: These are dependencies that are only needed during the development, testing, and build phases of your project. They are typically not required for the final, production version of your application to run.

--save-dev Flag: When you run a command like npm install <package-name> --save-dev or yarn add <package-name> --dev, the package manager will:

Download and install the specified package.
Add an entry for this package to the devDependencies section of your project's package.json file.
Why separate development dependencies?

Smaller Production Builds: By marking packages as development dependencies, you ensure that they are not included when your application is built for production. This results in smaller bundles, faster deployment times, and potentially better performance for your users.
Clearer Project Scope: It clearly distinguishes between the libraries your application needs to run and the tools you use during development.
Optimized Installations: When deploying to a production environment, you can often skip installing devDependencies, which speeds up the installation process.
Reproducible Builds: Listing your development dependencies in package.json (or yarn.lock) ensures that everyone working on the project uses the same versions of development tools, leading to more consistent and reproducible builds.
Examples of typical development dependencies:

Testing Frameworks: Jest, Mocha, Jasmine
Linters and Formatters: ESLint, Prettier
Build Tools: Webpack, Parcel, Rollup
Transpilers: Babel, TypeScript (as a build-time dependency)
Type Checkers: TypeScript (as a development tool)
Mocking Libraries: Sinon.js, Mockito

You generally don't need to use --save-dev when you are installing packages that are essential for your application to run in a production environment. These are your core dependencies.

Here's a breakdown of when --save-dev is typically omitted:

Runtime Dependencies: These are the libraries and frameworks that your application directly relies on to function when it's deployed and running for users. Without these, your application would likely crash or not work correctly.

Examples:
Frameworks like React, Angular, Vue.js (the core library itself)
Backend frameworks like Express, Django, Flask, Symfony (core components)
Utility libraries like Lodash, Moment.js (if their functions are used in the production code)
Database drivers and ORMs (e.g., pg, mysql2, mongoose, sequelize, typeorm, prisma/client)

Networking libraries (e.g., axios, node-fetch)
Security libraries (e.g., bcrypt, jsonwebtoken)
Packages Required for Deployment: Sometimes, a package might not be directly used in your application's code but is necessary for the deployment process or the runtime environment on your server.

Examples (can be debated, but consider these):
Production-specific web servers (though often handled by deployment tools)
Certain polyfills that ensure compatibility in the target runtime environment.
When you use npm install or yarn add (without --save-dev or --dev):

The package manager will install the specified package and add it to the dependencies section of your package.json file. This section lists the packages that are considered necessary for your application to run in production.

Think of it this way:

dependencies (installed with npm install --save or yarn add): "What does my application need to run?"
devDependencies (installed with npm install --save-dev or yarn add --dev): "What do I need to build, test, and format my application?"

Top comments (0)