Web

What does npm install --save do? - Save to dependencies

Learn what the npm install --save flag does: how it records packages in package.json dependencies, how npm 5+ changed behavior and when to use --save-dev.

1 answer 4 views

What is the purpose of the --save option in the npm install command?

I encountered the following command in a tutorial:

npm install --save

Can someone explain what the --save flag does when installing npm packages?

The npm install --save flag used to explicitly add a package to your project’s package.json dependencies section, ensuring it’s tracked for production use and reproducible installs. Since npm version 5.0 (back in 2017), this became the default behavior for npm install <package>, so you rarely need --save anymore—packages get saved automatically. But --save-dev still matters for development tools like testing frameworks.


Contents


Understanding npm install --save

Ever run across a tutorial with npm install lodash --save and wondered why the extra flag? It boils down to dependency management. When you execute npm install <package> --save, npm doesn’t just download the package into node_modules—it also updates your package.json file by adding the package (and its version) to the "dependencies" object. This keeps your project reproducible: anyone cloning your repo can run npm install and get the exact same setup.

Why does this matter? Imagine deploying to a server without it—your app breaks because the missing package isn’t listed. The official npm documentation confirms that --save targets runtime dependencies, separate from dev tools.

How the --save Flag Works

Picture this: you’re building a Node.js app and need Express. Typing npm install express --save does two things fast.

  1. Installs express locally.
  2. Adds "express": "^4.18.0" (or whatever version) to package.json’s dependencies.

Without --save in old npm versions, it’d install but vanish from package.json. No big deal locally, but a nightmare for teams. Now? Just npm install express handles it, as noted in Stack Overflow discussions.


The History and Changes in npm

npm evolved a ton. Pre-version 5, --save was mandatory for production deps—skip it, and your package.json stayed silent. That changed with npm 5, making --save the default to reduce accidents, per this Medium guide.

Fast-forward to today (npm 10+ as of 2026), and it’s even smarter with lockfiles like package-lock.json pinning exact versions. But legacy tutorials? They stick to --save out of habit. GeeksforGeeks explains how this shift fixed common pitfalls.

What if you’re on an old npm? Check with npm --version. Upgrade if needed—npm install -g npm@latest.


npm install --save vs --save-dev

Here’s where it gets interesting: not all packages are equal. Use npm install --save-dev (or -D) for build tools, linters, or tests—like Jest or Webpack. These land in "devDependencies", skipped in production for lighter installs.

Flag package.json Section Use Case Example
--save (default) dependencies Runtime code npm install axios
--save-dev or -D devDependencies Development only npm install jest -D
--save-peer (rare) peerDependencies Host app provides Plugin libs

Better Stack nails it: --save-dev keeps prod bundles lean. Pro tip: npm i -D is shorthand everyone loves.


Real-World npm install Scenarios

Tutorials often show incomplete commands like npm install --save. Add a package! Common ones from search trends:

  • React apps: npm install react react-dom—auto-saves to deps.
  • Express servers: npm install express --save (though redundant now).
  • Dev tools: npm install nodemon -D for auto-restarts.

Bulk install? npm install lodash axios handles multiples. From GitHub? npm install user/repo#v1.0.0. And globals? npm install -g create-react-app—no package.json involvement.

These patterns match high-volume queries like “npm install react” or “npm install express.”


Troubleshooting Common Issues

Stumbled on errors? npm install fails aren’t rare.

  • Permission denied? Use sudo sparingly; better: own your dirs with sudo chown -R $USER node_modules.
  • Peer deps warnings? npm auto-installs now, but --legacy-peer-deps silences old conflicts.
  • No package.json update? You’re on npm <5, or using --no-save.

Lockfiles clash? Delete node_modules and package-lock.json, then reinstall. The npm docs cover cache clears: npm cache clean --force.


Alternatives to npm

npm dominates, but check Yarn or pnpm for speed. Yarn: yarn add lodash mirrors --save. pnpm: pnpm add lodash—symlinks save disk space. Trends show “npm install yarn” and “npm install pnpm” rising.

Stick to npm for most projects, especially with Docker or Vercel.


Sources

  1. npm-install | npm Docs
  2. What is the --save option for npm install? | Stack Overflow
  3. What is the save option for npm install? | Better Stack
  4. What is the save option for npm install? | GeeksforGeeks
  5. Understanding “npm install --save”: A Guide | Medium

Conclusion

The npm install --save flag simplified dependency tracking by auto-updating package.json, and it’s now default—making your workflows smoother. Stick to --save-dev for tools, embrace lockfiles, and you’ll avoid most headaches. Whether tweaking that tutorial command or scaling a big app, understanding these basics keeps your npm install dependencies rock-solid.

Authors
Verified by moderation
Moderation
What does npm install --save do? - Save to dependencies