question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add support for custom environments (Next 9.4)

See original GitHub issue

Feature request

Is your feature request related to a problem? Please describe.

Currently, built-in environments allows only .env.development and .env.production environments, so when you run yarn dev it uses .env.development or .env.local and when you do yarn build, then yarn start it uses .env.production environment. But there’s no way to use a staging environment for example, like .env.staging, there’s no way to run app with staging environment, if we try to NODE_ENV=staging yarn build && yarn start we get an error: “https://github.com/zeit/next.js/blob/master/errors/non-standard-node-env.md”, and it won’t have effect, because NODE_ENV would be production anyway, it’s being overwritten.

Describe the solution you’d like

There should be a way to set environment when you run your app, like NODE_ENV=staging/prestaging yarn build && yarn start, etc. In CRA i use it this way:

"build:staging": "env-cmd -f .env.staging yarn build",
"build:prestaging": "env-cmd -f .env.prestaging yarn build"

Would be nice to have the same, but built-in in Next.

PS

"build:staging": "env-cmd -f .env.staging yarn build && yarn start" this actually works with Next either, though NODE_ENV would be set to “production”.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:19
  • Comments:12 (4 by maintainers)

github_iconTop GitHub Comments

44reactions
timneutkenscommented, May 12, 2020

We already give you flexibility to choose from 8 different files, which should be enough:

  • .env
  • .env.local
  • .env.development
  • .env.development.local
  • .env.test
  • .env.test.local
  • .env.production
  • .env.production.local

https://nextjs.org/docs/basic-features/environment-variables#exposing-environment-variables

This has been the standardized way that people load .env files for a long time and has prior art:

Hence why I don’t think introducing even more ways to load .env files is a feasible idea. Supporting 8 different files was already a stretch but it was what’s mostly supported as said.

42reactions
evangowcommented, Feb 10, 2021

I would say that .env.staging is probably a common enough use-case that it should be supported out-of-the-box

Read more comments on GitHub >

github_iconTop Results From Across the Web

Blog - Next.js 9.4
Next.js 9.4 introduces React Fast Refresh, Incremental Static Regeneration, New Environment Support, Built-in Fetch, and more!
Read more >
How to add Environment Variables in Next.JS - YouTube
After the release of Next.js 9.4, the framework supports dotfiles giving a more intuitive and ergonomic experience for adding environment ...
Read more >
Environment Variables in Next.js - Frontend Digest
Here you have the option to define different environment variables depending on whether a production or a preview build is deployed. You can...
Read more >
Next.js environment variables - Refine Dev
Built-in support for environment variables got a lot easier in Next.js versions 9.4 and later. This means using environment variables in ...
Read more >
Environment variables not working (Next.JS 9.4.4)
Any help would be much appreciated! EDIT : So i've fixed this by adding my env variables to my next.config.js like so :...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Hashnode Post

No results found