72

I'm trying to put together documentation for new developers installing our codebase on their local development environments. I'd like to give them command(s) that:

  • Installs both devDependencies and dependencies based on the versions in package-lock.json
  • Doesn't update package-lock.json

"npm ci" does almost exactly what I want, but doesn't seem to install devDependencies. "npm install" does install devDependencies, but it sometimes modifies package-lock.json.

I could imagine something janky like "npm install && git checkout package-lock.json", but I feel like there must be a more idiomatic way of saying "give me a clean install of this project's dependencies for development?"

josh
  • 9,038
  • 8
  • 31
  • 37

2 Answers2

104

npm ci does install both dependecies and dev dependencies. But if you use npm ci --production or if your NODE_ENV is set to production, then it avoids installing dev dependencies. Please check docs here.

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

NOTE: The --production flag has no particular meaning when adding a dependency to a project.

David Buck
  • 3,752
  • 35
  • 31
  • 35
Sai Tej
  • 1,064
  • 1
  • 8
  • 6
  • 2
    So how do you `npm install` then `npm test` and then remove devdependencies? If you `npm install --only=production` then you cannot run tests. – Marc Jun 09 '20 at 15:54
  • 7
    @Mark `npm install --no-save` or `npm ci`, then `npm test`, then `npm prune --production` – alice kibin Aug 07 '20 at 10:50
  • 23
    The doc you are linking to is for `npm install`. The `npm ci` page does not list **ANY** command line flags. Does `npm ci` support all the same command line flags as `npm install`? I would expect this to be clearly called out in the docs somewhere if that is the case. Otherwise there are a lot of answers about "use this flag" where people may add options to commands that ignore them. If I run `npm install --foobar`, npm doesn't warn or complain that that is an invalid option. So good chance lots of people are using CLI options that do nothing. – mattpr Mar 01 '21 at 08:38
  • 12
    For future readers, just to clarify @mattpr's comment, `npm ci --production` does work as intended. (I tested it.) Perhaps one could raise a PR to update the `npm ci` documentation. – Leponzo Sep 20 '21 at 03:47
  • 7
    Going foward, use --omit=dev instead of --production – Ε Г И І И О Oct 21 '22 at 07:10
68

Override NODE_ENV variable

When your NODE_ENV environment variable is set to production, using npm ci will not install devDependencies. But if you still want to install devDependencies

npm ci --include=dev

will do the trick ;)


For versions older than NPM v7.x, use

npm ci --also=dev
Niket Pathak
  • 6,323
  • 1
  • 39
  • 51
  • 5
    As v7 the `also` option is deprecated. Use `--include=dev` instead. https://docs.npmjs.com/cli/v7/using-npm/config#also – n370 Jun 16 '21 at 21:58