1

TL;DR: Why am I getting the error with my code? Previous question is different (online vs desktop) and it's answers don't work for me.

Complete code here

Based on code more or less originating here (I'm not quite to the end of the "lesson"

Question: Following this "intro to ReactJS". The walkthrough has Webpack/Babel setup. It runs with plain JS, but when I switch to JSX it chokes. This is similar to this question, but none of those answers seem to work. Main difference: Web Playground vs locally on my box?

The end of the video I'm working on leads to this code - although, I'm only 3/4 of the way through so parts aren't included yet. So, I've dialed it back into this fork with my edits (Sorry if I've butchered forking and pushing my changes...)

Notes: The BEFORE and AFTER is the only things I've changed. It works with javascript/jquery - but not with JSX. I found a couple typos, case errors (thisItem vs thisitem) and some items that shouldn't have been there (brackets removed).

I've changed "my" typed out version to more closely match "their" version (Hello instead of HelloWorld) and made other minor changes... same error.

The biggest remaining changes I see other than some spacing issues is versions - minor version bumps from the recorded class.

My Code:

.babelrc

{ "presets": [ "react" ] }

package.json

 {
  "name": "github-battle",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "webpack-dev-server",
    "production": "webpack -p"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "react": "^0.14.7",
    "react-dom": "^0.14.7"
  },
  "devDependencies": {
    "babel-core": "^6.6.0",
    "babel-loader": "^6.2.4",
    "babel-preset-react": "^6.5.0",
    "html-webpack-plugin": "^2.9.0",
    "webpack": "^1.12.14",
    "webpack-dev-server": "^1.14.1"
  }
}

webpack.config.js

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$/,include: __dirname + '/app',loader: "babel-loader" }
        ]
    },
    plugins: [HtmlWebpackPluginConfig]
}

app\index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Github Battle</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" >
</head>
<body>
<div id="app"></div>
</body>
</html>

app\index.js BEFORE

var app = document.getElementbyid('app')
app.innerHTML = 'Hello

app\index.js AFTER

var React = require('react');
var ReactDOM = require('react-dom');

var HelloWorld = React.createClass({
    render: function () {
        return <div> Hello World </div>
    }
});

ReactDOM.render(
    <HelloWorld />,
    document.getElementById('app')
);

Result:

> B:\Users\Chris\react-js\React-Fundamentals>npm run production
> gh-battle@1.0.0 production B:\Users\Chris\react-js\React-Fundamentals
> webpack -p

Hash: 21e367e251c35209471c
Version: webpack 1.12.14
Time: 375ms
          Asset       Size  Chunks             Chunk Names
index_bundle.js  289 bytes       0  [emitted]  main
     index.html  305 bytes          [emitted]
   [0] multi main 28 bytes {0} [built] [1 error]
   [1] ./app/index.js 0 bytes [built] [failed]

ERROR in ./app/index.js
Module parse failed: B:\Users\Chris\react-js\React-Fundamentals\app\index.js Line 6: Unexpected token <
You may need an appropriate loader to handle this file type.
| var Hello = React.createClass({
|   render: function () {
|     return <div> Hello ReactJS World! </div>
|   }
| });
 @ multi main
Child html-webpack-plugin for "index.html":
        + 3 hidden modules

B:\Users\Chris\react-js\React-Fundamentals>

webpack.config.js #2: Same rror

var HtmlWebpackPlugin = require('html-webpack-plugin');
...
module.exports = {
  ...
  module: {
    loaders: [
      {test: /\.js$/, include: __dirname + '/app', loader: "babel-loader"}
    ],
    query: {
      presets: ['react']
    }
  },
  plugins: [HTMLWebpackPluginConfig]
};
Community
  • 1
  • 1
WernerCD
  • 2,137
  • 6
  • 31
  • 51
  • Have you tried to add `query: { presets: ['react']}`? – The Reason Mar 02 '16 at 19:28
  • @The for `.babelrc`? As in put query: before the initial bracket? Doesn't seem to be in any of the git repo or in any of the examples listed [here](https://babeljs.io/blog/2015/10/31/setting-up-babel-6) - and fiddling with quotes/no quotes-single/double quotes leaves the same error. – WernerCD Mar 02 '16 at 19:46
  • you can also add it into your `webpack.config.js` file like here [gitrepo](https://github.com/babel/babel-loader) – The Reason Mar 02 '16 at 20:06
  • shouldn't index.js be index.jsx? – Daniel A. White Mar 02 '16 at 20:17
  • @The deleted `.babelrc`, moved contents to `query:`, same error. Also, downgraded npm as suggested [here](https://github.com/foreverjs/forever/issues/788) for an unrelated(?) error, no change. Updated it back to 3.8. Changed file to jsx (and related settings), same issue. – WernerCD Mar 02 '16 at 20:29
  • @WernerCD `query` should be included into loader like here -> `loader: 'babe-loader', query:{blabla}`, if it also doesnt help you them try to remove `include` options from your config file – The Reason Mar 02 '16 at 20:41
  • @WernerCD or even try to use `path.join(__dirname, 'app')`, maybe that's why webpack can't find a loader – The Reason Mar 02 '16 at 20:47
  • I copied your code and `package.json` on my machine and did a `npm install` and `npm run production` and no errors at all. Why don't you try remove your `node_modules` and reinstall the dependencies? – r0dney Mar 02 '16 at 21:44
  • Second computer (Work vs home) pulled from , same issue. JS or JSX (per Daniel). `.bablerc` or `query:` in webpack (per The). npm@3.8. I've switched my repo to [here](https://github.com/WernerCD/ReactJS) to stop any confusion with the "class" js. – WernerCD Mar 02 '16 at 23:11
  • @The `include: path.join(__dirname, 'app)` in webpack created the error in the log file located in the [updated repo](https://github.com/WernerCD/ReactJS) in [this log file](https://github.com/WernerCD/ReactJS/blob/master/npm-debug.1.log) – WernerCD Mar 02 '16 at 23:34
  • What are the odds that this is a Windows issue? – WernerCD Mar 03 '16 at 03:18
  • @The I think... I may have solved it... your suggestion of path.join didn't work because *drum roll* I didn't have require('path') at the top. Combination of 'tard and just not crossing my mind. Other suggestions elsewhere of path.resolve(...) didn't work and I noticed the missing require... – WernerCD Mar 03 '16 at 14:38
  • @WernerCD of course you should `require('path')` before using it. Did it help you? – The Reason Mar 03 '16 at 16:06
  • @The "Of Course" is a bit self evident now :) I'm very rusty on the javascript so it didn't occur to me. Lesson learned. I put an answer down below with my final script (jsx and path.join()) that now works. – WernerCD Mar 03 '16 at 16:12

4 Answers4

1

You need to rename index.js to index.jsx.

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
  • I don't think that matters... because you tel the app where to the entry point is, the loader to transform it and the output... All the samples in the class I'm trying to work through so far have been js. – WernerCD Mar 02 '16 at 20:31
  • While I don't think it matters to the process, I think I am going to move to jsx extension... I like the differentiation that the different extension gives. – WernerCD Mar 03 '16 at 14:41
1

silly question: have you tried removing the square brackets in the "entry" declaration?

module.exports = {
    entry: './app/index.js',
    output: {
        path: __dirname + '/dist',
        filename: "index_bundle.js"
    },
    module: {
        loaders:[
            {
                test: /\.js$/,
                include: __dirname + '/app',
                loader: "babel-loader"
            }
        ]
    },
    plugins: [HtmlWebpackPluginConfig]
}
JulioCT
  • 1,057
  • 8
  • 7
  • Same issue with it as a "string" or as an "array of strings". The "class" has it like that because at "some point soon" we will use multiple things there. – WernerCD Mar 02 '16 at 23:35
1

From your second webpack config file, query should be inside the babel loader object.

module: {
  loaders: [
    {
      test: /\.js$/, 
      include: __dirname + '/app', 
      loader: "babel-loader",
      query: {
        presets: ['react']
      } 
    }
  ]
}

Don't forget to install the babel-preset-es2015 plugin if you plan on using es6.

Eni Arinde
  • 555
  • 5
  • 6
1

Working Code here

After banging my head against the wall (which, honestly, helps beat the knowledge in - so it isn't all for naught)... I've made a couple minor changes and seem to be successful now:

  1. changed .js to .jsx - I like the "explicit" acknowledgement that these aren't plain ole js. (not required I think, more style)
  2. I've removed the babelrc file and moved query into webpack.config... seems to be easier to compartmentalize things in a single file. this actually isn't working for me... se'la'vie
  3. The "solution" seems to be the __dirname + '/dir' changed into path.join(...) - with var path = require('path') actually included. I'll research (and ask a new question if I don't find one) how/why those two aren't equal, but I can only assume it has something to do with differing operating systems (Windows 10x64 for me).

EDIT:: Just some random poking, but include:__dirname + 'app', fails... as does '\app', '\app\', '/app', '/app/'... no clue why, but path.join(...) works.

Also worth noting, is that template: __dirname + '...', seems to work, but not the parts below it. Filename vs directory, so again not sure of the difference.

webpack.config.js

var path = require('path');

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.jsx'
  ],
  output: {
    path: path.join(__dirname, '/dist'),
    filename: "index_bundle.js"
  },
  module: {
    loaders: [{
        test: /\.jsx?$/, 
        include: path.join(__dirname, '/app'), 
        loader: "babel-loader"},
    ]
  },
  plugins: [HTMLWebpackPluginConfig]
};

index.jsx

var React = require('react');
var ReactDOM = require('react-dom');

var HelloWorld = React.createClass({
  render: function () {
    return <div> Hello ReactJS World! </div>
  }
});

ReactDOM.render(
    <HelloWorld />,
    document.getElementById('app')
);

index.html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Github Battle</title>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
</head>
<body>
  <div id="app"></div>
  <script src="index_bundle.js"></script></body>
</html>
WernerCD
  • 2,137
  • 6
  • 31
  • 51