babel-loader icon indicating copy to clipboard operation
babel-loader copied to clipboard

React - Module parse failed: You may need an appropriate loader to handle this file type.

Open westdavidr opened this issue 11 years ago • 71 comments

I've been banging my head against the wall all day trying to figure this one out.

ERROR in ./app/assets/frontend/main.jsx
Module parse failed: /Users/david/work/sd/sd-web/app/assets/frontend/main.jsx Line 4: Unexpected token <
You may need an appropriate loader to handle this file type.
|   render() {
|     return (
|       <h1>Hello, World</h1>
|     );
|   }

Here's my relevant package.json:

"devDependencies": {
  "babel-core": "^6.2.1",
  "babel-loader": "^6.2.0",
  "babel-preset-es2015": "^6.1.18",
  "babel-preset-react": "^6.1.18",
  "webpack": "^1.12.9"
}

and web pack.config.js:

module.exports = {
  entry: './app/assets/frontend/main.jsx',
  output: {
    path: __dirname + '/app/assets/javascripts',
    filename: 'bundle.js',
    resolve: {
      extensions: ['', '.js', '.jsx']
    },
    module: {
      loaders: [
        {
          test: /\.jsx?$/,
          loader: 'babel',
          exclude: /node_modules/,
          query: {
            cacheDirectory: true,
            presets: ['react', 'es2015']
          }
        }
      ]
    }
  }
}

and then running webpack from the command line.

Am I missing something obvious here?

westdavidr avatar Dec 03 '15 03:12 westdavidr

I should add that if I run:

babel app/assets/frontend/main.jsx --presets es2015,react

from the command line, it parses correctly, which led me to believe it was an issue with babel-loader and/or webpack.

westdavidr avatar Dec 03 '15 03:12 westdavidr

i am having a similar issue with web pack and babel

epicallan avatar Dec 06 '15 14:12 epicallan

@westdavidr Your resolve and module properties are incorrectly enclosed in the output property. Try this:

module.exports = {
  entry: './app/assets/frontend/main.jsx',
  output: {
    path: __dirname + '/app/assets/javascripts',
    filename: 'bundle.js'
  },
  resolve: {
    extensions: ['', '.js', '.jsx']
  },
  module: {
    loaders: [
      {
        test: /\.jsx?$/,
        loader: 'babel',
        exclude: /node_modules/,
        query: {
          cacheDirectory: true,
          presets: ['react', 'es2015']
        }
      }
    ]
  }
}

stevenross avatar Dec 08 '15 00:12 stevenross

@stevenross You are correct. I'm not sure if I should bang my head harder for such a simple mistake, or thank you for the answer. Haha.

Thanks!

westdavidr avatar Dec 08 '15 16:12 westdavidr

This should help for those who're still looking for solution:

My webpack.config.js file with working hot reloading

require('webpack');

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

aseem2625 avatar Mar 06 '16 22:03 aseem2625

Hi

I am start learning react and i am facing similar type of issue.

varunkot avatar Sep 04 '16 17:09 varunkot

web.config.js file

module.exports = { entry: [ './src/App.js' ], output: { path: __dirname, filename: 'app-min.js' }, resolve:{ extensions: ['','.js','.jsx'] }, module: { loader: [ { test: /.jsx?$/, loader: 'babel', exclude: /node_modules/, query: { presets: ['react','es2015'] } } ] } } App.js

import React from 'react'; class App extends React.Component { render(){ return (

Contact list

) } }

error in console : app-min.js:47 Uncaught Error: Cannot find module "./src/App.js"

varunkot avatar Sep 04 '16 17:09 varunkot

Hey @varunkot To indent/organize your comment well for code snippets try putting your code inside


aseem2625 avatar Sep 05 '16 15:09 aseem2625

I am also facing same issue today.

webpack.config.js

module.exports = {
  entry: "./public/main.js",
  output: {
    path: __dirname + "/public",
    filename: "/bundle.js"
  },
  module: {
    loader: [{
        test: /\.jsx?$/,
        exclude: /node_modules/,
        loader: 'babel',
        query: {
          presets : ['es2015', 'react']
        }
    }]
  },
  watch: true
}

main.js

import React from 'react';
import ReactDOM from 'react-dom';
import App from './App.jsx';

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

App.js

import React from 'react';

class App extends React.Component {
  render() {
      return (
         <div>
            <h2>Content</h2>
            <p>The content text!!!</p>
         </div>
      );
    }
}

export default App;

index.ejs within views folder

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Express React Redux</title>
    <link rel="stylesheet" href="../css/mystyles.css">
  </head>
  <body>

    <h1>Index Page</h1>
    <div id="app">

    </div>
    <script type="text/javascript" src="../bundle.js">

  </body>
</html>

Error Window

error_screen

package.json

{
  "name": "express_react_redux",
  "version": "1.0.0",
  "description": "simple react redux expressjs app",
  "main": "index.js",
  "scripts": {
    "webpack": "webpack",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "babel": "^6.5.2",
    "babel-cli": "^6.22.2",
    "babel-core": "^6.22.1",
    "babel-loader": "^6.2.10",
    "babel-preset-es2015": "^6.22.0",
    "babel-preset-react": "^6.22.0",
    "ejs": "^2.5.5",
    "express": "^4.14.0",
    "pug": "^2.0.0-beta7",
    "react": "^15.4.2",
    "react-dom": "^15.4.2",
    "react-redux": "^5.0.2",
    "redux": "^3.6.0",
    "webpack": "^1.14.0"
  }
}

zeexan-dev avatar Jan 24 '17 17:01 zeexan-dev

In webpack.config.js, you miss s in module: { loaderS. At least this was my issue with Webpack 2.

mpazitny avatar Feb 15 '17 14:02 mpazitny

I have the same problem My webpack.config.js looks like below

'use strict';
var path = require('path');
var webpack = require('webpack');

module.exports = {
  entry: './main.js',
  output: { path: __dirname, filename: 'bundle.js' },
  module: {
    loaders: [
      {
        test: /\.jsx?$/,
        loader: 'babel-loader',
        exclude: /node_modules/,
        query: {
          presets: ['es2015', 'react']
        }
      }
    ]
  },
};

here is my package.json file

{
  "name": "samplereact",
  "version": "1.0.0",
  "description": "test project",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "Sreedhar Talakokkula",
  "license": "ISC",
  "dependencies": {
    "babel-polyfill": "6.23.0",
    "babel-runtime": "6.23.0",
    "react": "0.14.7",
    "react-dom": "0.14.7"
  },
  "devDependencies": {
    "babel-core": "6.4.5",
    "babel-loader": "^6.3.2",
    "babel-plugin-transform-runtime": "6.23.0",
    "babel-preset-es2015": "^6.22.0",
    "babel-preset-react": "^6.23.0",
    "webpack": "1.12.12"
  }
}

My main.js file

import Hello from './Hello.JSX';
import World from './World.JSX';

i get the following error

webpack-dev-server --progress --colors
http://localhost:8080/webpack-dev-server/
webpack result is served from /
content is served from C:\Learning\Projects\React
loaderUtils.parseQuery() received a non-string value which can be problematic, see https://github.com/webpack/loader-utils/issues/56
parseQuery() will be replaced with getOptions() in the next major version of loader-utils.
Hash: 6d152ff83dc0a532b2cd
Version: webpack 2.2.1
Time: 15299ms
    Asset     Size  Chunks             Chunk Names
bundle.js  3.47 kB       0  [emitted]  main
chunk    {0} bundle.js (main) 826 bytes [entry] [rendered]
    [0] ./Hello.JSX 273 bytes {0} [built] [failed] [1 error]
    [1] ./World.JSX 273 bytes {0} [built] [failed] [1 error]
    [2] ./main.js 280 bytes {0} [built]

ERROR in ./Hello.JSX
Module parse failed: C:\Learning\Projects\React\Hello.JSX Unexpected token (6:9)
You may need an appropriate loader to handle this file type.
| class Hello extends React.Component {
|       render(){
|               return <h1>Hello</h1>
|       }
| }
 @ ./main.js 3:13-35

ERROR in ./World.JSX
Module parse failed: C:\Learning\Projects\React\World.JSX Unexpected token (6:9)
You may need an appropriate loader to handle this file type.
| class World extends React.Component {
|       render(){
|               return <h1>World</h1>
|       }
| }

stalakokkula avatar Feb 28 '17 18:02 stalakokkula

@stalakokkula Your test regex is for lower-case .jsx but your files are .JSX it looks like.

loganfsmyth avatar Feb 28 '17 18:02 loganfsmyth

Hi, @stalakokkula, I got the same error and still not sure about the reason. I'm on node v7.7.2

Version: webpack 2.2.1
Time: 64ms
    Asset     Size  Chunks             Chunk Names
bundle.js  3.27 kB       0  [emitted]  main
   [0] ../src/client/app/index.jsx 289 bytes {0} [built] [failed] [1 error]

ERROR in ../src/client/app/index.jsx
Module parse failed: /RU/src/client/app/index.jsx Unexpected token (6:15)
You may need an appropriate loader to handle this file type.
| class App extends React.Component {
|     render() {
|         return <p>Hello World</p>;
|     }
| }

My index.jsx

import React from 'react';
import { render } from 'react-dom';

class App extends React.Component {
    render() {
        return <p>Hello World</p>;
    }
}

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

My webpack-config.js

var webpack = require('webpack');
var path = require('path');

var BUILD_DIR = path.resolve(__dirname, './../src/client/public');
var APP_DIR = path.resolve(__dirname, './../src/client/app');

var config = {
    entry: APP_DIR + '/index.jsx',
    output: {
        path: BUILD_DIR,
        filename: 'bundle.js'
    },
    resolve: {
        extensions: ['.js', '.jsx']
    },
    module: {
        loaders: [
            {
                test: /\.jsx?$ /,
                include: APP_DIR,
                loader: 'babel-loader',
                query: {
                    presets: ['es2015', 'react']
                }
            }
        ]
    }
};

module.exports = config;

pbarrientos avatar Mar 20 '17 20:03 pbarrientos

Your test regex is broken: /\.jsx?$ / has an extra space at the end.

loganfsmyth avatar Mar 20 '17 21:03 loganfsmyth

Oh! Thanks, @loganfsmyth! I corrected the extra space and part of the error is gone :)

Version: webpack 2.2.1
Time: 409ms
    Asset     Size  Chunks             Chunk Names
bundle.js  3.86 kB       0  [emitted]  main
   [0] ../src/client/app/index.jsx 818 bytes {0} [built] [failed] [1 error]

ERROR in ../src/client/app/index.jsx
Module build failed: SyntaxError: Unexpected token (6:15)

  4 | class App extends React.Component {
  5 |     render() {
> 6 |         return <p>Hello World</p>;
    |                ^
  7 |     }
  8 | }
  9 |

pbarrientos avatar Mar 20 '17 21:03 pbarrientos

If you post a repository that reproduces the issue we can take a look, but nothing jumps out at me that would be wrong.

loganfsmyth avatar Mar 20 '17 23:03 loganfsmyth

I had same issue today while I was experimenting with Redux tutorial from its site. I was using my custom directory structure.

functional-programming/ functional-programming/src/ functional-programming/components/ functional-programming/containers/

my index file was in

functional-programming/src/index.js

and that file was referring components

so I got error message that I need appropriate loader

then I've moved my components and containers folders into /src folder and everything begin to work okay for me.

P.S: I was using create-react-app

rubyonrails3 avatar Mar 28 '17 21:03 rubyonrails3

I am just starting out with react and have been facing similar issue

ERROR in ./dev/index.jsx Module parse failed: /Users/imran/Desktop/MyTotallyAwesomeApp/dev/index.jsx Unexpected token (7:6) You may need an appropriate loader to handle this file type. | render: function() { | return ( |

Hello, {this.props.greetTarget}!

| ); | }

package.json:

{ "name": "mytotallyawesomeapp", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "echo "Error: no test specified" && exit 1" }, "author": "", "license": "ISC", "dependencies": { "babel-core": "^6.24.0", "babel-loader": "^6.4.1", "babel-preset-es2015": "^6.24.0", "babel-preset-react": "^6.23.0", "react": "^15.4.2", "react-dom": "^15.4.2", "webpack": "^2.3.2" }, "babel": { "presets": [ "es2015", "react" ] } }

web pack.config.js:

var webpack = require("webpack"); var path = require("path");

var DEV = path.resolve(__dirname, "dev"); var OUTPUT = path.resolve(__dirname, "output");

var config = { entry: DEV + "/index.jsx", output: { path: OUTPUT, filename: "myCode.js" } }; module: { loaders: [{ include: DEV, loader: "babel-loader", }] }

module.exports = config;

where is the issue here?

nidaibani21 avatar Apr 01 '17 09:04 nidaibani21

Hi again! @loganfsmyth Sorry for the delay, here is a repo with an example that reproduces the error. https://github.com/pbarrientos/new-react

I'm using node v7.8.0 and npm 4.2.0 After installing the packages I run: ./node_modules/.bin/webpack -d

This is what I get (now on webpack 2.3.2 same error): Module build failed: SyntaxError: Unexpected token (6:15)

Thanks in advance.

pbarrientos avatar Apr 02 '17 21:04 pbarrientos

@pbarrientos I might be wrong, but I think the problem is not with webpack nor babel-loader, but with babel itself due to the way babel does lookup behaviour.

See this for more information https://babeljs.io/docs/usage/babelrc/#lookup-behavior

I could be wrong though.

couto avatar Apr 03 '17 00:04 couto