Passing environment variables in npm-scripts
<p>I have a package.json with following (simplified) content in the scripts key:</p>

<pre><code>...
scripts: {
   "start": "NODE_ENV=${NODE_ENV:=production} node start-app.js",
   "poststart": "echo $NODE_ENV"
}
...
</code></pre>

<p>From the command line I can run:</p>

<pre><code>npm start
</code></pre>

<p>This will run my start-app.js script and set the process.env.NODE_ENV  environment variable to "production". See <a href="https://stackoverflow.com/a/2013589/228450">here</a> for syntax explanation.</p>

<p>The poststart will automatically run after start as described <a href="https://docs.npmjs.com/misc/scripts" rel="noreferrer" title="here">here</a>.</p>

<p>However poststart will not "inherit" the NODE_ENV shell environment variable, so the echo command will not echo anything.</p>

<p>My producation code is a little more complex, but what I am trying to accomplish is passing down the NODE_ENV variable from the "starting point" to dependent scripts. Any suggestions/best practices on how to do that?</p>

<p>I dont want to hardcode the NODE_ENV in the poststart, because I might want to do:</p>

<pre><code>NODE_ENV=development npm start
</code></pre>

<p>and I want everyting "down the chain" inherit the same environment.</p>