Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Module parse failed with webpack and react, even using babel-loader

Tags:

webpack

I am trying to use webpack with react but am getting this error:

ERROR in ./app/main.js

Module parse failed: /Users/me/app/main.js Line 2: Unexpected token
You may need an appropriate loader to handle this file type.
| //npm
| import React               from 'react';

This is the webpack config section of interest:

test: /\.jsx?$/,
        include: path.join(__dirname, 'src'),
        loader: 'babel-loader',
        query: {
            presets: ['es2015', 'react']

and the package.json:

"devDependencies": {
    "autoprefixer-loader": "^3.1.0",
    "babel-core": "^6.1.20",
    "babel-loader": "^6.1.0",
    "babel-preset-es2015": "^6.1.18",
    "babel-preset-react": "^6.1.18",
    "jest": "^0.1.40",
    "webpack": "^1.12.4",
    "webpack-dev-server": "^1.12.1",
    "css-loader": "^0.22.0",
    "style-loader": "^0.13.0"
  },
  "dependencies": {
    "babel-polyfill": "^6.1.19",
    "history": "^1.17.0",
    "react": "^0.14.2",
    "react-dom": "^0.14.2",
    "react-mdl": "^1.0.4",
    "react-router": "^1.0.2"
  }
like image 350
SuperUberDuper Avatar asked Feb 08 '23 05:02

SuperUberDuper


2 Answers

Looking at the path in the error message

Module parse failed: /Users/me/app/main.js Line 2: Unexpected token

it seems that the module is not inside the src folder. However, you explicitly specified that only modules inside path.join(__dirname, 'src') should passed through the babel loader.

Adjust include so that it will also include main.js.

like image 180
Felix Kling Avatar answered Mar 05 '23 15:03

Felix Kling


loaders: [{
        test: /\.jsx?$/,
        loader: 'babel',
        query: {
            presets: ['es2015', 'react']
        }
    }]

This is my config for the loader. One big difference in your config is the naming of the loader. Try 'babel' instead of 'babel-loader'. Next possible error could be your version. Update babel-loader (to 6.2.0).

like image 20
n3rdRick Avatar answered Mar 05 '23 15:03

n3rdRick