4

I am using webpack, reactjs, typescript. In my .tsx code, I have a requirement where I need to route to URL depending upon my environment i.e. Production or Development. So, how can I check for the environment and load the correct URL dynamically ?

5
  • using environment variables is one option - webpack.js.org/guides/environment-variables Commented Apr 3, 2018 at 21:26
  • How will that help dynamically i.e. during actual code execution ? Config is only used during the build process. Could you please elaborate with a sample if possible ? Commented Apr 3, 2018 at 21:31
  • Hi @AngshumanAgarwal, have you achieved something? I would like to have some feedback on my answer Commented Apr 4, 2018 at 11:14
  • Thanks for your answer - researched more and found that webpack has nicely documented it too - webpack.js.org/guides/production Commented Apr 4, 2018 at 11:26
  • @AngshumanAgarwal Woah, that's cool. I didn't know that. Going to apply it to my projects haha Commented Apr 4, 2018 at 11:47

1 Answer 1

3

You can add a plugin to define an environment variable that can be accessed within the code, like this:

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

And then inside your code you just have to check for process.env.NODE_ENV.

Needless to say that you can manage the plugin with an environment variable that you can pass via cli, like this:

webpack --env.production

And then have your webpack.config.js with something like this:

module.exports = function(env) {
    return {
        /*[...]*/
        plugins: [new webpack.DefinePlugin({
            'process.env.NODE_ENV': JSON.stringify(env.production ? 'production' : 'development')
        })]
        /*[...]*/
    };
};

Source: That's how react works :-)

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks - will give a shot at it. My understanding is since the NODE_ENV environment variable and the corresponding process.env.NODE_ENV runtime field are strictly Node-specific concepts, by default that value does not exist in client-side code. Looks like DefinePlugin is forcing that to be a global variable ?
@AngshumanAgarwal Exactly, DefinePlugin makes that inside any webpack bundle code running in any environment will have the defined variable.
Hope UglifyJS does not strip that out as a dead code brach during minification because it will encounter if("production" !== "production") where process.env.NODE_ENV = "production" -- as being set from the cli during the build process.
@AngshumanAgarwal There's no problem with that, as if the branch is never reached, you don't need the code inside it. Is actually good to strip it to reduce bundle size.
import using "require" worked for me: new (require('webpack')).DefinePlugin({ ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.