jcubic
jcubic

Reputation: 66480

webpack - how to have different variable for production and development environment

Is it possible to have one variables that will be different on dev and production environments. What should I use to make that variable. Is testing for location url a good solution?

Upvotes: 1

Views: 190

Answers (1)

mguijarr
mguijarr

Reputation: 7900

In your package.json file, you can set environment variables in the script section, like NODE_ENV=production for the build script in this example:

{
  "scripts": {
  "start": "npm run dev",
  "dev": "./node_modules/.bin/webpack-dev-server ...",
  "build": "NODE_ENV=production ./node_modules/.bin/webpack -p --config webpack.production.config.js"
} ... }

Then, in your Javascript code you can test for it using the process global:

if (process.env.NODE_ENV === 'production') { ... } else { ... }

Hope this helps.

Upvotes: 2

Related Questions