2

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.

2 Answers 2

3

in next.config.js, define

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

Sign up to request clarification or add additional context in comments.

2 Comments

Then how can I call it inside _app.js I tried your solution and called ${process.env.ID} inside _app.js, still undefined
@PujaGarg you can directly use it in the _app.js like "ID".
3

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

1 Comment

this answer is not correct, because the front-end cannot read envirement constants . this should be read in server and properly in getStaticsProps

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.