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

456
Visualizações
React + webpack: 'process.env' is undefined

I'm trying to run the hot dev server on our site with webpack; the site uses ReactJS, which has this code in it:

if (\"production\" !== process.env.NODE_ENV) // etc

When not running hot-swap it's fine, but with the hot-swap, it gets run, resulting in the error:

TypeError: process.env is undefined

The code looks like this:

error is on this line

The project is modelled after https://github.com/webpack/react-starter which does work; so the question is; what error have I made in the config file and/or how do I go about looking for the error when the 'production' compilation works just fine?

I've posted the gist of the webpack config file.

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

0

In your webpack config, there are two options that can affect process.env:

  • When you specify config.target (see config.target)
  • When you define the process.env variable via DefinePlugin

Looking at your code, it looks like process.env might be undefined when both options.prerender and options.minimize are false.

You could fix this by always using an environment that defines process.env (ex: node), or by using DefinePlugin to assign a default value to the variable yourself.

over 4 years ago · Santiago Trujillo Relatório

0

This answer made more sense to me. Posting for others with the same need for a complete example.

https://github.com/webpack/webpack/issues/868

over 4 years ago · Santiago Trujillo Relatório

0

This is the simplest way:

new webpack.EnvironmentPlugin( { ...process.env } )

Add that to your list of webpack plugins.

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