Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Babel throws on JSX files when preset-react is not added. #900

Open
AbdelhayA0100 opened this issue Apr 14, 2021 · 3 comments
Open

Babel throws on JSX files when preset-react is not added. #900

AbdelhayA0100 opened this issue Apr 14, 2021 · 3 comments

Comments

@AbdelhayA0100
Copy link

"babel-loader": "^8.1.0",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.24.1",
"babel-preset-stage-2": "^6.24.1",
"webpack": "^4.44.2",
"webpack-cli": "^4.6.0",
"webpack-dev-server": "^3.11.1"

const path = require("path");
var SRC_DIR = path.resolve(__dirname, "src");

///////////////////////////file web webpack.config /////////////////////////////
module.exports = {
mode: "development",
entry: "./src/index.js",
output: {
path: path.resolve(__dirname, "dist"),
filename: "bundle.js",
},
resolve: {
extensions: [".js", ".jsx"],
alias: {
assets: path.resolve(__dirname, "src/assets/"),
},
},
module: {
rules: [
{
test: /.jsx?$/,
loader: "babel-loader",
include: __dirname + "/src/",
exclude: /node_modules/,
query: {
presets: ["babel-preset-myPreset", "@babel/preset-env"],
},
},
],
},
// resolve: {
// extensions: ["*", ".js", ".jsx"],
// alias: {
// assets: path.resolve(__dirname, "/src/Main/assets"),
// },
// },
};

ERROR in ./src/index.js 7:2
Module parse failed: Unexpected token (7:2)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
|
| ReactDOM.render(

<React.StrictMode>
|
| </React.StrictMode>,

file index
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';

ReactDOM.render(
<React.StrictMode>

</React.StrictMode>,
document.getElementById('root')
);

@JLHwung
Copy link
Contributor

JLHwung commented Apr 14, 2021

You need to add preset-react to your babel config

presets: ["babel-preset-myPreset", "@babel/preset-env", "@babel/preset-react"],

@JLHwung JLHwung changed the title You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. Babel throws on JSX files when preset-react is not added. Apr 14, 2021
@AbdelhayA0100
Copy link
Author

AbdelhayA0100 commented Apr 14, 2021

@JLHwung
i add this line presets: ["babel-preset-myPreset", "@babel/preset-env", "@babel/preset-react"], but same error
/////////////////////////// webpack.config /////////////////////////////
const path = require("path");

var DIST_DIR = path.resolve(__dirname, "dist");
var SRC_DIR = path.resolve(__dirname, "src");

module.exports = {
mode: "development",
entry: SRC_DIR + "/index.js",
output: {
path: DIST_DIR + "/src",
filename: "bundle.js",
publicPath: "/src/",
},
resolve: {
extensions: [".js", ".jsx"],
alias: {
assets: path.resolve(__dirname, "src/assets/"),
},
},
module: {
rules: [
{
test: /.jsx?$/,
loader: "babel-loader",
include: __dirname + "/src/",
exclude: /node_modules/,
query: {
presets: [
"babel-preset-myPreset",
"@babel/preset-env",
"@babel/preset-react",
],
},
},
],
},
};

@hzoo
Copy link
Member

hzoo commented Apr 14, 2021

I would think you should have a different error? https://github.com/babel/babel-loader#install

the packages you are installing are not the same name (the babel config and install should match): You'd need to install @babel/preset-react (note the scoped package) since the ones currently in package.json are older as well

"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.24.1",
"babel-preset-stage-2": "^6.24.1",

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants