next.js: Standalone build does not include server-side variables from `.env.local`

Verify canary release

  • I verified that the issue exists in the latest Next.js canary release

Provide environment information

Operating System: Platform: darwin Arch: arm64 Version: Darwin Kernel Version 22.2.0: Fri Nov 11 02:04:44 PST 2022; root:xnu-8792.61.2~4/RELEASE_ARM64_T8103 Binaries: Node: 18.13.0 npm: 8.19.3 Yarn: 1.22.19 pnpm: N/A Relevant packages: next: 13.1.7-canary.28 eslint-config-next: N/A react: 18.2.0 react-dom: 18.2.0

Which area(s) of Next.js are affected? (leave empty if unsure)

Standalone mode (output: “standalone”)

Link to the code that reproduces this issue

https://github.com/evakkuri/nextjs-standalone-envvar-bug

To Reproduce

  • Build Docker image: docker build -t nextjs-standalone-envvar-bug:latest .
    • Dockerfile from NextJS with-docker example, apart from creating .env and .env.local files
  • Run container: docker run -p 3000:3000 nextjs-standalone-envvar-bug
  • Navigate to http://localhost:3000
  • Note that from server logs that process.env.API_URL_1 is undefined, and all other environment variables are defined as expected

Describe the Bug

Standalone build does not include server-side environment variables from .env.local, at least with the Dockerfile as per NextJS example.

Expected Behavior

Also process.env.API_URL_1should have retrieved a value from .env.local, indicating that server-side variables are read at build-time from .env.local.

Which browser are you using? (if relevant)

Firefox 110.0 (64-bit)

How are you deploying your application? (if relevant)

Dockerfile (included in repo)

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Reactions: 17
  • Comments: 20

Most upvoted comments

This is still an issue in 2024. If you do not precede all of the variables names with NEXT_PUBLIC the variables will not be available on the server. This leaves the SSR feature of storing secrets on the Next.js server completely unusable. This must be fixed as soon as possible.

It is important to notice that this is an issue not only for environment variables defined in the .env file, but also for environment variables that are passed to next build command.

Doing:

MY_VARIABLE=my-variable-value next build

Does not work neither, using process.env.MY_VARIABLE server side will always give undefined.

Nextjs only copies .env to the standalone folder so we need to manually copy other environment files (.env.local, .env.production… ) to the standalone folder as well to make them work together.

This is so confusing and should at least be mentioned on the docs.

Environment variables should be loaded at runtime rather than being bundled into the build or included in the standalone folder.

We hit this issue when building our containers in Github Actions. The .env and .env.production files are imported but not the .env.local We’re currently copying that file manually in the container as a workaround, but this is creating unneeded technical debt.

I’m also having this issue. .env and .env.production are copied to .next/standalone, but .env.local is missing.

I have experienced this issue - public variables from .env.local are inlined into the build perfectly fine, but the server-side variables are missing from the resultant .env file which is bundled with the build.

The workaround I’ve found is to simply copy the .env.local file to the build folder. In the case of your repo, copying to the app folder of the runner stage in Dockerfile (this is how mine is set up too). It seems like env files are still picked up by a build at run time.

With this answer I was able to access my environment variable inside my docker container.

What I did:

Add my environment variables as docker build arg

docker build --build-arg API_URL_1=https://... -t nextjs-standalone-envvar-bug:latest .

Define ENV variable in Dockerfile

ARG API_URL_1
ENV API_URL_1 = ${API_URL_1}

Use global.process.env instead of process.env

global.process.env.API_URL_1