Reputation: 95
I'm new to webpack 4/react/babel, and i am getting the error below:
ERROR in ./src/index.js 7:4
Module parse failed: Unexpected token (7:4)
You may need an appropriate loader to handle this file type.
|
| ReactDOM.render(
> <Hello compiler="TypeScript" framework="React" />,
| document.getElementById("example")
| );
i tried this soluction:link from stack overflow but also didn't work.
So i am stuck, the main difference is that i am using webpack 4 (remarks that i didn't made it work with webpack 3)
My enviroment is:
Below are the files with the relative path of them
./package.json
{
"name": "using-webpack",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "webpack"
},
"author": "",
"license": "ISC",
"dependencies": {
"react": "^16.4.1",
"react-dom": "^16.4.1"
},
"devDependencies": {
"babel-core": "^6.26.3",
"babel-loader": "^7.1.5",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.24.1",
"file-loader": "^1.1.11",
"webpack": "^4.16.1"
}
}
./.babelrc
{
"presets":[
"es2015", "react"
]
}
./webpack.config.js
module.exports = {
mode:"development",
entry: {
javascript: "./src/index.js",
html: "./src/index.html",
},
output: {
filename: "bundle.js",
path: __dirname + "/dist"
},
resolve: {
// Add '.ts' and '.tsx' as resolvable extensions.
extensions: [".js"]
},
module: {
rules: [
{
test:"/\.js$/",
exclude:"/node_modules/",
loader:"babel-loader",
},
]
},////
externals: {
"react": "React",
"react-dom": "ReactDOM"
}
};
./index.js
import * as React from "react";
import * as ReactDOM from "react-dom";
import { Hello } from "./components/hello";
ReactDOM.render(
<Hello compiler="TypeScript" framework="React" />,
document.getElementById("example")
);
./components/hello.js
import * as React from "react";
// 'HelloProps' describes the shape of props.
// State is never set so we use the '{}' type.
export class Hello extends React.Component {
render() {
return <h1>Hello from {this.props.compiler} and {this.props.framework}!</h1>;
}
}
Upvotes: 2
Views: 1080
Reputation: 2806
Your module section has plain strings in it when it should have regex instead.
Remove the quotes around your regex! :)
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
loader:"babel-loader",
},
]
},
Upvotes: 1