677

I have this in my package.json file (shortened version):

{
  "name": "a-module",
  "version": "0.0.1",
  "dependencies": {
    "coffee-script":      ">= 1.1.3"
  },
  "devDependencies": {
    "stylus":             ">= 0.17.0"
  }
}

I am using NPM version 1.1.1 on Mac 10.6.8.

When I run the following command from the project root, it installs both the dependencies and devDependencies:

npm install

I was under the impression that this command installed the devDependencies:

npm install --dev

How do I make it so npm install only installs dependencies (so production environment only gets those modules), while something like npm install --dev installs both dependencies and devDependencies?

Goblinlord
  • 2,760
  • 1
  • 17
  • 23
Lance Pollard
  • 66,757
  • 77
  • 237
  • 416
  • 1
    According to the doc, you are right, the --dev installs the devdeps, otherwise, only the deps. http://npmjs.org/doc/json.html. I know this works for named packages, at least. – mna Feb 13 '12 at 22:54
  • 2
    if you come across this Question in 2015, --save-dev switch does the trick exactly as asked in this Question. – Anand Mar 25 '15 at 08:38
  • 1
    This answer http://stackoverflow.com/a/22004559/3718119 gives a nice explanation to differences between `dependencies` and `devDependencies` and when each of them is used. – quasoft Sep 11 '15 at 12:02

16 Answers16

1002

The npm install command will install the devDependencies along other dependencies when run inside a package directory, in a development environment (the default).

Use npm install --only=prod (or --only=production) to install only dependencies, and not devDependencies,regardless of the value of the NODE_ENV environment variable.

Source: npm docs

Note: Before v3.3.0 of npm (2015-08-13), the option was called --production, i.e. npm install --production.

Peterino
  • 10,324
  • 2
  • 25
  • 26
Rohan Singh
  • 16,731
  • 1
  • 37
  • 46
  • So if we publish one module. Do we need to remove the devDependencies from package.json? – agate Jan 15 '13 at 07:13
  • 3
    Agate's reasoning is probably to remove devDependencies so that consumers installing your package with `npm install yourpackage.tgz` don't also get the devDependencies. However, that is already the case. See Kevin Cox's answer below (http://stackoverflow.com/a/15826602/825588). – Johann Aug 27 '13 at 23:51
  • 5
    https://www.npmjs.org/doc/cli/npm-install.html "By default, npm install will install all modules listed as dependencies. With the --production flag, npm will not install modules listed in devDependencies." – tomByrer Sep 29 '14 at 14:49
  • 17
    Oh my god. I totally had `NODE_ENV=production` from something else I was doing and could not figure out for the life of me why `npm install` wouldn't install dependencies. Thanks for the thorough answer. – aendrew May 05 '15 at 23:29
  • 1
    `npm install --dev` would only install dev dependencies – Rustem K Jun 12 '15 at 05:25
  • 11
    For npm 3.3 and onward: npm WARN install Usage of the `--dev` option is deprecated. Use `--only=dev` instead. – srcspider Nov 02 '15 at 09:22
  • 1
    The problem with this "solution" is that it treats any non-"production" environment as "development". It shouldn't. What if I have a "staging" environment where I only want production deps? – Adam Reis Dec 17 '16 at 19:31
  • As of npm 6.9.0, https://docs.npmjs.com/cli/install clearly documents BOTH `--production` and `--only={prod[uction]|dev[elopment]}`. – Dan Dascalescu May 01 '19 at 07:23
  • 1
    According to the current [documentation](https://docs.npmjs.com/cli/install) it is still only `--production`: "With the `--production` flag (or when the NODE_ENV environment variable is set to production), npm will not install modules listed in devDependencies." – João Pimentel Ferreira May 16 '20 at 10:48
  • 1
    @JoãoPimentelFerreira: did you also notice the "The `--only={prod[uction]|dev[elopment]}` argument will cause either only `devDependencies` or only `non-devDependencies` to be installed regardless of the NODE_ENV." part? – Dan Dascalescu Jul 27 '20 at 03:59
  • --production flag is still there. – Čamo Feb 03 '21 at 14:05
256

I run into that problem too! npm install is somewhat confusing and web posts keep bringing in the -d/--dev flags as if there is an explicit 'development' install mode.

  • npm install will install both "dependencies" and "devDependencies"

  • npm install --production will only install "dependencies"

  • npm install --dev will only install "devDependencies"

Liam
  • 22,818
  • 25
  • 93
  • 157
smertrios
  • 2,837
  • 1
  • 12
  • 9
  • 36
    WARNING: If `NODE_ENV` is set to `production` and you run `npm install` it will not install dev dependencies. I ran into this issue in my Dockerfile. – vaughan Jan 28 '16 at 23:46
  • 4
    @vaughan I've run into this too, can be solved by running `npm --production=false install` (though not sure if this will work alongside the --dev flag) – Bavell Mar 16 '16 at 16:21
151

The new option is:

npm install --only=prod

If you want to install only devDependencies:

npm install --only=dev
user1614572
  • 153
  • 1
  • 7
Cloxure
  • 1,669
  • 1
  • 14
  • 15
  • 1
    This answers the opposite of the question. OP is asking how NOT to install devDependencies. – musicin3d Nov 07 '16 at 00:01
  • 3
    you are partially correct @musicin3d, that's why on the first part I answered how to install prod only dependencies and on the second part how to install dev only dependencies, just in case. – Cloxure Dec 27 '16 at 19:06
  • 5
    Hey there. I left that comment before @user1614572 added the part about `--only=prod`. You, sir, are partially correct. ;P – musicin3d Dec 28 '16 at 20:11
  • can we include separate dependency for the deployment? – Jeeva J Dec 31 '18 at 10:51
  • @Jacques: why is this the correct answer as of 2015//2016? As opposed to...? https://docs.npmjs.com/cli/install clearly documents BOTH `--production` and `--only={prod[uction]|dev[elopment]}`. – Dan Dascalescu May 01 '19 at 07:22
  • @DanDascalescu you’re asking me a question about a comment from 3 years ago while linking to *todays* documentation? – Jacques ジャック May 01 '19 at 09:44
  • @Jacques: here: http://web.archive.org/web/20160115001135/https://docs.npmjs.com/cli/install. Better? – Dan Dascalescu May 02 '19 at 01:10
  • @DanDascalescu You missed the point. It was posted in 2016, I had no way at the time to know if things would change in the future. Why are you commenting on stuff from years ago that brings nothing new or valuable to the answers/comments? – Jacques ジャック May 02 '19 at 01:20
  • @Jacques: I didn't miss the point, but let me clarify: [at the time you commented, the documentation](http://web.archive.org/web/20160115001135/https://docs.npmjs.com/cli/install) showed both options, with no indication as to which would be preferred or new. What made you claim "This is the correct answer"? Furthermore, both options are still equally valid today, and if you care about integrity, you'd delete your 2016 comment, and/or add a new one if you do have reasons to say that `--production` is deprecated. SO isn't a set-in-time blog post. It's an evolving wiki. Let's keep it current! – Dan Dascalescu May 02 '19 at 01:24
  • 1
    It’s a comment, not an answer. I don’t go checking all my comments periodically. I’ve never seen people go update *comments* from years ago. You basically came on here to criticize that I put a year on my comment. I won’t be responding further. Have fun trolling other people. – Jacques ジャック May 02 '19 at 02:32
46

If you read this POST in 2016, please achieve what you want by using

--only={prod[uction]|dev[elopment]} 

argument will cause either only devDependencies or only non-devDependencies to be installed regardless of the NODE_ENV.

from: https://docs.npmjs.com/cli/install

wzr1337
  • 3,099
  • 4
  • 25
  • 46
  • 1
    Why capitalize "POST"? https://docs.npmjs.com/cli/install clearly documents BOTH `--production` and `--only={prod[uction]|dev[elopment]}`. – Dan Dascalescu May 01 '19 at 07:21
45

If you have already installed all your dependencies, and you want to avoid having to download your production packages from NPM again, you can simply type:

npm prune --production

This will remove your dev dependencies from your node_modules folder, which is helpful if you're trying to automate a two step process like

  1. Webpack my project, using dev dependencies
  2. Build a Docker image using only production modules

Running npm prune in between will save you from having to reinstall everything!

piercebot
  • 1,638
  • 14
  • 16
  • 2
    In 2021, npm v7.11 you just need `npm install --only=production` even if you installed the whole thing and it achieves the same result – santiago arizti May 11 '21 at 22:26
21

When using "npm install" the modules are loaded and available throughout your application regardless of if they are "devDependencies" or "dependencies". Sum of this idea: everything which your package.json defines as a dependency (any type) gets installed to node_modules.

The purpose for the difference between dependencies/devDependencies/optionalDependencies is what consumers of your code can do w/ npm to install these resources.

Per the documentation: https://npmjs.org/doc/json.html...

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 list these additional items in a devDependencies hash.

These things will be installed whenever the --dev configuration flag is set. This flag is set automatically when doing npm link or when doing npm install from the root of a package, and can be managed like any other npm configuration param. See config(1) for more on the topic.

However, to resolve this question, if you want to ONLY install the "dependencies" using npm, the following command is:

npm install --production

This can be confirmed by looking at the Git commit which added this filter (along with some other filters [listed below] to provide this functionality).

Alternative filters which can be used by npm:

--save          => updates dependencies entries in the {{{json}}} file
--force         => force fetching remote entries if they exist on disk 
--force-latest  => force latest version on conflict
--production    => do NOT install project devDependencies
--no-color      => do not print colors

@dmarr try using npm install --production

creatovisguru
  • 281
  • 2
  • 5
16

npm will install dev dependencies when installing from inside a package (if there is a package.json in the current directory). If it is from another location (npm registry, git repo, different location on the filesystem) it only installs the dependencies.

Kevin Cox
  • 2,709
  • 1
  • 26
  • 30
  • Could you please reformulate? I think you made a mistake in your parenthesis, I think you forgot to close it earlier and your frase doesn't make much sense to me (non-native english speaker). Johann's comment under Rohan Singh answer pointed me to look to your answer (because my doubt was exactly the same that agate's doubt), and no success. I still don't get it how `npm install some-module` will not install dev dependencies of `some-module`. – Rafael Eyng Feb 21 '15 at 00:26
  • Thanks, fixed. I don't understand the rest of your question however. – Kevin Cox Feb 21 '15 at 13:18
8

I suggest to use npm ci. If you want to install only production-needed packages (as you wrote - without devDependencies) then:

npm ci --only=production

or

NODE_ENV=production npm ci

If you prefer oldschool npm install then:

npm install --production

or

NODE_ENV=production npm install

Here is good answer why you should use npm ci.

marverix
  • 5,984
  • 6
  • 31
  • 46
4

Use npm install packageName --save this will add package in dependencies, if you use npm install packageName --save-dev then it devDependencies.

npm install packageName --save-dev should be used for adding packages for development purpose. Like adding TDD packages (Chai, mocha, etc). Which are used in development and not in production.

Sandip Nirmal
  • 1,760
  • 14
  • 22
  • The OP is not having an issue saving dependencies in `package.json` but *once these dependencies are already saved*, then how to coerce `npm` to install only one specific type of dependency. When I read your answer here, I did not learn anything that helps solve the problem posed by the OP. – Louis Nov 28 '16 at 13:59
3

I have found that, when trying to install dev dependencies for a package that contains a node addon, you cannot avoid building the addon when running npm install --dev even if you just want to install the devDependencies. So, I had to go around npm's back:

node -e 'console.log( Object.keys( require( "./package.json" ).devDependencies ) );' | \
sed  -e "s/^[^']*'//" -e "s/'.*$//" | \
xargs npm install

Or, better (and more succinctly) yet,

node -e 'Object.keys( require( "./package.json" ).devDependencies )
.map( function( item ){ console.log( item ) } );' | xargs npm install
3

It's worth mentioning that you can use the NODE_ENV environment variable to achieve the same result. Particularly useful if you're containerizing your Node application (e.g. Docker).

NODE_ENV=production npm install

The above code will install all your dependencies but the dev ones (i.e. devDependencies).

if you need to use environment variables in your Dockerfile more information can be found here.

Environment variables are easy to overwrite whenever needed (e.g. if you want to run your test suite say on Travis CI). If that were the case you could do something like this:

docker run -v $(pwd):/usr/src/app --rm -it -e NODE_ENV=production node:8 npm install

NPM Documentation here

production

  • Default: false
  • Type: Boolean Set to true to run in "production" mode.

    1. devDependencies are not installed at the topmost level when running local npm install without any arguments.
    2. Set the NODE_ENV="production" for lifecycle scripts.

Happy containerization =)

Francesco Casula
  • 22,369
  • 11
  • 121
  • 128
2

Need to add to chosen answer: As of now, npm install in a package directory (containing package.json) will install devDependencies, whereas npm install -g will not install them.

ack
  • 6,488
  • 2
  • 22
  • 19
  • 8
    Yes, `-g` will not install `devDependencies`, but it does install the packages globally, which is often not the intended result. – Bardi Harborow Jul 31 '14 at 02:15
1

I ran into a problem in the docker node:current-slim (running npm 7.0.9) where npm install appeared to ignore --production, --only=prod and --only=production. I found two work-arounds:

  1. use ci instead (RUN npm ci --only=production) which requires an up-to-date package-lock.json
  2. before npm install, brutally edit the package.json with:

RUN node -e 'const fs = require("fs"); const pkg = JSON.parse(fs.readFileSync("./package.json", "utf-8")); delete pkg.devDependencies; fs.writeFileSync("./package.json", JSON.stringify(pkg), "utf-8");'

This won't edit your working package.json, just the one copied to the docker container. Of course, this shouldn't be necessary, but if it is (as it was for me), there's your hack.

ericP
  • 1,130
  • 12
  • 17
0

npm install --production is the right way of installing node modules which are required for production. Check the documentation for more details

Rubin bhandari
  • 1,321
  • 10
  • 16
-1

Now there is a problem, if you have package-lock.json with npm 5+. You have to remove it before use of npm install --production.

-1

npm install --production --no-optional

It installs only deps from dependencies and will ignore optionalDependencies and devDependencies