3

This is probably a silly question, but I can't find a straight answer anywhere, so I rather ask it than continue to have this doubt.

I'm reading about setting Node environment. The answer to this question says we shouldn't set NODE_ENV from within a node application itself.

So, what I understand is we set through the OS, like this:

- linux & mac: export NODE_ENV=development

- windows: set NODE_ENV=development

But when we deploy it, say to Heroku or AWS, and we want to change it to production, where do we make that change? Is it automatic (the servers on Heroku or AWS already have it set to production?)

As I said, probably silly question. But documentations seems to be written for people who already know how to use it.

james keller
  • 99
  • 2
  • 7

1 Answers1

2

It depends on what your deploy mechanism looks like. If you run an ec2 instance on aws it's just a dumb machine and knows nothing about how to deploy code.

As a concrete example, maybe you're running Docker and have a build step where you create your docker images. Then in your respective Dockerfile you can set NODE_ENV to production.

rtn
  • 127,556
  • 20
  • 111
  • 121
  • So, I'm right to assume that, somewhere between development stage and deployment, there must be, either by me or the deploy mechanism, something setting NODE_ENV to production? – james keller Mar 12 '18 at 16:15
  • Correct. But also note that it's nothing that you _have_ to use. It's just convention if you need to have different behavior depending on where you run your code, i.e. in development, staging or production. – rtn Mar 12 '18 at 16:39
  • Sometimes it can be useful to do e.g. `if (process.env.NODE_ENV === 'dev') { /* something related to dev env only */ }` when running things locally. Perhaps talking to a local db. – rtn Mar 12 '18 at 16:40
  • Ok, but then, the correct place to change NODE_ENV to production is gonna be provided by the documentation of the mechanism I'm deploying to? That is, Heroku will have it's own place where I should set NODE_ENV, AWS will have it's own place, etc, etc... ? And maybe some mechanisms have Production set by default...? – james keller Mar 12 '18 at 17:01
  • Some do, some don't. Hard to say in general. – rtn Mar 12 '18 at 17:03
  • 1
    Ok, that's what I needed to know. Thank you. – james keller Mar 12 '18 at 17:14