# Environment variable NODE\_ENV being ignored?

**URL:** <https://community.fly.io/t/environment-variable-node-env-being-ignored/7825>\
**Category:** Questions / Help\
**Created:** [October 12, 2022, 4:16pm UTC](https://community.fly.io/t/environment-variable-node-env-being-ignored/7825 "2022-10-12T16:16:16Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![lpares12](https://sea1.discourse-cdn.com/fly1/user_avatar/community.fly.io/lpares12/32/5878_2.png) [@lpares12](https://community.fly.io/u/lpares12)\
**Post date:** [October 12, 2022, 4:16pm UTC](https://community.fly.io/t/environment-variable-node-env-being-ignored/7825/1 "2022-10-12T16:16:16Z")

</div>

So I am trying to deploy an Node.js + express web app to [Fly.io](http://Fly.io). The website works correctly, but I just realized it never detects the NODE\_ENV variable, even if I added it to the fly.toml file:

This is the output after running `$ flyctl deploy`

```auto
[Installing Node.js distribution]
Reusing Node.js 18.9.1
[INFO] Installing toolbox
[INFO] Using npm v8.19.1 from Node
[INFO] Reusing node modules

[Warning: Skip pruning because NODE_ENV is not 'production'.]

```

And you can see how in the file it is set:

```auto
$ cat fly.toml 
# fly.toml file generated for myaccount on 2022-09-25T18:29:52+02:00

app = "myaccount-app"
kill_signal = "SIGINT"
kill_timeout = 5
processes = []

[build]
  builder = "heroku/buildpacks:20"

[env]
  NODE_ENV = "production"
  PORT = "8080"

# Some other stuff here

```

Could anyone tell me what am I doing wrong?  
Thanks!

---

<div class="post-metadata">

**Author:** ![Christian\_B](https://avatars.discourse-cdn.com/v4/letter/c/b4bc9f/32.png) [@Christian\_B](https://community.fly.io/u/Christian_B)\
**Post date:** [October 12, 2022, 6:21pm UTC](https://community.fly.io/t/environment-variable-node-env-being-ignored/7825/2 "2022-10-12T18:21:59Z")

</div>

Try checking what `flyctl logs` is responding back with and also check [Fly.io Docs: env](https://fly.io/docs/reference/configuration/#the-env-variables-section) you might need to place some sensitive information with [Fly.io Docs: secrets](https://fly.io/docs/reference/secrets) which should allow your app to detect what’s missing.

---

<div class="post-metadata">

**Author:** ![lpares12](https://sea1.discourse-cdn.com/fly1/user_avatar/community.fly.io/lpares12/32/5878_2.png) [@lpares12](https://community.fly.io/u/lpares12)\
**Post date:** [October 12, 2022, 6:57pm UTC](https://community.fly.io/t/environment-variable-node-env-being-ignored/7825/3 "2022-10-12T18:57:25Z")

</div>

There’s nothing in the logs, and I have some other env variables set in fly.toml that are correctly set within my app.

---

<div class="post-metadata">

**Author:** ![fideloper-fly](https://sea1.discourse-cdn.com/fly1/user_avatar/community.fly.io/fideloper-fly/32/3834_2.png) [@fideloper-fly](https://community.fly.io/u/fideloper-fly)\
**Post date:** [October 12, 2022, 7:08pm UTC](https://community.fly.io/t/environment-variable-node-env-being-ignored/7825/4 "2022-10-12T19:08:23Z")

</div>

Secrets and env vars aren’t automatically injected into the Docker builder during deployment (I know, that’s a “fun” fact), so you need to use some combination of the following to get those into the Docker builder.

```bash
fly deploy \
    --build-args="foo=bar" \
    --build-secrets="bar=baz" \
    -e "foo=bar"

```

Secrets in particular require a bit of extra work to use in your `Dockerfile` : [Build Secrets · Fly Docs](https://fly.io/docs/reference/build-secrets/)

---

<div class="post-metadata">

**Author:** ![ignoramous](https://avatars.discourse-cdn.com/v4/letter/i/a9adbd/32.png) [@ignoramous](https://community.fly.io/u/ignoramous)\
**Post date:** [October 13, 2022, 2:59pm UTC](https://community.fly.io/t/environment-variable-node-env-being-ignored/7825/5 "2022-10-13T14:59:57Z")

</div>

> [@fideloper-fly](#):
>
> Secrets and env vars aren’t automatically injected into the Docker builder during deployment (I know, that’s a “fun” fact)

[Fly’s more strict](https://community.fly.io/t/why-is-fly-secrets-list-not-showing-the-values/7192/2) with `secrets` (a good thing!), but if folks could set secrets for remote builders, then that should work-out nicely afa [_devex_](https://community.fly.io/t/simulating-pr-review-apps-via-github-actions/6993/4) is concerned?

That is, imagine if `fly secrets set k1=v1 k2=v2 -a <free-builder-name>` set approp env vars for remote deploys?

`fly env set k1=v1 k2=v2 -a <free-builder-name>` would also be neat.

---

<div class="post-metadata">

**Author:** ![lpares12](https://sea1.discourse-cdn.com/fly1/user_avatar/community.fly.io/lpares12/32/5878_2.png) [@lpares12](https://community.fly.io/u/lpares12)\
**Post date:** [October 13, 2022, 3:48pm UTC](https://community.fly.io/t/environment-variable-node-env-being-ignored/7825/6 "2022-10-13T15:48:45Z")

</div>

Thanks for the info! I am not an expert with deploying websites and DevOps in general. In this case then, the problem is that the docker builder is not detecting this variable, so I should inject it in the deployment with the call:  
`flyctl deploy --build-args="NODE_ENV=production"`  
Is this correct?  
Thanks

---

<div class="post-metadata">

**Author:** ![fideloper-fly](https://sea1.discourse-cdn.com/fly1/user_avatar/community.fly.io/fideloper-fly/32/3834_2.png) [@fideloper-fly](https://community.fly.io/u/fideloper-fly)\
**Post date:** [October 13, 2022, 3:59pm UTC](https://community.fly.io/t/environment-variable-node-env-being-ignored/7825/7 "2022-10-13T15:59:18Z")

</div>

Yes, mostly!

I’m not exactly sure how the buildpack operates (vs using a `Dockerfile`), I would experiment with:

```auto
fly deploy -e "NODE_ENV=production"

```

The `--build-args` flag likely requires a `Dockerfile` with an `ARG` defined in it.

---

<div class="post-metadata">

**Author:** ![lpares12](https://sea1.discourse-cdn.com/fly1/user_avatar/community.fly.io/lpares12/32/5878_2.png) [@lpares12](https://community.fly.io/u/lpares12)\
**Post date:** [October 13, 2022, 4:30pm UTC](https://community.fly.io/t/environment-variable-node-env-being-ignored/7825/8 "2022-10-13T16:30:12Z")

</div>

I see… I have tried with:

```auto
flyctl deploy -e "NODE_ENV=production"
flyctl deploy -e NODE_ENV="production"
flyctl deploy -e NODE_ENV=production

```

So finally I tried with:

```auto
flyctl deploy --build-arg NODE_ENV=production

```

And it did work (I think), I see some other warnings now, like:

```auto
[INFO] Installing node modules from ./package-lock.json
npm WARN config production Use `--omit=dev` instead.

```

But it looks like now it is pruning the dev dependencies, so I expect `NODE_ENV` to be production.

```auto
[INFO] Successfully pruned devdependencies!

```

Thank you.

EDIT: Also, a [build.args] section can be added to the fly.toml file to not have to add the variable every time manually.

---

<div class="post-metadata">

**Author:** ![ignoramous](https://avatars.discourse-cdn.com/v4/letter/i/a9adbd/32.png) [@ignoramous](https://community.fly.io/u/ignoramous)\
**Post date:** [October 14, 2022, 5:44pm UTC](https://community.fly.io/t/environment-variable-node-env-being-ignored/7825/9 "2022-10-14T17:44:23Z")

</div>

> [@lpares12](#):
>
> Also, a [build.args] section can be added to the fly.toml file to not have to add the variable every time manually.

Another way is, to set it with the `ENV` directive of the [`Dockerfile`](https://github.com/serverless-dns/serverless-dns/blob/641b5f96ae679ac1b6045d688f364e56b90a51e5/node.Dockerfile#L18).
