Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

2.6K
Visualizações
next.js environment variables are undefined (Next.js 10.0.5)

I am coding a website with Next.js and I tried to add google Tag Manager.
I followed the tutorial on the Next.js Github example but for some reasons I can't access to my environment variables.
It says my variable is undefined.
I created a file .env.local on my project folder (at the same level as components, node_modules, pages, etc)
In this file I created a variable like this (test purpose) :

NEXT_PUBLIC_DB_HOST=localhost

And on my index page I tried this code :

console.log("test ", process.env.NEXT_PUBLIC_DB_HOST);

But in my console I get a "test undefined".
I tried to put my variable into an .env file instead, without success.
What I am doing wrong ?

over 4 years ago · Santiago Trujillo
7 Respostas
Responde à pergunta

0

Restarting the server worked for me.

  1. Edit & save .env.local
  2. Stop the server and restart it, npm run dev
  3. You should get an output on the next line like this:
> klout@0.1.0 dev
> next dev

Loaded env from [path]/.env.local
over 4 years ago · Santiago Trujillo Relatório

0

This envs just works in Server Side. To access this envs in Client Side, you need declare in the next.config.js

This way:

module.exports = {
  reactStrictMode: true,
  env: {
    BASE_URL: process.env.BASE_URL,
  }
}
over 4 years ago · Santiago Trujillo Relatório

0

After spending countless hours on this, I found that there is a tiny little paragraph in both the pre and post nextjs 9.4 documentation:

  • (Pre-9.4) https://nextjs.org/docs/api-reference/next.config.js/environment-variables (same as this answer)

    Next.js will replace process.env.customKey with 'my-value' at build time.

  • (^9.4) https://nextjs.org/docs/basic-features/environment-variables

    In order to keep server-only secrets safe, Next.js replaces process.env.* with the correct values at build time.

Key words being BUILD TIME. This means you must have set these variables when running next build and not (just) at next start to be available for the client side to access these variables.

over 4 years ago · Santiago Trujillo Relatório

0

  1. Create .env (all environments), .env.development (development environment), and .env.production (production environment).

  2. Add the prefix NEXT_PUBLIC to all of your environment variables.

NEXT_PUBLIC_API_URL=http://localhost:3000/

  1. Use with prefix process.env

process.env.NEXT_PUBLIC_API_URL

  1. Stop the server and restart it:

npm run dev

  1. I hope it works. This solution for latest version of nextJs (above 9)
over 4 years ago · Santiago Trujillo Relatório

0

For those using NextJS +9 and looking for environment variables in the browser, you should use the NEXT_PUBLIC_ prefix. Example:

NEXT_PUBLIC_ANALYTICS_ID=123456789

See documentation for reference.

over 4 years ago · Santiago Trujillo Relatório

0

This is my next.config.js file.

/** @type {import('next').NextConfig} */
const nextConfig = {
  reactStrictMode: true,
  env: {
    BASE_URL: process.env.NEXT_PUBLIC_SITE_URL,
  },
};

module.exports = nextConfig;

Restart the server and it worked fine. using Nextjs 12.1.0 with typescript

over 4 years ago · Santiago Trujillo Relatório

0

In my case, Im pasting REACT_APP_API_URL instead of NEXT_PUBLIC_API_URL.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda