Conditional NPM scripts based on environment
Using this method, we can run a different set of commands for the same script based on the environment variable NODE_ENV
. NODE_ENV
is often set automatically by the hosting environment, such as Vercel, Netlify, or AWS. If you are running locally, you can set the environment variable manually in your terminal.
NODE_ENV=development npm run start
package.json
{
"scripts": {
"start": "npm run start:${NODE_ENV:-development}",
"start:development": "nodemon app.js",
"start:production": "node app.js",
"start:staging": "node app_staging.js"
}
}