308

I've just started using Grunt.js. It is pretty hard to set up and I am at the point of creating a package.json file.

Following this tutorial, it says there are 3 ways to create a package.json file.

The first is to do npm install grunt --save-dev

But what does --save-dev means? I tried looking but it ends in vain.

SteveC
  • 15,808
  • 23
  • 102
  • 173
Vennsoh
  • 4,853
  • 5
  • 26
  • 41

8 Answers8

341

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

According to the npm install docs.

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 other words, when you run npm install, your project's devDependencies will be installed, but the devDependencies for any packages that your app depends on will not be installed; further, other apps having your app as a dependency need not install your devDependencies. Such modules should only be needed when developing the app (eg grunt, mocha etc).

According to the package.json docs

Edit: Attempt at visualising what npm install does:

  • yourproject
    • dependency installed
      • dependency installed
        • dependency installed
        • devDependency NOT installed
      • devDependency NOT installed
    • devDependency installed
      • dependency installed
      • devDependency NOT installed
Sam
  • 1,384
  • 2
  • 20
  • 30
Andreas Hultgren
  • 14,763
  • 4
  • 44
  • 48
  • 2
    by default it wont install with dev dependencies but how i install with then? ```npm install --dev``` flag? – StanleyZheng Sep 16 '14 at 18:34
  • 6
    @stanzheng I'm not sure what you need to know. If you're in a project and run `npm install`, the project's devDependencies will also be installed. You don't want a dependency's devDependencies installed. If you want to develop on an npm package, you'd `git clone xxx` that project, and run `npm install` in it. – Andreas Hultgren Sep 16 '14 at 18:38
  • 5
    "devDependencies for your app's dependencies will not be installed when you run npm install" is incorrect. If I have a package.json and it only contains "devDependencies" with my npm packages, and I then type "npm install," those "devDependecies" get installed. – core Jan 28 '15 at 23:20
  • @core that's exactly what i mean. You devDependencies gets installed. The devDependencies of the installed packages does not in turn get installed. I agree it's not completely clearly worded though; maybe you could suggest a better way to phrase it? – Andreas Hultgren Jan 29 '15 at 09:10
  • @core I added a visualisation; does that help? – Andreas Hultgren Jan 29 '15 at 09:35
  • 9
    I tried to understand and I didn't. Then I tried to understand the visualization and I didn't. What does each bullet-point item mean? A folder? What the strikethrough item mean? A non-existing directory? If that is true, how can you list exhaustively *non-existing* things? – Rafael Eyng Feb 21 '15 at 00:31
  • 1
    @RafaelEyng It's a tree of the dependencies in your project (package.json). The **bold** shows that any devDependency that _you_ have specified in package.json are installed. The strikethrough shows that any devDependency that the packages in your package.json in turn have specified in their own package.json are not installed. – Andreas Hultgren Feb 21 '15 at 09:40
  • @AndreasHultgren From what you said, seems that npm installs recursively the dependencies going only 1 level down when is devDependencies (is that right)? – Rafael Eyng Mar 04 '15 at 13:01
  • 2
    @RafaelEyng yes, only one level of devDependencies are installed. – Andreas Hultgren Mar 04 '15 at 14:04
  • 10
    To clarify, if `npm install` is run in your project, all devDependencies in package.json will be installed, but if you package your project as a new npm package and publish it, when somebody else installs your package with npm install, the devDependencies will not be installed on their system. Is that right? @stanzheng or you can run npm install --production to install everything in package.json except the dev dependencies – Reese Jun 04 '15 at 19:26
111

There are (at least) two types of package dependencies you can indicate in your package.json files:

  1. Those packages that are required in order to use your module are listed under the "dependencies" property. Using npm you can add those dependencies to your package.json file this way:

    npm install --save packageName
    
  2. Those packages required in order to help develop your module are listed under the "devDependencies" property. These packages are not necessary for others to use the module, but if they want to help develop the module, these packages will be needed. Using npm you can add those devDependencies to your package.json file this way:

    npm install --save-dev packageName
    
Sumner Evans
  • 8,951
  • 5
  • 30
  • 47
Lonnie Best
  • 9,936
  • 10
  • 57
  • 97
36

To add on to Andreas' answer, you can install only the dependencies by using:

npm install --production
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
machinehead115
  • 1,647
  • 10
  • 20
14

When you use the parameter "--save" your dependency will go inside the #1 below in package.json. When you use the parameter "--save-dev" your dependency will go inside the #2 below in package.json.

#1. "dependencies": these packages are required by your application in production.

#2. "devDependencies": these packages are only needed for development and testing

Sathesh
  • 6,323
  • 6
  • 36
  • 48
7

Documentation from npm for npm install <package-name> --save and npm install <package-name> --save-dev can be found here:

https://docs.npmjs.com/getting-started/using-a-package.json#the-save-and-save-dev-install-flags

A package.json file declares metadata about the module you are developing. Both aforementioned commands modify this package.json file. --save will declare the installed package (in this case, grunt) as a dependency for your module; --save-dev will declare it as a dependency for development of your module.

Ask yourself: will the installed package be required for use of my module, or will it only be required for developing it?

Niko
  • 658
  • 8
  • 14
5

For me the first answer appears a bit confusing, so to make it short and clean:

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

npm install <package_name> --no-save Prevents saving to dependencies.

npm install <package_name> ---save-dev updates the devDependencies in your package. These are only used for local testing and development.

You can read more at in the dcu

Anna Klein
  • 1,906
  • 4
  • 27
  • 56
2

Use only when developing

  • --save-dev means omit in production environments, use only in development environments (smaller, and probably faster).
BenKoshy
  • 33,477
  • 14
  • 111
  • 80
2

–save

The package installed is core dependency.

–save-dev

The package installed is not a core rather development dependency.