Feature request: Support for multiple production environments in package.json

(Abdullah Ahsan) #1

(Copied from this issue on GitHub.)

With serverless and other technologies becoming more mainstream, the number of production environments in which a Node application will be deployed is increasing. Different production environments may have different dependencies. Having a bloated node_modules with unnecessary dependencies on AWS Lambda, for example, is not very efficient.

Currently, in npm there is only one production environment. npm install --only=prod installs the dependencies listed in the dependencies section of package.json. You can’t specify additional production environments. There exists a workaround to install additional dependencies in certain environments using environment variables in npm scripts. It looks something like this:

"scripts": {
    "customInstall": "if [ ENV = some_env ]; then npm install --save additional_dependency; fi; && npm install;"

But this is an ugly solution.

I propose that there should be a native feature in npm and package.json that allows users to define multiple production environments. Each production environment can then have its own additional dependencies (listed under its own section in package.json) which will also get installed with the base production dependencies (listed under dependencies) when npm install --only=<prod environment name> is run.

A simple dev and deployment workflow could look like this:

  1. During dev, install dependencies as usual.
npm install --save <shared_dependency>
  1. Add a dependency only for prod1 environment.
npm install --save-prod prod1 <prod1_dependency>

Which would make the package.json look something like this:

"dependencies": {
    "<shared_dependency>": "1.0.0"
  "devDependencies": {
    "<dev_dependency>": "1.0.0"
  "prod1Dependencies": {
    "<prod1_dependency>": "1.0.0"
  1. Deploy on a production environment where additional dependency is not required:
npm install --only-prod
  1. Deploy on prod1 environment where prod1_dependency is also required in addition to shared_dependency:
npm install --only-prod1

I dont think this feature will introduce a breaking change. But its inclusion in npm and package.json will make deployments on different production environments very simple and natural.