Nx vs Next.js | Environments files loaded in wrong order
See original GitHub issueCurrent Behavior
nx serve command, load .env file in apps/<next-app> two times:
- Nx loading…: see order priority
- Next.js loading…: see order priority
That cause .env file to be loaded in process.env on the first loading (Nx) so during the second loading (Next.js) the order is no longer maintained -> https://github.com/vercel/next.js/blob/canary/packages/next-env/index.ts#L52
i.e: same env var in both .env.development and .env, cause .env to have highest priority
Expected Behavior
The order of Next.js environment variables should be maintained
i.e: same env var in both .env.development and .env, should give to .env.development highest priority
Steps to Reproduce
- Create Next.js app
- Create all possibles env files supported by Next.js and set in each file an env var with the same key but different value https://github.com/bkeepers/dotenv#what-other-env-files-can-i-use
- Launch
nx serve - the order is not maintained (ie: in process.env.<envVarName> there is
.envand not.env.development)
Environment
Node : 14.17.0
OS : darwin x64
yarn : 1.22.15
nx : 12.9.0
@nrwl/angular : Not Found
@nrwl/cli : 12.9.0
@nrwl/cypress : 12.9.0
@nrwl/devkit : 12.9.0
@nrwl/eslint-plugin-nx : 12.9.0
@nrwl/express : Not Found
@nrwl/jest : 12.9.0
@nrwl/linter : 12.9.0
@nrwl/nest : Not Found
@nrwl/next : 12.9.0
@nrwl/node : Not Found
@nrwl/nx-cloud : Not Found
@nrwl/react : 12.9.0
@nrwl/schematics : Not Found
@nrwl/tao : 12.9.0
@nrwl/web : 12.9.0
@nrwl/workspace : 12.9.0
@nrwl/storybook : 12.9.0
@nrwl/gatsby : Not Found
typescript : 4.3.5
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:6 (1 by maintainers)
Top Results From Across the Web
What's the difference between exposing environment ...
env file whereas the other uses the next.config file. Since Next.js 9.4, loading environment variables with .env files are now supported.
Read more >Using Environment Variables with Next.js and Nx | egghead.io
This is a perfect use case for environment variables. In this lesson, we learn about how Nx, as well as Next.js, is able...
Read more >Base Path - next.config.js
To deploy a Next.js application under a sub-path of a domain you can use the basePath config option. basePath allows you to set...
Read more >nrwl-nx/community - Gitter
ill play around with the root paths and see if i can make it work and report back ... have a config.json and...
Read more >Configuration | NestJS - A progressive Node.js framework
Externally defined environment variables are visible inside Node.js ... The above code will load and parse a .env file from the default location...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Same issue with loading order holds true for nest.js + nx combination. The load order of nx overrides the one of the nest.js config module definition which makes the integration not as smooth as everything else nx does so far.
A configuration option to disable default loader would be welcome.
Hi @kirjai , thanks for the answer.
Exactly! Fortunately, the environment variables loading process logic of Nx and Next.js is the same so if Nx can handle also these files would be great because we can use the framework transparently without having any side effects.