How to export correct object from Node.js on Windows 10 machine using NPM?

  javascript, node.js, npm, npm-scripts, windows

As far as I know and as far as it’s relevant, there is 2 ways to run project on Windows 10 machine, while passing variables:

  1. NODE_ENV=production && npm run dev
  2. npm run prod

What’s going on here?.. Why first example runs smoothly, while the second – returns undefined?..

What I get with second example is currentEnv = "production", but exportableEnvName = undefined.

Any ideas how to solve it? And why it happens? Thanks!

P.S. I would prefer in not using other packages as a solution.

Code example:

config.js

const environments = {};

environments.dev = {
    httpPort: 3000,
    httpsPort: 3001,
}

environments.production = {
    httpPort: 5000,
    httpsPort: 5001,
}

const currentEnv = typeof process.env.NODE_ENV === 'string' ? process.env.NODE_ENV.toLowerCase() : '';
const exportableEnvName = typeof environments[currentEnv] === 'object' ? currentEnv : 'dev';

module.exports = environments[exportableEnvName];

package.json

{
  "scripts": {
    "dev": "node .",
    "prod": "SET NODE_ENV=production && node ."
  },
}

Source: Windows Questions

LEAVE A COMMENT