babel-loader icon indicating copy to clipboard operation
babel-loader copied to clipboard

Error when compiling React JSX using ES6

Open stalakokkula opened this issue 9 years ago • 2 comments

I have the same problem My webpack.config.js looks like below

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

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

here is my package.json file

{
  "name": "samplereact",
  "version": "1.0.0",
  "description": "test project",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "Sreedhar Talakokkula",
  "license": "ISC",
  "dependencies": {
    "babel-polyfill": "6.23.0",
    "babel-runtime": "6.23.0",
    "react": "0.14.7",
    "react-dom": "0.14.7"
  },
  "devDependencies": {
    "babel-core": "6.4.5",
    "babel-loader": "^6.3.2",
    "babel-plugin-transform-runtime": "6.23.0",
    "babel-preset-es2015": "^6.22.0",
    "babel-preset-react": "^6.23.0",
    "webpack": "1.12.12"
  }
}

My main.js file

import Hello from './Hello.JSX';
import World from './World.JSX';

i get the following error

webpack-dev-server --progress --colors
http://localhost:8080/webpack-dev-server/
webpack result is served from /
content is served from C:\Learning\Projects\React
loaderUtils.parseQuery() received a non-string value which can be problematic, see https://github.com/webpack/loader-utils/issues/56
parseQuery() will be replaced with getOptions() in the next major version of loader-utils.
Hash: 6d152ff83dc0a532b2cd
Version: webpack 2.2.1
Time: 15299ms
    Asset     Size  Chunks             Chunk Names
bundle.js  3.47 kB       0  [emitted]  main
chunk    {0} bundle.js (main) 826 bytes [entry] [rendered]
    [0] ./Hello.JSX 273 bytes {0} [built] [failed] [1 error]
    [1] ./World.JSX 273 bytes {0} [built] [failed] [1 error]
    [2] ./main.js 280 bytes {0} [built]

ERROR in ./Hello.JSX
Module parse failed: C:\Learning\Projects\React\Hello.JSX Unexpected token (6:9)
You may need an appropriate loader to handle this file type.
| class Hello extends React.Component {
|       render(){
|               return <h1>Hello</h1>
|       }
| }
 @ ./main.js 3:13-35

ERROR in ./World.JSX
Module parse failed: C:\Learning\Projects\React\World.JSX Unexpected token (6:9)
You may need an appropriate loader to handle this file type.
| class World extends React.Component {
|       render(){
|               return <h1>World</h1>
|       }
| }

stalakokkula avatar Feb 28 '17 18:02 stalakokkula

@stalakokkula your loader config test regex doesn't match "./Hello.JSX", so either change it to:

import Hello from './Hello.jsx';

or the loader config test to be case insensitive:

{
  "test": /\.jsx?$/i,
  "loader": "babel-loader",
  "exclude": /node_modules/,
  "query": {
    "presets": ["es2015", "react"]
  }
}

existentialism avatar Mar 01 '17 17:03 existentialism

Thanks @existentialism. Changing the loader config to

import Hello from './Hello.jsx';

did the trick. Thank you for getting back to me.

stalakokkula avatar Mar 02 '17 17:03 stalakokkula