saintyusuf
saintyusuf

Reputation: 504

Can't reach env from React Component in Next.js

I have a Next.js project. I have created .env file and using enviroments from this file in server.js file. But when I want to reach these enviroments from component, I can't access. What should I do?

This is .env file:

SERVER_PORT=3000
DOMAIN=127.0.0.1
DATABASE=dburl

This is server.js file:

require('dotenv/config')

const port = process.env.SERVER_PORT || 3000
server.listen(port, (err) => {
      if(err) throw err
      console.log(`Server listen on http://127.0.0.1:${port}`)
    })

This is any component of react:

axios
    .get(`http://${process.env.DOMAIN}/api/improve-language`)
    .then(res => {
      ...
    })
    .catch(err =>{
      console.log(err)
    })

Upvotes: 2

Views: 4073

Answers (1)

Peter Kusza
Peter Kusza

Reputation: 471

Based on the documentation if you want to use the env variables in the browser, they must be named with prefix NEXT_PUBLIC_.

Upvotes: 4

Related Questions