dependencies vs. devDependencies
A rule of thumb for installing your next NPM package
If you’ve spent much time in a Node.js project, you’ve probably encountered a distinction in how external packages can be installed:
npm install @11ty/eleventy
dependenciesnpm install --save-dev @11ty/eleventy
devDependenciesBut when do you use which? Like just about everything else in our little trade, it depends. Here’s how I think about it.
dependencies
Anything the project requires to run or behave as intended. For a website, this might include:
- Frameworks
- Libraries that simplify complex or difficult requirements, either client-side or server-side
- External assets & stylesheets
Rule of thumb 👍 — “If this package didn't get installed, would the project still work in a production environment?”
devDependencies
Dependencies you need while developing, but that aren’t used by the project itself:
- Testing tools; libraries, runners, etc.
- Linters & formatters
- 3rd-party type definitions
Rule of thumb 👍 — “Is this package used only for ensuring code quality and/or improving the process of development?”
Why does it matter?
Distinguishing between dependencies required for functionality vs. those that provide development-only benefits allows you to install packages with precision.
Both of these commands will omit devDependencies when installing packages:
npm install --production
NODE_ENV=production npm install
In an automated build environment (like GitHub Actions or any other CI pipeline), configuring your build to skip devDependencies will reduce your package install times (sometimes considerably!) and avoid burning through lots of resources unnecessarily.
For example, Storybook currently adds over 35MB to the overall dependency pile 😳 That’s somewhat less of an issue in a development environment where dependencies are usually installed infrequently, but it’s wholly unnecessary in a production build of a project in which it only plays a supporting role.
By installing development-only packages using --save-dev, you can ensure they’re only installed when it’s appropriate.
Does it always matter?
Nope! There are also times the distinction doesn’t matter at all. If:
- You don’t use a CI pipeline to deploy your project
- Your project’s development environment is also its production deployment environment
you can safely forget everything you just read ☺️