variablesnext.jsstaticexportenvironment

NextJS Static Export Specify .env


I have a NextJS app with a Django backend. The NextJS app is statically exported which the Django app redirects too.

When exporting, NextJS uses .env.production by default, however when developing locally I'd prefer it to use .env.development however I can't figure out how to specify that.

When running via npm run dev it uses the proper .env however there is limited info on if this is even possible to choose for the static export - I've just been switching the contents of the files as needed.

Thank you in advance!


Solution

  • When the command next dev (npm run dev) is run, Next.js sets the NODE_ENV environment variable to development (production for anything else, e.g., export) and will load the following files in top-to-bottom order:

    1. .env.development.local
    2. .env.local
    3. .env.development
    4. .env

    So there's no need to specify that .env.development should be used — it just needs to exist in your project's root directory.

    .env.local gets loaded in both development and production environments, but not in test.

    .env gets loaded in all three environments.

    Variables in .env*.local will override the same variables in .env, .env.development, and .env.production.