Next.js Discord

Discord Forum

.env file is the only one respected by standalone build

Answered
Devon Rex posted this in #help-forum
Open in Discord
Devon RexOP
It looks like all the other files like env.production or env.production.local are ignored by the build if standalone option is set to true. During build it looks like it's all read corectly. Console.logs are returing process.env.VARIABLE correctly, but the distribution ran with "node .next/standalone/server.js" is missing variables from env.* files. If I remove .env build is progressing correctly reading env.production.local and env.production but the result is missing all the variables as there was no .env file. Looks like it's the only file respected for standalone. Is that expected behavior?
Answered by Devon Rex
Maybe it's just how it is. I handle it by copying the .env.test as .env.production into standalone folder (.next/standalone/.env.production) after the build.
View full answer

13 Replies

Burmese
you could try booting your standalone build via dotenvx run or even https://github.com/chihab/dotenv-run
which would do the loading, and inject the env vars into the nextjs process
I'm also working on something new that might help, feel free to send me a DM if you want to try it
Burmese
You can also try using https://varlock.dev/integrations/nextjs/ to manage your env files, and when you boot your standalone app, use varlock run -- node .next/standalone/server.js
@Burmese You can also try using https://varlock.dev/integrations/nextjs/ to manage your env files, and when you boot your standalone app, use `varlock run -- node .next/standalone/server.js`
Devon RexOP
Thanks! That looks like pretty nice plugin, but I have some troubles to set it up. I am getting:
$ pnpm dev                                      
> biogas-resources@0.1.0 dev /home/kalinq/Workspace/biostock/bio-resources
> next dev --turbopack

🚨 process.env.__VARLOCK_ENV is not set 🚨

To use this plugin, you must override @next/env with @varlock/next-integration
See https://varlock.dev/integrations/nextjs for more information

/home/kalinq/Workspace/biostock/bio-resources/node_modules/.pnpm/@varlock+nextjs-integration@0.0.2_next@15.3.5_@babel+core@7.28.0_react-dom@19.1.0_react_9059da9ba388feb8adbb5c41696de238/node_modules/@varlock/nextjs-integration/dist/plugin.js:23
  throw new Error("VarlockNextWebpackPlugin: __VARLOCK_ENV is not set");
        ^

Error: VarlockNextWebpackPlugin: __VARLOCK_ENV is not set
    at Object.<anonymous> (/home/kalinq/Workspace/biostock/bio-resources/node_modules/.pnpm/@varlock+nextjs-integration@0.0.2_next@15.3.5_@babel+core@7.28.0_react-dom@19.1.0_react_9059da9ba388feb8adbb5c41696de238/node_modules/@varlock/nextjs-integration/dist/plugin.js:23:9)
    at Module._compile (node:internal/modules/cjs/loader:1562:14)
    at node:internal/modules/cjs/loader:1699:10
    at require.extensions..js (/home/kalinq/Workspace/biostock/bio-resources/node_modules/.pnpm/next@15.3.5_@babel+core@7.28.0_react-dom@19.1.0_react@19.1.0__react@19.1.0/node_modules/next/dist/build/next-config-ts/require-hook.js:49:20)
    at Module.load (node:internal/modules/cjs/loader:1313:32)
    at Function._load (node:internal/modules/cjs/loader:1123:12)
    at TracingChannel.traceSync (node:diagnostics_channel:322:14)
    at wrapModuleLoad ...
Node.js v22.13.1
Tho it seems to be overrided correctly:
$ pnpm why @next/env                                                                (varlock✱) 
Legend: production dependency, optional only, dev only

biogas-resources@0.1.0 /home/kalinq/Workspace/biostock/bio-resources (PRIVATE)

dependencies:
@kinde-oss/kinde-auth-nextjs 2.8.2
└─┬ next 15.3.5 peer
  └── @next/env link:@varlock/nextjs-integration
@next/third-parties 15.3.5
└─┬ next 15.3.5 peer
  └── @next/env link:@varlock/nextjs-integration
@varlock/nextjs-integration 0.0.2
└─┬ next 15.3.5 peer
  └── @next/env link:@varlock/nextjs-integration
cookies-next 5.1.0
└─┬ next 15.3.5 peer
  └── @next/env link:@varlock/nextjs-integration
next 15.3.5
└── @next/env link:@varlock/nextjs-integration
Devon RexOP
oh. I figured it out.
overrides:
  "@next/env": "npm:@varlock/nextjs-integration"
Devon RexOP
@Burmese I am encountering pretty much the same issue. Only .env variables are being read and .env.test seems to be ignored.

During the build I can see expected env variables, but when running standalone there're only .env ones.

It looks to me that raw .env is just and only copied into standalone version despite the NODE_ENV or APP_ENV during the build. I would expect generated .env file based on NODE_ENV variable during build, but maybe I am wrong here.

What I really wanted to achieve is standalone version for e2e testing purpose.
@Losti! Can you test this library to see if it works for you? https://github.com/t3-oss/t3-env
Devon RexOP
Experiencing the same behavior with "@t3-oss/env-nextjs".
Devon RexOP
Maybe it's just how it is. I handle it by copying the .env.test as .env.production into standalone folder (.next/standalone/.env.production) after the build.
Answer
Burmese
Ah yes you must copy them into that folder. I’ll add that to our docs πŸ™‚
@Devon Rex <@110561554197401600> I am encountering pretty much the same issue. Only .env variables are being read and .env.test seems to be ignored. During the build I can see expected env variables, but when running standalone there're only .env ones. It looks to me that raw .env is just and only copied into standalone version despite the NODE_ENV or APP_ENV during the build. I would expect generated .env file based on NODE_ENV variable during build, but maybe I am wrong here. What I really wanted to achieve is standalone version for e2e testing purpose.
Burmese
Yeah that makes sense. Next internally assumes if you are doing a build that it must be production, so they would only think about copying over .env and .env.production

the varlock plugin allows you much more control over what the current env is - including adding new ones like staging/preview/etc, or to override the current value.

But because next is still in charge of doing the standalone build and I don't think I can hook into it, you'll still have to copy over all the env files manually