20

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
  • 7,480
  • 7
  • 28
  • 33

2 Answers2

28

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,439
  • 29
  • 24
  • 31
Sai Tej
  • 296
  • 4
  • 6
  • 1
    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
  • 5
    @Mark `npm install --no-save` or `npm ci`, then `npm test`, then `npm prune --production` – alice kibin Aug 07 '20 at 10:50
  • 3
    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
16

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 --also=dev

will do the trick ;)

Niket Pathak
  • 4,417
  • 1
  • 30
  • 38