PepperAddict
PepperAddict

Reputation: 1008

webpack won't set to production. Defaults to development

I'm lost. I've been following a Udemy Webpack 4 tutorial on webpack and I am stuck on having different configs for different modes.

I can build from my production config just fine, but when it comes to running the node server, it's not set to production.

I have my main.js write this:

console.log(`Environment is: ${process.env.NODE_ENV`}

and I have only seen it in show as Development.

Here's my package.json's script to run production: I run npm run build and then npm run prod

  "scripts": {
    "build:dev": "webpack --config=config/webpack.dev.js",
    "build": "webpack --config=config/webpack.prod.js",
    "dev": "nodemon --inspect --watch config --watch src/server 
     src/server/main.js",
    "prod": "cross-env NODE_ENV=production node src/server/main.js"
  },

It was mentioned that I needed to define NODE_ENV in the webpack prod config in the plugin section. Here is how I have it:

    new webpack.DefinePlugin({
        'process.env': {
            NODE_ENV:'"production"'
        }
    }),

I've tried it in different ways: 'process.env': {'NODE_ENV': JSON.Stringify("production")} and a couple of other ways and I have had no luck.

Here is my repository to see my complete config: https://github.com/PepperAddict/webpack-express

I would appreciate any help even recommendation on a better way to set this up. Thank you in advance!

Upvotes: 1

Views: 610

Answers (1)

user4851087
user4851087

Reputation:

Below is mine config using webpack 4

"prod": "webpack -p --mode=production --config webpack.prod.js",
"start": "webpack --mode=development --config webpack.dev.js",

And if you want to set NODE_ENV you can do like this

"prod": "SET NODE_ENV=production& webpack -p --mode=production",

Hope that help

Upvotes: 2

Related Questions