user5393552
user5393552

Reputation:

What's the difference between --save and --save-dev in npm install?

I am trying to understand, when and where to use these tags, i understand that one is for the dev-dependency and the other is for the project dependency, but when using it for live project, i don't find any differences. I am looking for a way, that will throw an error for the wrong type of installation. is there a way to do that?

Upvotes: 6

Views: 9832

Answers (3)

Abiodun_Sam
Abiodun_Sam

Reputation: 21

The difference of the command's arguments are found when you run

npm help install

This will open a webpage of a local file for you.

From the webpage, there a re numerous options available to supply to npm install that are listed and explained.

Here is part of it that answers your question from npm functionality standpoint.

Click to see the screenshot. (I'm not allowed yet to embed an images in my post)

The major part of it is pasted here below:

npm install saves any specified packages into dependencies by default. Additionally,
you can control where and how they get saved with some additional flags:

-P, --save-prod: Package will appear in your dependencies. This is the default unless -D or -O are present.

-D, --save-dev: Package will appear in your devDependencies.

-O, --save-optional: Package will appear in your optionalDependencies.

--no-save: Prevents saving to dependencies.

Upvotes: 0

eush77
eush77

Reputation: 4088

dependencies are modules your project depends on, devDependencies are modules you use to develop your project. You can read the detailed description on the npmjs website:

If someone is planning on downloading and using your module in their program, then they probably don't want or need to download and build the external test or documentation framework that you use.

In this case, it's best to map these additional items in a devDependencies object.

Examples of dependencies: request, concat-stream, object.assign, through2.

Examples of devDependencies: mocha, tape, eslint, grunt, browserify.

dependencies are always installed whenever your project is installed or initialized, they are needed for your project to function. devDependencies are only for development (test framework, task runner…) and they are installed only when someone runs npm install from the root of the project. For example, after cloning the project repository.

You can easily verify that. Suppose I have modules foo, bar, baz, and quux in the same directory. Let foo be a dependency of baz, bar be a devDependency of baz, and baz itself be a dependency of quux.

#/$ cd baz
#/baz$ cat package.json
{
  "name": "baz",
  "version": "0.0.0",
  "dependencies": {
    "foo": "../foo"
  },
  "devDependencies": {
    "bar": "../bar"
  }
}
#/baz$ npm install
[email protected] /tmp/tmpdir/g6jBr9/baz
├── [email protected]
└── [email protected]

As you can see, both dependencies and devDependencies are installed.

Now let's install baz as a dependency of quux:

#/$ cd quux
#/quux$ cat package.json
{
  "name": "quux",
  "version": "0.0.0",
  "dependencies": {
    "baz": "../baz"
  }
}
#/quux$ npm install
#/quux$ npm ls
[email protected] /tmp/tmpdir/g6jBr9/quux
└─┬ [email protected]
  └── [email protected]

Notice that foo is installed, but bar is not. This is because if you require some module as a dependency of another module (i.e. you are a consumer of that module), you don't need its devDependencies, because they are not needed for the module to function.

Upvotes: 16

Explosion Pills
Explosion Pills

Reputation: 191749

npm install will install both devDependencies and dependencies by default. You need to use a specific flag/setting to ignore devDependencies for installation. From npm help install:

With the --production flag (or when the NODE_ENV environment variable
       is set to production), npm will not install modules listed in
       devDependencies.

Obviously do not use --save-dev or add to devDependencies when you are requiring something vital that is used by the main part of the app or that will be needed for production. The devDependencies are usually only needed for things like testing and building.

To be quite honest I don't see a huge benefit to using devDependencies. Excluding them will save you minimal space and time. Using it, you run the risk of excluding a vital dependency. I'm not saying not to use it though -- just suggesting that for some projects you may not need to worry about it.

Upvotes: 3

Related Questions