Reputation: 338
I have a server-side rendering application built with expressjs and webpack. There are some react scripts in the SSR pages. However, after console log I found that process.env are not always a same value in different js files.
I use the process.env.node_env to define different API urls in corresponding environment and use Dotenv-webpack to inject node_env.
Undefined node_env are all console logged on terminal before webpack built info. They are all used on SSR static pages.
[nodemon] starting `node ./bin/www.js`
4 undefined
envenvenv undefined
19 undefined
envenvenv undefined
18 undefined
envenvenv undefined
5 undefined
envenvenv undefined
6 undefined
envenvenv undefined
3 undefined
envenvenv undefined
17 undefined
envenvenv undefined
(node:27468) DeprecationWarning: Tapable.plugin is deprecated. Use new API on `.hooks` instead
webpack built 6a9a1e2ed86071ed2ac3 in 14421ms
Hash: 6a9a1e2ed86071ed2ac3
Version: webpack 4.45.0
Time: 14421ms
Built at: 07/20/2021 11:11:15 AM
While the correct defined node_env are all console logged on chrome and they are all used on react components.
I suppose that some SSR pages are "generated" before webpack bundle so they are undefined. But the react components are rendered in chrome after bundled and sent to client so they have correct node_env.
Is it correct? How to solve this issue?
Upvotes: 2
Views: 419
Reputation: 338
Ernesto's method should be a good solution, while I implement dotenv instead of dotenv-webpack to the specific file where node_env is needed.
It seems that SSR page is compiled before webpack bundle. So that I cannot use dotenv-webpack plugin to make them available.
Upvotes: 0
Reputation: 4252
nodemon is telling you the err, The command been executed is
node ./bin/www.js
you need to add NODE_ENV to it
NODE_ENV=development node ./bin/www.js
Upvotes: 1