ReactJS module build error: SyntaxError: Unexpected token - ReactDOM.render
Why am I getting this error below? My claim ReactDOM.render(<App/>, container);
is 100% legal code.
My github repository: https://github.com/leongaban/react_starwars
App.js file
import react from 'react'
import ReactDom from 'react-dom'
import App from 'components/App'
require('index.html');
// Create app
const container = document.getElementById('app-container');
// Render app
ReactDOM.render(<App />, container);
Application components / file
import React from 'react'
const App = () =>
<div className='container'>
<div className='row'>
</div>
<div className='row'>
</div>
</div>;
export default App;
My webpack.config
const webpack = require('webpack');
const path = require('path');
module.exports = {
entry: [
'./src/app.js'
],
output: {
path: path.resolve(__dirname, './build'),
filename: 'app.bundle.js',
},
module: {
loaders: [
{
test: /\.html$/,
loader: 'file-loader?name=[name].[ext]',
},
{
test: /\.jsx?$/,
exclude: /node_modules/,
loader: 'babel-loader',
},
],
},
plugins: [
new webpack.NamedModulesPlugin(),
]
};
source to share
Use this as webpack config file
const webpack = require('webpack');
const path = require('path');
module.exports = {
entry: [
'./src/app.js'
],
output: {
path: path.resolve(__dirname, './build'),
filename: 'app.bundle.js',
},
module: {
loaders: [
{
test: /\.html$/,
loader: 'file-loader?name=[name].[ext]',
},
{
test: /\.jsx?$/,
exclude: /node_modules/,
loader: 'babel-loader',
query: {
presets: ['es2015', 'react']
}
},
],
},
plugins: [
new webpack.NamedModulesPlugin(),
]
};
You are missing presets
source to share
If anyone is working with CRA and get this error. Here's the solution goes.
Go to package.json
and add configuration babel
. If I don't seem so generous, below is the code
"babel": {
"presets": [
"react-app"
]
}
Leave the CRA setting unchanged. Actually CRA uses its own pre-installed react-app
and what is configured in the file webpack.config.js
. Hence, there is no need for other presets.
source to share