babel-loader: React - Module parse failed: You may need an appropriate loader to handle this file type.
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?
About this issue
- Original URL
- State: closed
- Created 9 years ago
- Reactions: 177
- Comments: 71 (8 by maintainers)
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 ( <div>
Contact list
</div> ) } }error in console : app-min.js:47 Uncaught Error: Cannot find module “./src/App.js”
Hey @varunkot To indent/organize your comment well for code snippets try putting your code inside
@westdavidr Your resolve and module properties are incorrectly enclosed in the output property. Try this:
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?
@MrRahulR Downvoted for not acknowledging your improper editing, secretly editing your post and writing anti to it! Now, I’m totally ignoring your query.
This should help for those who’re still looking for solution:
My webpack.config.js file with working hot reloading
@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!
Hi
I am start learning react and i am facing similar type of issue.
I am also facing same issue today.
webpack.config.js
main.js
App.js
index.ejs within views folder
Error Window
package.json
For me it was something very silly. In my webpack.config.js I had the following line: exclude: /node_modules/ One of the parents of my working folder was node_modules e.g. C:\React\node_modules\MyProject
As soon as I changed the folder structure removing node_modules from the parent folder, everything worked fine.
Come on be friendly here. If you want to fight go somewhere else.
@MrRahulR As you are using webpack 3 your config is outdated. Read this migration guide which should cover all the cases you have. https://webpack.js.org/guides/migrating/#module-loaders-is-now-module-rules & https://webpack.js.org/guides/migrating/#what-are-options- Or have a look at our README in the usage section to see proper working configs: https://github.com/babel/babel-loader/#usage
My mistake was I didn’t add the babel plugin that transforms
jsx
.So for
React
, you would install the@babel/preset-react
, then in your.babelrc
:For
Preact
, install@babel/plugin-transform-react-jsx
, then in your.babelrc
:Hope that helps someone!
@MrRahulR even though you could be new to Git, you can check other people’s formatting of code above and google it put code on git! Disappointed! Personally, I’m passing your query.
I had same issue today while I was experimenting with Redux tutorial from its site. I was using my custom directory structure.
my index file was in
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
I’m working on a react code and it showing the similar error,
package.json
webpack.config.js
It display error as below.
Try removing the line
exclude: /node_modules/
in your loaders. That worked for my similarModule parse failed:
issue.Although this issue is closed, it is easily found via google, so adding my solution in case it helps.
For me babel was configured in
package.json
and theenv.production.only
key was too restrictive after a refactor.Updating to e.g.
Solved my problem. This may apply to you even if your babel options are configured elsewhere.
@shubhamk54 use not query, use options. my working:
And your
.babelrc
is inC:\Users\ujjwa\Desktop\app\.babelrc
? You should check to make sure you don’t have any other.babelrc
files that might be confusing things.Solutions listed here didn’t help. I did the following and it worked. .babelrc -->
{ "presets": [ "react", "env", "stage-0" ] }
package.json -->Doing npm run build produces the following: -->
The files are here. P.S: I am excluding the react and react-dom from my bundle because I don’t want to create version inconsistency when users do ‘npm install --save <my app>’ on my package. Hope this helps.
In my case, this happens when using Lerna & Yarn workspaces with multiple CRA Apps.
I’m facing a similar error. Still not able to fix it
Error:
Webpack Config:
The entire project is available at React-Redux-BoilerPlate
In
webpack.config.js
, you miss s inmodule: { loaderS
. At least this was my issue with Webpack 2.I should add that if I run:
from the command line, it parses correctly, which led me to believe it was an issue with babel-loader and/or webpack.
Hi there,
I’m also facing similar problems to OP but the solutions listed here didn’t help me either.
The errors look like the below:
webpack.config.js:
package.json
Firstly make sure it picks the right config file: “build”: “./node_modules/.bin/webpack --mode production --config ./webpack/webpack.config.js”.
Then make sure you use the right presets in .babelrc
{ "presets": ["@babel/preset-env", "@babel/preset-react"] }
Then make sure you have the right loader:
{ test: /\.(js|jsx)?$/, exclude: /node_modules/, use: ["babel-loader"], }
This is for webpack version:
“webpack”: “^4.29.6”, “webpack-cli”: “^3.3.0”, “webpack-dev-server”: “^3.2.1”
I have the same error above. Here is my wepack.config, I am still figuring out whats wrong in this.
`var webpack = require(‘webpack’); var path = require(‘path’);
var BUILD_DIR = path.resolve(__dirname, ‘C:\Users\DELL\Sample\src\client\public’); var APP_DIR = path.resolve(__dirname, ‘C:\Users\DELL\Sample\src\client\app’);
var config = { entry: APP_DIR + ‘/index.jsx’, output: { path: BUILD_DIR, filename: ‘bundle.js’ } }; module : { loaders : [ { test : /.jsx?/, include : APP_DIR, loader : ‘babel-loader’, query: { presets: [‘es2015’] } } ] };
module.exports = config; `
@aseem2625 Everyone has dropped their code snippets above, I saw package.json and webpack.config.js I did same!