141

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

0

8 Answers 8

259

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']
}
Sign up to request clarification or add additional context in comments.

6 Comments

For webpack 4, I found that i needed to put this in one of the rules that were listed under module... { module: { rules: [ { test: /\.jsx?$/, resolve: { extensions: [".js", ".jsx"] }, include: ... } ] }
@mrrogers I've searched everywhere for this! I tip my hat to you sir!
thank you sooo much! I was looking for that for hours!
@mrrogers Consider promoting your comment to an answer :)
@mrrogers Thank you! This solution also works for webpack 5!
|
48

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"
      }
    },
  ]
}

1 Comment

From 4.36.1 to be precise. Here's the doc webpack.js.org/configuration/module/#ruleresolve
29

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'] 
  }
};

1 Comment

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."
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: ...
      }
    ]
  }
}

Comments

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.

Comments

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.

Comments

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

Comments

-5

I found restarting my server fixed this issue. simply run

npm start

2 Comments

While restarting your server should always be tried, that is only restarting the server and not dealing with the current problem.
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.

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.