Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Standalone build does not include server-side variables from .env.local #46296

Open
1 task done
evakkuri opened this issue Feb 23, 2023 · 10 comments
Open
1 task done

Standalone build does not include server-side variables from .env.local #46296

evakkuri opened this issue Feb 23, 2023 · 10 comments
Labels
bug Issue was opened via the bug report template. Output (export/standalone) Related to the the output option in `next.config.js`.

Comments

@evakkuri
Copy link

evakkuri commented Feb 23, 2023

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)

@evakkuri evakkuri added the bug Issue was opened via the bug report template. label Feb 23, 2023
@balazsorban44 balazsorban44 added the Output (export/standalone) Related to the the output option in `next.config.js`. label Feb 23, 2023
@feledori
Copy link

feledori commented Mar 9, 2023

Having this issue at the moment. Have you found a workaround?

@evakkuri
Copy link
Author

evakkuri commented Mar 10, 2023

I have not, unfortunately. In my case, I luckily could get by with using just .env

@feledori
Copy link

Yes I did the same on before build i copy files to .env and temp delete the other files. This is also a affects .env.test.

@qpavy
Copy link

qpavy commented May 19, 2023

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

@klawson6
Copy link

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.

@EnablingLogic
Copy link

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

@mikgross
Copy link

mikgross commented Apr 9, 2024

Hey all, same for .env.test

@tolgaand

This comment has been minimized.

@dogukanakkaya
Copy link

dogukanakkaya commented Aug 9, 2024

I ran into the same issue with my GitHub Actions CI. I've created the env file in the CI itself:

 - name: "Create frontend env file"
   run: |
        touch apps/web/.env
        echo NEXT_PUBLIC_ENV1=${{ secrets.NEXT_PUBLIC_ENV1 }} >> apps/web/.env
        echo NEXT_PUBLIC_ENV2=${{ secrets.NEXT_PUBLIC_ENV2 }} >> apps/web/.env

@Dacaramo
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Issue was opened via the bug report template. Output (export/standalone) Related to the the output option in `next.config.js`.
Projects
None yet
Development

No branches or pull requests

10 participants