question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Use with webpack.DefinePlugin

See original GitHub issue

I’m trying to inject some constants into my app at build time under a global object called config, but no matter where I put my plugin, when I run the app I get 'config' is not defined. What am I doing wrong? Here’s my craco.config.js:

const webpack = require('webpack')
const reactHotReloadPlugin = require('craco-plugin-react-hot-reload')

const appConfig = require('./config')

module.exports = {
  webpack: {
    plugins: [
      new webpack.DefinePlugin({
        config: JSON.stringify(appConfig),
      })
    ]
  },
  plugins: [
    { plugin: reactHotReloadPlugin }
  ]
}

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:3
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

9reactions
david-soyezcommented, Oct 30, 2020

If you want it to work you have to do this:

  • First try console.log in your code because in chrome dev tools it will return undefined.
  • Second, put everything under process.env otherwise you will get a compilation error.
  • Or use dotenv-webpack instead
const { DefinePlugin } = require("webpack")
const Dotenv = require('dotenv-webpack')
const CracoAntDesignPlugin = require('craco-antd')

module.exports = {
  plugins: [
    {
      plugin: CracoAntDesignPlugin,
      options: {
        customizeTheme: {
        }
      }
    }
  ],
  webpack: {
    plugins: [
      new Dotenv(),
      new DefinePlugin({
        "process.env.VERSION": JSON.stringify(
          require("./package.json").version
        )
      })
    ]
  }
}

6reactions
saostadcommented, Apr 18, 2019

Here is the solution in case anybody wants to use it:

const { DefinePlugin } = require("webpack");

module.exports = {
  webpack: {
    plugins: [
      new DefinePlugin({
        "process.env.VERSION": JSON.stringify(
          require("./package.json").version
        ),
        "process.env.YEAR": new Date().getFullYear()
      })
    ]
  }
}

and here is another example I added ant design to config, it’s good if you need mix and match webpack plugins and babel plugins!

const { DefinePlugin } = require("webpack");

module.exports = {
  webpack: {
    plugins: [
      new DefinePlugin({
        "process.env.VERSION": JSON.stringify(
          require("./package.json").version
        ),
        "process.env.YEAR": new Date().getFullYear()
      })
    ]
  },
  plugins: [
    {
      plugin: require("craco-antd"),
      options: {
        customizeTheme: {
          "@primary-color": "#1DA57A"
        },
        lessLoaderOptions: {
          noIeCompat: true
        }
      }
    }
  ]
};
 
Read more comments on GitHub >

github_iconTop Results From Across the Web

DefinePlugin | webpack
The DefinePlugin replaces variables in your code with other values or expressions at compile time. This can be useful for allowing different behavior ......
Read more >
Environment Variables : Webpack config using DefinePlugin
“The DefinePlugin allows you to create global constants which can be configured at compile time.” We can use Define plugin in webpack.config ...
Read more >
How to include and use DefinePlugin in webpack config?
I Have "webpack": "^4.28.4" and define in webpack config new webpack.DefinePlugin({ PRODUCTION: JSON.stringify(true), });.
Read more >
webpack.DefinePlugin JavaScript and Node.js code examples
webpack. Best JavaScript code snippets using webpack.DefinePlugin(Showing top 15 results out of 909).
Read more >
The hidden potential of webpack DefinePlugin
tl;dr. If you want to pass environment variables and use it within your JavaScript, you need to: Create the environment variable ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found