Next.js Discord

Discord Forum

Next.js doesn't find environment variables from 13.4.13 onward in standalone mode

Answered
nyxz posted this in #help-forum
Open in Discord
Hi all!
I'm deploying Next.js app in standalone mode in AWS ECS Fargate - basically I build a Docker image and AWS takes care of it.
It worked perfectly well until a few days ago when I upgraded from Next 13.4.7 to 13.4.18 and my app started crashing. Soon it was clear all my environment variables are not there.
After some investigation I found out that all versions through 13.4.12 work fine, but 13.4.13 and above don't.
In the release notes for 13.4.13 I didn't notice anything suspicious, but I'm not that experienced in the Next.js community to know what to look for.

I use [the Dockerfile](https://github.com/vercel/next.js/blob/canary/examples/with-docker/Dockerfile) that is linked in [the Next.js docs](https://nextjs.org/docs/pages/building-your-application/deploying#docker-image).

My next.config.js:
/** @type {import('next').NextConfig} */
const nextConfig = {
  reactStrictMode: true,
  swcMinify: true,
  images: {
    remotePatterns: [
      {
        protocol: 'https',
        hostname: '**'
      }
    ]
  },
  output: 'standalone',
}
module.exports = nextConfig


As you can see I don't declare the environment variables there. I'm directly accessing them in the code with process.env.VAR_NAME.

When I run a container from the image locally and pass an environment variable to it with -e VAR_NAME=value it works fine with Next.js 13.4.12 but not with 13.4.13 and above.
docker run -it -p 3000:3000 -e VAR_NAME=value my-image:latest

I would appreciate any help or advice on how to debug this issue. I'd like to use latest Next.js in the end.

Here is next info if it helps:
Operating System:
  Platform: linux
  Arch: x64
  Version: #1 SMP PREEMPT_DYNAMIC Thu May 11 16:51:53 UTC 2023
Binaries:
  Node: 18.12.1
  npm: 8.1.3
  Yarn: 1.22.17
  pnpm: 8.6.12
Relevant Packages:
  next: 13.4.13
  eslint-config-next: 13.4.18
  react: 18.2.0
  react-dom: 18.2.0
  typescript: 5.1.6
Next.js Config:
  output: standalone

17 Replies

European sprat
known issue, fixed in canary
or WILL BE fixed in canary when they cut a release for that
European sprat
Answer
Oh wow! I don't know how did I miss that.
Thank you @European sprat 🙇‍♂️
@nyxz Hi all! I'm deploying Next.js app in *standalone mode* in AWS ECS Fargate - basically I build a Docker image and AWS takes care of it. It worked perfectly well until a few days ago when I upgraded from Next `13.4.7` to `13.4.18` and my app started crashing. Soon it was clear all my environment variables are not there. After some investigation I found out that all versions through `13.4.12` work fine, but `13.4.13` and above don't. In the release notes for `13.4.13` I didn't notice anything suspicious, but I'm not that experienced in the Next.js community to know what to look for. I use [the Dockerfile](https://github.com/vercel/next.js/blob/canary/examples/with-docker/Dockerfile) that is linked in [the Next.js docs](https://nextjs.org/docs/pages/building-your-application/deploying#docker-image). My `next.config.js`: js /** @type {import('next').NextConfig} */ const nextConfig = { reactStrictMode: true, swcMinify: true, images: { remotePatterns: [ { protocol: 'https', hostname: '**' } ] }, output: 'standalone', } module.exports = nextConfig As you can see I don't declare the environment variables there. I'm directly accessing them in the code with `process.env.VAR_NAME`. When I run a container from the image locally and pass an environment variable to it with `-e VAR_NAME=value` it works fine with Next.js `13.4.12` but not with `13.4.13` and above. sh docker run -it -p 3000:3000 -e VAR_NAME=value my-image:latest I would appreciate any help or advice on how to debug this issue. I'd like to use latest Next.js in the end. Here is `next info` if it helps: yml Operating System: Platform: linux Arch: x64 Version: #1 SMP PREEMPT_DYNAMIC Thu May 11 16:51:53 UTC 2023 Binaries: Node: 18.12.1 npm: 8.1.3 Yarn: 1.22.17 pnpm: 8.6.12 Relevant Packages: next: 13.4.13 eslint-config-next: 13.4.18 react: 18.2.0 react-dom: 18.2.0 typescript: 5.1.6 Next.js Config: output: standalone
Red-tailed wasp
We ran into the same problem. I got next to reproduce the issue we see in k8s locally when using a non-localhost HOSTNAME e.g. the HOSTNAME assigned by k8s/docker. We no set HOSTNAME to 0.0.0.0 which solves the issue for now.
Red-tailed wasp
Oh sorry you are right.
European sprat
no need to apologize i was just confirming with you mostly :2747_PartyParrot:
@nyxz Hi all! I'm deploying Next.js app in *standalone mode* in AWS ECS Fargate - basically I build a Docker image and AWS takes care of it. It worked perfectly well until a few days ago when I upgraded from Next `13.4.7` to `13.4.18` and my app started crashing. Soon it was clear all my environment variables are not there. After some investigation I found out that all versions through `13.4.12` work fine, but `13.4.13` and above don't. In the release notes for `13.4.13` I didn't notice anything suspicious, but I'm not that experienced in the Next.js community to know what to look for. I use [the Dockerfile](https://github.com/vercel/next.js/blob/canary/examples/with-docker/Dockerfile) that is linked in [the Next.js docs](https://nextjs.org/docs/pages/building-your-application/deploying#docker-image). My `next.config.js`: js /** @type {import('next').NextConfig} */ const nextConfig = { reactStrictMode: true, swcMinify: true, images: { remotePatterns: [ { protocol: 'https', hostname: '**' } ] }, output: 'standalone', } module.exports = nextConfig As you can see I don't declare the environment variables there. I'm directly accessing them in the code with `process.env.VAR_NAME`. When I run a container from the image locally and pass an environment variable to it with `-e VAR_NAME=value` it works fine with Next.js `13.4.12` but not with `13.4.13` and above. sh docker run -it -p 3000:3000 -e VAR_NAME=value my-image:latest I would appreciate any help or advice on how to debug this issue. I'd like to use latest Next.js in the end. Here is `next info` if it helps: yml Operating System: Platform: linux Arch: x64 Version: #1 SMP PREEMPT_DYNAMIC Thu May 11 16:51:53 UTC 2023 Binaries: Node: 18.12.1 npm: 8.1.3 Yarn: 1.22.17 pnpm: 8.6.12 Relevant Packages: next: 13.4.13 eslint-config-next: 13.4.18 react: 18.2.0 react-dom: 18.2.0 typescript: 5.1.6 Next.js Config: output: standalone
Maine Coon
I'm having this same problem. Did you solve it?
@Maine Coon I'm having this same problem. Did you solve it?
European sprat
Did you update nextjs?
@European sprat Did you update nextjs?
Maine Coon
I'm using 13.4.19
European sprat
And you're passing environment variables into the container and you've connected to the container and confirmed they are in the environment but nextjs still doesn't read them?
@Maine Coon I'm using 13.4.19
European sprat
And your image is for sure based on that version?
Maine Coon
Well that's where I'm confused because when I build it locally, the environment variables are there, but when it gets to ECS they are not
actually in the logs for the ECS task, it reads the environment variable, but the frontend javascript it is undefined
@Maine Coon actually in the logs for the ECS task, it reads the environment variable, but the frontend javascript it is undefined
European sprat
Open a new #help-forum thread with your dockerfile and the code to a client component file that is unable to read the public variable
Maine Coon
I finally got the env variable working using console.log('TRPC RUN ON', publicRuntimeConfig.trpc_api);
Now I'm not sure how to get the url of the running api service to that env var though.