webpack can't find module if file named jsx

前端 未结 7 845
既然无缘
既然无缘 2020-11-28 06:43

As I write webpack.config.js like this

module.exports = {
  entry: \'./index.jsx\',
  output: {
    filename: \'bundle.js\'
  },
  module: {
    loaders: [{         


        
相关标签:
7条回答
  • 2020-11-28 06:46

    As mentioned in the comments on the answer from @max, for webpack 4, I found that I needed to put this in one of the rules that were listed under module, like so:

    {
      module: {
        rules: [ 
          {
            test: /\.jsx?$/, 
            resolve: {
              extensions: [".js", ".jsx"]
            },
            include: ...
          }
        ]
      }
    }
    
    0 讨论(0)
  • 2020-11-28 06:58

    I found restarting my server fixed this issue. simply run

    npm start
    
    0 讨论(0)
  • 2020-11-28 07:02

    Adding to the above answer,

    The resolve property is where you have to add all the file types you are using in your application.

    Suppose you want to use .jsx or .es6 files; you can happily include them here and webpack will resolve them:

    resolve: {
      extensions: ["", ".js", ".jsx", ".es6"]
    }
    

    If you add the extensions in the resolve property, you can remove them from the import statement:

    import Hello from './hello'; // Extensions removed
    import World from './world';
    

    Other scenarios like if you want coffee script to be detected you should configure your test property as well like:

    // webpack.config.js
    module.exports = {
      entry: './main.js',
      output: {
        filename: 'bundle.js'       
      },
      module: {
        loaders: [
          { test: /\.coffee$/, loader: 'coffee-loader' },
          {
            test: /\.js$/,
            loader: 'babel-loader',
            query: {
              presets: ['es2015', 'react']
            }
          }
        ]
      },
      resolve: {
        // you can now require('file') instead of require('file.coffee')
        extensions: ['', '.js', '.json', '.coffee'] 
      }
    };
    
    0 讨论(0)
  • 2020-11-28 07:02

    In the interest of readability and copy-paste coding. Here is the webpack 4 answer from mr rogers comment in this thread.

    module: {
      rules: [
        {
          test: /\.(js|jsx)$/,
          exclude: /node_modules/,
          resolve: {
            extensions: [".js", ".jsx"]
          },
          use: {
            loader: "babel-loader"
          }
        },
      ]
    }
    
    0 讨论(0)
  • 2020-11-28 07:07

    Webpack doesn't know to resolve .jsx files implicitly. You can specify a file extension in your app (import App from './containers/App.jsx';). Your current loader test says to use the babel loader when you explicitly import a file with the jsx extension.

    or, you can include .jsx in the extensions that webpack should resolve without explicit declaration:

    module.exports = {
      entry: './index.jsx',
      output: {
        filename: 'bundle.js'
      },
      module: {
        loaders: [{
          test: /\.jsx?$/,
          exclude: /node_modules/,
          loader: 'babel',
          query: {
            presets: ['es2015', 'react']
          }
        }]
      },
      resolve: {
        extensions: ['', '.js', '.jsx'],
      }
    };
    

    For Webpack 2, leave off the empty extension.

    resolve: {
      extensions: ['.js', '.jsx']
    }
    
    0 讨论(0)
  • 2020-11-28 07:11

    I was facing similar issue, and was able to resolve using resolve property.

    const path = require('path');
    module.exports = {
        entry: './src/app.jsx',
        output: {
            path: path.join(__dirname,'public'),
            filename:  'bundle.js'
        },
        module : {
            rules: [{
                loader: 'babel-loader',
                test: /\.jsx$/,
                exclude: /node_modules/
            }]
        },
        resolve: {
            extensions: ['.js', '.jsx']
        }
    }
    

    As You can see I have used .jsx in there which resolved following error

    ERROR in ./src/app.jsx Module not found: Error: Can't resolve

    For Reference: https://webpack.js.org/configuration/resolve/#resolve-extensions

    0 讨论(0)
提交回复
热议问题