npm: start script fails

Viewed 278

I'm developing a simple backend project with Node.js (v12.16.1) on Windows 10.

My package.json file contains the following command:

"scripts": {
    "start": "NODE_ENV=production node --experimental-modules --require dotenv/config server.mjs dotenv_config_path=config/config.env",
    ...
}

And when I type npm run start, I get

> NODE_ENV=production node --experimental-modules --require dotenv/config server.mjs dotenv_config_path=config/config.env

'NODE_ENV' is not recognized as an internal or external command,
operable program or batch file.
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! devcamper-api@1.0.0 start: `NODE_ENV=production node --experimental-modules --require dotenv/config server.mjs dotenv_config_path=config/config.env`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the devcamper-api@1.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

Why does this failure occure?

1 Answers

Windows doesn't understand this way of setting environment variables. Use cross-env package instead: https://www.npmjs.com/package/cross-env

npm install --save-dev cross-env

and then in your script:

"cross-env NODE_ENV=production ... "

Related