reactjswebpack

Webpack automatically require a file in all files...?


Using Webpack and building an app in React. In all my files, I have to include to type: var React = require('React'); seems like a useless repetition.

Is there something I can add in the config file ofwebpack to import/require React in all my files?


Solution

  • You can use ProvidePlugin to automatically require React when needed.

    Just add to your webpack config file:

    var webpack = require('webpack');
    
    module.exports = {
      // Your configuration stuff.
      plugins: [
        new webpack.ProvidePlugin({
          React: 'react'
        })
      ]
    };
    

    Every time React is used in a file it will be auto-required/imported without having to do it explicitly.