1

Hi I was going through this tutorial and I have setup my webpack configuration based on this tutorial.

Regardless I have the following file index.js

import React from 'react'
import { render } from 'react-dom'
import { Provider } from 'react-redux'
import { createStore } from 'redux'
import todoApp from './reducers'
import AppComp from './components/App' 

let store = createStore(todoApp)

let App = React.createClass({
  render: () => {
    return (
      <Provider store={store}>
        <AppComp />
      </Provider>
    )
  }
});

render(
  <App/>,
  document.getElementById('app')
)

And my webpack configuration is

var HtmlWebpackPlugin = require ('html-webpack-plugin');
var HtmlWebpackPluginConfig = new HtmlWebpackPlugin({
    template: __dirname + '/app/index.html',
    filename: 'index.html',
    inject: 'body'
});

module.exports = {
    entry: [
        './app/index.js'
    ],
    output: {
        path: __dirname + '/dist',
        filename: 'index_build.js'
    },

    module: {
        loaders: [
            { 
                test: /\.js$/, exclude: /node_modules/, loader: 'babel-loader', presets: ['es2015', 'react']  
            },
        ]
    },
    plugins: [HtmlWebpackPluginConfig]
};

When I run the app with webpack, I got the following error

ERROR in ./app/index.js
Module build failed: SyntaxError: Unexpected token (13:6)

  11 |   render: () => {
  12 |     return (
> 13 |       <Provider store={store}>
     |       ^
  14 |         <App />
  15 |       </Provider>
  16 |     )

Anyone can help me with resolving this error?

Edit: The issue was the way I defined my webpack configs, the presets should be inside query block. Here is my updated webpack config file

var HtmlWebpackPlugin = require('html-webpack-plugin')
var HTMLWebpackPluginConfig = new HtmlWebpackPlugin({
  template: __dirname + '/app/index.html',
  filename: 'index.html',
  inject: 'body'
});

module.exports = {
  entry: [
    './app/index.js'
  ],
  output: {
    path: __dirname + '/dist',
    filename: "index_bundle.js"
  },
  module: {
    loaders: [
      { 
          test: /\.js$/, 
          exclude: /node_modules/, 
          loader: 'babel-loader',
          query: {
            presets: ['react', 'babel-preset-react']
          } 
      }
    ]
  },
  plugins: [HTMLWebpackPluginConfig]
};
4
  • That error message indicates babel-preset-react not found... I'll suggest you include a .babelrc file in your root directory with { "presets": ["babel-preset-es2015", "babel-preset-react"] } Commented Aug 29, 2016 at 12:36
  • Tried but it didn't work. Commented Aug 29, 2016 at 12:50
  • Do you have babel-preset-react installed?/ Commented Aug 29, 2016 at 12:53
  • yes. The version is 6.11.1 Commented Aug 29, 2016 at 13:07

2 Answers 2

1

You must specify babel presets. You can use .babelrc

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

or you can specify it in your loader query:

loaders: [ 'babel?presets[]=react,presets[]=es2015' ]
Sign up to request clarification or add additional context in comments.

Comments

0

You need to use npm module 'path' for defining path. Here is the webpack.config.js file

    var HtmlWebpackPlugin = require ('html-webpack-plugin');
    var path = require('path');
    var HtmlWebpackPluginConfig = new HtmlWebpackPlugin({
        template: __dirname + '/app/index.html',
        filename: 'index.html',
        inject: 'body'
    });

    module.exports = {
        entry: [
            './app/index.js'
        ],
        output: {
            path: path.resolve(__dirname, 'dist/'),
            filename: 'index_build.js'
        },

        module: {
            loaders: [
                { 
                    test: /\.js$/, include: path.join(__dirname,'/app'), loader: 'babel-loader', presets: ['es2015', 'react']  
                },
            ]
        },
        plugins: [HtmlWebpackPluginConfig]
   };

5 Comments

That is not correct. You can see in my config file I am using concatenation instead of path. path: path.join(__dirname,'/dist') would be equivalent to path: __dirname + '/dist'
string concatenation will not work in windows as __dirname have restrictions there. you can check running that in windows. i have faced similar problem before. let me have a look again. i can see there are many string concatenations in the webpack.config.js file. you chould change.
I don't use windows.
here is webpack.config.js file. you can have a look that is working for react-redux. github.com/OnlyRefat/react-redux-webpack/blob/master/…
As I said it was not due to path. May be you only need to use path when you are in windows.

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.