0

Can someone explain to me what it means to --save-dev and how this impacts distribution and how NPM is aware of what you're trying to do?

1984
  • 297
  • 3
  • 14

2 Answers2

1

--save-dev is useful for dependencies such as unit testing libraries. These dependencies are not required by the application to run in production; therefore if you published your package, consumers of your package would not get these dev dependencies in their node_modules folder.

NPM doesn't necessarily know that your in dev mode, it's just a package manager that allows you to install packages into your working directory and publish your own package from said directory.

yeah-buddy
  • 424
  • 3
  • 6
  • What does it mean for an application being "run" in production, how does npm know you're running in production or development? – 1984 Jun 11 '16 at 16:10
  • Matthew Bakaitis's answer gave a good description of how npm knows not to install dev dependencies. You either install with the production flag `--production` or you set the environment variable, NODE_ENVIRONMENT, to "production" – yeah-buddy Jun 11 '16 at 16:59
1

First, see the answer to this question, What's the difference between dependencies, devDependencies and peerDependencies.

That will explain a TON.

Second, npm will install devDependencies by default unless one of two things is true, in which case devDependencies will be skipped. These things are:

  1. You explicitly tell npm it's production with npm install --production
  2. You set an environment variable that npm checks, NODE_ENVIRONMENT=production

In general, if you are distributing to something like Heroku, they will have the production flag set and your devDependencies will not be installed. So only install things with the --save-dev or -D flag (both do the same thing) if it is a module used for development, such as tests/mocks/scaffolding/etc.

Community
  • 1
  • 1
Matthew Bakaitis
  • 11,600
  • 7
  • 43
  • 53