0

I'm trying to set an external config in my webpack according to the NODE_ENV.

Here's my script code from package.json:

"scripts": {
    "build": "set NODE_ENV=production && webpack --mode production",
    "start": "webpack-dev-server --hot --mode development"
  },

Then in my webpack.config.js I have the following code:

externals: {
      'Config': JSON.stringify(process.env.NODE_ENV === 'production' ? {
          apiUrl: "."
      } : {
          apiUrl: "http://localhost:3000"
      })
    },

Somehow this doesn't work. I always get the "false" (localhost) as value. I'm 100 % sure process.env.NODE_ENV is set since first of all, I log it at the beginning of the file (console.log(process.env.NODE_ENV)) and it gives me "production" as answer.

Second, I tried to create another config like:

externals: {
   'Test': {"mytest": process.env.NODE_ENV } ...

and this sets mytest to "production" ....

I have no idea why this doesn't work, I copied the code from stackoverflow (How to store Configuration file and read it using React).

I tried everything... I wrapped around the JSON.stringify around other parts, I tried to debuff as good as I can. Its now the 4th hour I spend on this problem and I think its a good moment to ask here for some advise.

If you can help me solve this problem, I would be really thankful!

Have a nice day.

Twinfriends
  • 1,972
  • 1
  • 14
  • 34
  • "set NODE_ENV=production" works only on windows and on that executed script. If you log(process.env.NODE_ENV) before webpack what is the result? Just make sure your env is set.. you can be sure by using `crossenv`. – yotke Oct 04 '18 at 14:39
  • @shmotam I'm working on a Win10 machine, so that shouldn't be the problem. What do you mean by "before webpack"? Not sure what "before webpack" ... I placed the console.log(process.env.NODE_ENV) at the top of the webpack.config.js file, before the "require("webpack")" part, and it still loggs correctly. – Twinfriends Oct 04 '18 at 14:48
  • I meant at the top of webpack.config.js. Do you get any errors? do you have a file config.js and do you require it/import in react component? – yotke Oct 04 '18 at 14:56
  • Don't get any errors, it just logs "production" as I want. Nope, no external config file. But I may just found a solution, gonna test it tomorrow and post it here if it works – Twinfriends Oct 04 '18 at 15:05

0 Answers0