5

For some reason I can't seem to get Webpack to import modules from .JSX files. Every time I try to run Webpack I'll get this message:

ERROR in ./src/Example.jsx
Module parse failed: /path/to/project/src/Example.jsx Unexpected token (6:12)
You may need an appropriate loader to handle this file type.
SyntaxError: Unexpected token (6:12)

The thing is; there isn't a whole lot in ./src/Example.jsx. In fact this is all that it contains:

import React from 'react';


export default class Example extends React.Component{
  render() {
    return (<h2>Hello world!!</h2>);
  }
};

Webpack did not have any issues when I had the class in my index.jsx file but when I moved it to its own file webpack all of a sudden couldn figure out what to do. I've tried to resolve this by using babel-plugin-transform-react-jsx but that didn't seem to solve my problem. What do I need to do to get Webpack to properly convert/parse .JSX files?

/* package.json */

{
  ...,
  "dependencies": {
    "babel-loader": "^6.2.4",
    "babel-preset-es2015": "^6.9.0",
    "babel-preset-react": "^6.11.1",
    "react": "^15.2.1",
    "react-dom": "^15.2.1",
    "webpack": "1.13.1"
  },
  "devDependencies": {
    "concurrently": "^2.2.0",
    "eslint": "^3.1.1",
    "eslint-plugin-react": "^5.2.2",
    "jest-cli": "^13.2.3",
    "react-addons-test-utils": "^15.2.1",
    "webpack-dev-server": "^1.14.1"
  },
  "scripts": {
    "build": "webpack -p",
    "dev": "webpack-dev-server --port 9999",
    "start": "npm run build && python -m SimpleHTTPServer 9999",
    "test": "jest --verbose --coverage --config jest.config.json"
  }
}

/* webpack.config.js */

var path    = require('path');
var webpack = require('webpack');

var BUILD_DIR  = path.resolve(__dirname, 'build/');
var SOURCE_DIR = path.resolve(__dirname, 'src/');

module.exports = {
  entry: SOURCE_DIR + '/index.jsx',
  output: {
    path: BUILD_DIR,
    filename: 'bundle.js'
  },
  resolve: {
    extensions: ['', '.js', '.jsx']
  },
  module: {
    loaders: [
      {
        test: /[^\.spec]+\.jsx$/,
        include: SOURCE_DIR,
        loader: 'babel'
      }
    ]
  }
};

/* .baberc */

{
  "presets": ["es2015", "react"]
}

/* src/index.jsx */

import React from 'react'
import ReactDOM from 'react-dom';

import Example from './Example'


ReactDOM.render(<Example />, document.getElementById('floor-plan'));
0

2 Answers 2

12

I got your setup working with these few modifications. I isolated the problem to your test property.

var path    = require('path');
var webpack = require('webpack');

var BUILD_DIR  = path.resolve(__dirname, 'build/');
var SOURCE_DIR = path.resolve(__dirname, 'src/');

module.exports = {
  entry: SOURCE_DIR + '/index.jsx',
  output: {
    path: BUILD_DIR,
    filename: 'bundle.js'
  },
  resolve: {
    extensions: ['.js', '.jsx']
  },
  module: {
    loaders: [
      {
        test: /\.jsx?$/,
        include: SOURCE_DIR,
        exclude: /node_modules/,
        loader: 'babel-loader',
        query: {
          presets: ['es2015', 'react']
        },
      }
    ]
  }
};
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot! This helped
configuration.resolve.extensions[0] should be an non-empty string.
4

The reason could be import Example from './Example' in index.jsx

webpack will treat this as js import instead of jsx.

try

import Example from './Example.jsx'

1 Comment

While this works, it is still undesirable to explicitly mention the file extension. The build-process should automagically handle such things

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.