Ilja Ilja - 5 months ago 27
Node.js Question

Set separate variable values for production, development, release

I am trying to figure out a way to set up different api endpoints based on application being in development, staging or production.

I've seen many developers use something line

process.env.NODE_ENV
but I tried that and I get an error saying


property env is undefined


Tried this inside my component and action, but no luck. I don't fully understand why this can be used in some places but not others. And would like to know if there are any alternative solutions to the problem?

Answer
  • First in terminal set $export NODE_ENV=dev

  • To check whether properly set $printenv NODE_ENV in terminal

  • Then in node you can access like $console.log(process.env.NODE_ENV)

  • So whenever my app wants environment variables at startup i do like this

    $NODE_ENV=dev SQL_IP=127.0.0.1 node server.js

  • Note : Environmental variables when directly set through terminal they wipe out on closing terminal , so please set these variables in .bashrc file in your machine permanently