Next.js Discord

Discord Forum

help with understanding how env files works in next js

Answered
Raspberry Horntail posted this in #help-forum
Open in Discord
Raspberry HorntailOP
Can someone help, to run scripts on a node (for example, fill a table with data) that uses env files, do I need to use the dotenv package? Next doesn't do this?
Answered by B33fb0n3
there will be no collisions. This is the loading order:
View full answer

10 Replies

@Raspberry Horntail Can someone help, to run scripts on a node (for example, fill a table with data) that uses env files, do I need to use the dotenv package? Next doesn't do this?
Macao paper wasp
Yes, to use environment variables in a Node.js script, you typically need to use the dotenv package to load variables from a .env file into process.env. While Next.js automatically loads environment variables from .env files in the root of the project for use in its environment, this does not extend to standalone Node.js scripts.
npm install dotenv
and load the environment variables in your Node.js script.
require('dotenv').config();
@Raspberry Horntail can you explain when next js loads enviroment variables?
Macao paper wasp
Next.js loads environment variables at specific points during its lifecycle to ensure they are available in the right context.
@Raspberry Horntail and it's not going to collision with dotenv package?
there will be no collisions. This is the loading order:
Answer