Next.js Discord

Discord Forum

API route not loading .env.development but detects .env file

Answered
Madeiran sardinella posted this in #help-forum
Open in Discord
Madeiran sardinellaOP
In my API route i'm trying to access a variable in .env.development file. But it looks like it does not detect the file when i deploy. it only loads .env and .env.production file

"build:dev": "dotenv -e .env.development next build",


i'm using this command to execute build
Answered by joulev
next build builds for production so won’t make use of development environment variables.

Similarly, next dev should use .env.development and not use .env.production
View full answer

56 Replies

Answer
Madeiran sardinellaOP
ohh! let me try doing that
thanks alot!!
Madeiran sardinellaOP
i did "build:dev": "dotenv -e .env.development next dev",

but it is stuck during deployment
i just realized. next dev command is for dev server right?
Yes
I think you are mistaking dev/prod for nextjs with the staging/production for your deployments, they are different
Nextjs always runs in production mode for all deployments, so when you build something to deploy, .env.production will be used
If you want different env vars for different deployment environments, you need other methods, depending on how you deploy the app
Madeiran sardinellaOP
Okay. I was using dotenv -e to load the .env.development file which works everywhere but api routes
shouldnt it load the .env.development file if i'm executing this command
"build:dev": "dotenv -e .env.development next build"
not sure why the api route does not load the .env.development file
@Madeiran sardinella shouldnt it load the .env.development file if i'm executing this command ts "build:dev": "dotenv -e .env.development next build"
This only loads the file during build. You need to load it during runtime as well: dotenv -e .env.development next start
Madeiran sardinellaOP
Sorry i'm just trying to understand. Are you saying i should run two commands. One for build and one for the API routes?

Something like this?
build:dev": dotenv -e .env.development next build && next start"
What does your package.json script look like?

"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "next lint"
}
Madeiran sardinellaOP
"scripts": {
"build:dev": "dotenv -e .env.development next build"
},
i'm using aws amplify to deploy this repo
but the api route is not picking up the .env.development file
i have a variable which is only for our development site
@joulev Where do you run next start? You need to load the .env file when you run next start
Madeiran sardinellaOP
At the moment i only do next build in aws amplify
i mean i specify yarn build:dev in the amplify.yml
@Madeiran sardinella At the moment i only do next build in aws amplify
If you don’t run next start how do you know the api route is not picking up the environment variables? You need to run the start command somewhere
At build time, only NEXT_PUBLIC env vars are recorded and put into the client bundle
Server side access of environment variables will use the environment variables present during runtime
So you need to set the environment variables to the runtime deployment environment
@joulev If you don’t run next start how do you know the api route is not picking up the environment variables? You need to run the start command somewhere
Madeiran sardinellaOP
what i did was, add this ENV_FILE_NAME= variable in .env.production, .env.development etc
export default function testAPI(req, res) {
  res
    .status(200)
    .json({
      envFileName: process.env.ENV_FILE_NAME,
    }); 
}
in .development i added this ENV_FILE_NAME='.env.development' & in .production this ENV_FILE_NAME='.env.production'
i then called the api to see which file is being used
it always used .env.production
i never used a start command maybe it defaults to production?
.env.production is automatically loaded during runtime
Madeiran sardinellaOP
okay
hmm
so i have to force it to use .env.development file during runtime
using the start command
?
like how i do with build
start:dev: "dotenv -e .env.development next start"
?
yes that looks like it should work
Madeiran sardinellaOP
got it
i'll have to figure out how to do this in aws-amplify
but thanks for your help. i was not aware i have to start the server as well.
there should be a settings page or something that allows you to do that i think
Madeiran sardinellaOP
in amplify.yml there is a backend section where we can execute backend commands
but for some reason it is executing it first and not building
looking into it
this thing i cant help at all cuz i never used this platform but good luck
but yeah you now understand it: you need to load the env vars into the server hosting the app during runtime as well
not just build time
@joulev this thing i cant help at all cuz i never used this platform but good luck
Madeiran sardinellaOP
i'll figure it out
@joulev not just build time
Madeiran sardinellaOP
yeah i didnt know this
thanks 🙂