Skip to main content
Post Undeleted by zerkms
deleted 98 characters in body
Source Link
zerkms
  • 256.1k
  • 73
  • 447
  • 551

That's how I usually do that:

if-envnew NODE_ENV=productionwebpack.DefinePlugin({  && npm run start'process.env':prod ||{  npm run start NODE_ENV:dev JSON.stringify(process.env.NODE_ENV), }, }), 

This command startsAnd pass that to the array of the webpack plugins.

Why the problem happens: when wepack processes code npm run start:prod without- the code being processed is not actually run but simply read+processed. So when you run it NODE_ENV- then it's too late to access environment variables.

Since it's a separateYou're checking the environment for the process you mustrunning on server, while react runs in the browser so obviously it does not have access to passthe server process environment variables. Hence you need to inject it there explicitly, like during build time.

if-env NODE_ENV=production && NODE_ENV=production npm run start:prod || npm run start:dev 
if-env NODE_ENV=production && npm run start:prod || npm run start:dev 

This command starts npm run start:prod without NODE_ENV.

Since it's a separate process you must to pass environment variables explicitly, like

if-env NODE_ENV=production && NODE_ENV=production npm run start:prod || npm run start:dev 

That's how I usually do that:

new webpack.DefinePlugin({  'process.env': {   NODE_ENV: JSON.stringify(process.env.NODE_ENV), }, }), 

And pass that to the array of the webpack plugins.

Why the problem happens: when wepack processes code - the code being processed is not actually run but simply read+processed. So when you run it - then it's too late to access environment variables.

You're checking the environment for the process running on server, while react runs in the browser so obviously it does not have access to the server process environment variables. Hence you need to inject it there explicitly during build time.

Post Deleted by zerkms
Source Link
zerkms
  • 256.1k
  • 73
  • 447
  • 551

if-env NODE_ENV=production && npm run start:prod || npm run start:dev 

This command starts npm run start:prod without NODE_ENV.

Since it's a separate process you must to pass environment variables explicitly, like

if-env NODE_ENV=production && NODE_ENV=production npm run start:prod || npm run start:dev