Puja Garg
Puja Garg

Reputation: 251

How to access .env variable inside _app.js file

env file

ID=123

I am able access this variable inside my pages/index.js file by ${process.env.ID} but when I am trying same in my _app.js file its giving undefined, can anyone please suggest me how I can access this inside my _app.js file.

Upvotes: 2

Views: 1479

Answers (2)

Ivan Maeder
Ivan Maeder

Reputation: 331

You can use process.env but for security reasons the vars need to start with NEXT_PUBLIC_ (explained here).

In the .env,

NEXT_PUBLIC_ID=123

In the _app.js (or any front-end code),

process.env.NEXT_PUBLIC_ID

Upvotes: 3

Bhoomika Agrawal
Bhoomika Agrawal

Reputation: 69

in next.config.js, define

env: { ID: process.env.ID },

Upvotes: 3

Related Questions