138

As I write webpack.config.js like this

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

And in index.jsx I import a react module App

import React from 'react';
import { render } from 'react-dom';

import App from './containers/App';

let rootElement = document.getElementById('box')
render(
  <App />,
  rootElement
)

I find if I named module app in App.jsx, then webpack will say in index.jsx can't find module App, but if I named named module app in App.js, it will find this module and work well.

So, I'm confuse about it. In my webpack.config.js, I have writed test: /\.jsx?$/ to check file, but why named *.jsx can't be found?

enter image description here

Henrik Andersson
  • 45,354
  • 16
  • 98
  • 92
qiuyuntao
  • 2,314
  • 4
  • 19
  • 24

8 Answers8

252

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']
}
Jeremy
  • 163
  • 3
  • 14
max
  • 5,979
  • 2
  • 21
  • 16
45

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"
      }
    },
  ]
}
Richard Vanbergen
  • 1,904
  • 16
  • 29
28

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'] 
  }
};
Shea Hunter Belsky
  • 2,815
  • 3
  • 22
  • 31
Ignatius Andrew
  • 8,012
  • 3
  • 54
  • 54
  • 10
    In WebPack 3.4, you can't use resolve.extension with value empty. This will cause an exception in the compilation: "Invalid configuration object. Webpack has been initialised using configuration object that does not match the API schema. - configuration.resolve.extensions[0] should not be empty." – Julio Spader Aug 04 '17 at 19:09
14

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: ...
      }
    ]
  }
}
mr rogers
  • 3,200
  • 1
  • 19
  • 31
2

Verify, that bundle.js is being generated without errors (check the Task Runner Log).

I was getting 'can't find module if file named jsx' due to the syntax error in html in component render function.

rluks
  • 2,762
  • 8
  • 38
  • 56
2

I faced similar issue with imports while building my typescript project having the following webpack dependencies:

"webpack": "^5.28.0",
"webpack-cli": "^4.5.0" 

So, I had a App.tsx file present in the correct path and exported with named export. But, yarn build was failing with Module not found: Error: Can't resolve './App' in '/app/sample_proj/src'. The relevant code for this error is: import {App} from './App';. To fix this, I had to add .tsx under webpack known extensions. Sample entry from webpack.config.js is :

 module.exports = {
       ......
       resolve: {
            extensions: [ '.ts', '.js', '.tsx', '.jsx'],
        }
     }

Also, for this error, it does not matter, if the imports are default or named. webpack only needs to know the kind of file extensions it should deal with.

Binita Bharati
  • 5,239
  • 1
  • 43
  • 24
1

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

just-be-weird
  • 1,242
  • 9
  • 7
-5

I found restarting my server fixed this issue. simply run

npm start
  • 1
    While restarting your server should always be tried, that is only restarting the server and not dealing with the current problem. – Brittan McGinnis Dec 11 '20 at 22:41
  • In fact this solution solved my problem. For a create-react-app application where changing webpack code is not an option, this was the solution. – Leonidas Jan 21 '21 at 10:13