10

I am trying to configure webpack according to this tutorial and keep getting the same error. I am having trouble debugging these 2 messages:

ERROR in ./app.js
Module parse failed: /path/react/react-webpack-babel/app/app.js Line 1: Unexpected reserved word
You may need an appropriate loader to handle this file type.
| import React from "react";
| import Greeting from "./greeting";
|

ERROR in ./index.html
Module parse failed: /path/react/react-webpack-babel/app/index.html Line 1: Unexpected token <
You may need an appropriate loader to handle this file type.
| <!DOCTYPE html>
| <html>
|

Here is my webpack.configure.js

module.exports = {
  context: __dirname + '/app',
  entry: {
    javascript: "./app.js",
    html: "./index.html"
  },
  output: {
    filename: 'app.js',
    path: __dirname + '/dist'
  },
  loaders: [
    {
      test: /\.js$/,
      exclude: /node_modules/,
      loaders: ['babel-loader']
    },
    {
      test: /\.jsx$/,
      loaders: ['babel-loader']
    },
    {
      test: /\.html$/,
      loader: "file?name=[name].[ext]"
    }
  ]
}

Here is the react components

app/greeting.js

import React from "react/addons";

export default React.createClass({
  render: function() {
    return (
      <div className="greeting">
        Hello, {this.props.name}!
      </div>
    );
  },
});

app/app.js

import React from "react/addons";
import Greeting from "./greeting";

React.render(
  <Greeting name="World"/>,
  document.body
);

app/index.html

<!DOCTYPE html>
<html>

  <head>
    <meta charset="utf-8">
    <title>Webpack + React</title>
  </head>

  <body></body>

  <script src="app.js"></script>

</html>

In case it's helpful, here's my package.json with dependencies

{
  "name": "project",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "private": true,
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "Me",
  "license": "ISC",
  "devDependencies": {
    "babel-core": "^5.8.22",
    "babel-loader": "^5.3.2",
    "file-loader": "^0.8.4",
    "webpack": "^1.11.0"
  },
  "dependencies": {
    "react": "^0.13.3"
  }
}

2 Answers 2

17

the loaders option should be nested in a module object like so:

module.exports = {
  context: __dirname + '/app',
  entry: {
    javascript: "./app.js",
    html: "./index.html"
  },
  output: {
    filename: 'app.js',
    path: __dirname + '/dist'
  },
  module: {
    loaders: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        loaders: ['babel-loader']
      },
      {
        test: /\.jsx$/,
        loaders: ['babel-loader']
      },
      {
        test: /\.html$/,
        loader: "file?name=[name].[ext]"
      }
    ]
  }
};

I also added a missing semi-colon at the end ;)

Sign up to request clarification or add additional context in comments.

6 Comments

Thank you so much! I've been trying to understand how the hot module replacement works with webpack but was stuck on this part. Thanks!!!
I've added the loaders in its correct position as per your answer, also added the semi-color, I'm still getting the same error, do you know if it could be due to any other reason?
@AbhinavSingi did you npm install the loaders? e.g. npm install --save-dev babel-loader file-loader or just npm install with Mdd's package.json?
@Almouro I did npm install . But if I specify the version no. in the former method - both should yield same results, right?
@Almouro apparently it's working fine now, I was require('webpack') in webpack.config.js which was causing some issues.
|
1

When I was importing with the syntax

import Component from './components/component';

I was getting the module parse error. To fix it, I had to specify .jsx and it worked

import Component from `./components/component.jsx`. 

It wasn't a config error at all. I'm on babel 6 with hot loader.

Comments

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.