I got unexpected error about bundling a JSON data. I tried to use json-loader to bundle json, but got the same error there.
ERROR in ./src/index.js 6:7
Module parse failed: Unexpected token (6:7)
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
| import data from './data/recipes.json';
|
> render(<Menu recipes={data} />, document.getElementById('root'));
webpack 5.53.0 compiled with 1 error in 82 ms
./src/index.js
import React from 'react';
import { render} from 'react-dom';
import Menu from './components/Menu';
import data from './data/recipes.json';
render(<Menu recipes={data} />, document.getElementById('root'));
webpack.config.js
var path = require('path');
module.exports = {
entry: './src/index.js',
output: {
path: path.join(__dirname, 'dist', 'assets'),
filename: 'bundle.js'
},
module: {
rules: [{ test: /\.js$/, exclude: /node_modules/, loader: 'babel-loader' }]
}
};
.babelrc
{
"presets": [
"#babel/preset-env",
[
"#babel/preset-react",
{
"runtime": "automatic"
}
]
]
}
package.json
{
"name": "recipes-app",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "webpack --mode production"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"react": "^17.0.2",
"react-dom": "^17.0.2",
"serve": "^12.0.1"
},
"devDependencies": {
"#babel/core": "^7.15.5",
"#babel/preset-env": "^7.15.6",
"#babel/preset-react": "^7.14.5",
"babel-loader": "^8.2.2",
"webpack": "^5.53.0",
"webpack-cli": "^4.8.0"
}
}
In 'webpack.config.js' file I tried to change 'loader' to 'use', same thing...
import ReactDOM from 'react-dom' and ReactDOM.render() also same thing...
webpack.config.js was at the wrong place of a dir structure.
Related
I am following instructions from a React course when setting up Babel with Webpack and now I get this error:
ERROR in ./src/app.js
Module build failed: SyntaxError: C:/Users/38164/Desktop/react-course-projects/indecision-app/src/app.js: Unexpected token (4:17)
image of the error
Here are the files from the setup:
app.js
import React from 'react';
import ReactDOM from 'react-dom';
const template = <p>testing</p>;
ReactDOM.render(template, document.getElementById('app'));
webpack.config.js
const path = require("path");
module.exports = {
entry: "./src/app.js",
output: {
path: path.join(__dirname, "public"),
filename: "bundle.js",
},
module: {
rules: [{
loader: "babel-loader",
test: /\.js$/,
exclude: /node_modules/
}
]
}
};
package.json
{
"name": "indecision-app",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"scripts": {
"serve": "live-server public/",
"build": "webpack --watch",
"build-babel": "babel src/app.js --out-file=public/scripts/app.js --presets=env,react --watch"
},
"dependencies": {
"#babel/core": "^7.20.12",
"#babel/preset-env": "^7.20.2",
"#babel/preset-react": "^7.18.6",
"babel-cli": "6.24.1",
"babel-loader": "7.1.1",
"live-server": "^1.2.2",
"react": "16.0.0",
"react-dom": "16.0.0",
"validator": "8.0.0",
"webpack": "3.1.0"
}
}
.babelrs
{
"presets": [
"#babel/preset-env",
"#babel/preset-react"
]
}
Does anyone have any suggestions?
There is probably something minor, but I can't figure it out, as this is new for me.
Im trying to configure webpack for React.
But when i ran "npm run start" i got this error in VSC console:
ERROR in ./src/index.js 5:16
Module parse failed: Unexpected token (5:16)
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
| import App from "./components/App";
|
> ReactDOM.render(<App />, document.getElementById("app"));
|
webpack.config.js:
const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");
module.export = {
entry: "./src/index.js",
output: {
path: path.resolve(__dirname, "dist"),
filename: "bundle.js",
},
resolve: {
extensions: [".js", ".jsx"],
},
module: {
rules: [
{
test: /\.js|\.jsx$/,
exclude: /node_modules/,
use: {
loader: "babel-loader",
},
},
{
test: /\.html$/,
use: [{ loader: "html-loader" }],
},
],
},
plugins: [
new HtmlWebpackPlugin({
template: "./public/index.html",
filename: "./index.html",
}),
],
devServer: {
static: path.join(__dirname, "dist"),
compress: true,
port: 3000,
},
};
Index.js:
import React from "react";
import ReactDOM from "react-dom";
import App from "./components/App";
ReactDOM.render(<App />, document.getElementById("app"));
Package.json:
{
"dependencies": {
"react": "^17.0.2",
"react-dom": "^17.0.2"
},
"name": "curso-webpack-react",
"description": "",
"version": "1.0.0",
"main": "index.js",
"devDependencies": {
"#babel/core": "^7.15.5",
"#babel/preset-env": "^7.15.6",
"#babel/preset-react": "^7.14.5",
"babel-loader": "^8.2.2",
"html-loader": "^2.1.2",
"html-webpack-plugin": "^5.3.2",
"prettier": "2.4.1",
"webpack": "^5.53.0",
"webpack-cli": "^4.8.0",
"webpack-dev-server": "^4.2.1"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "webpack-dev-server",
"build": "webpack --mode production"
},
"repository": {
"type": "git",
"url": "git+https://github.com/platzi/curso-webpack-react.git"
},
"keywords": [],
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/platzi/curso-webpack-react/issues"
},
"homepage": "https://github.com/platzi/curso-webpack-react#readme"
}
.babelrc:
{
"presets": [
"#babel/preset-env",
"#babel/preset-react"
]
}
I don't know what to try anymore.
Iv tried to change test: /\.(.js|jsx)$/, to test: /\.js$|jsx/, or test: /\.js|\.jsx$/, but no results.
Iv also tried others solutions to the same problems that iv saw in others stackoverflow publications, but no results tho.
I'm setting up new react project and I try to compile webpack and had syntax error. In my guesses it is due to package.json and webpack.config.js problem
this is my pacakage.json code
{
"name": "blog_project",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"webpack": "webpack"
},
"author": "",
"license": "ISC",
"devDependencies": {
"#babel/core": "^7.5.0",
"#babel/preset-env": "^7.5.2",
"#babel/preset-react": "^7.0.0",
"babel-loader": "^8.0.6",
"babel-preset-env": "^1.7.0",
"css-loader": "^3.0.0",
"node-sass": "^4.12.0",
"webpack": "^4.35.3",
"webpack-cli": "^3.3.5"
},
"dependencies": {
"babel-preset-react": "^6.24.1"
"react": "^16.8.6",
"react-dom": "^16.8.6"
}
}
and here is my webpack.config.js code
const path = require('path');
module.exports = {
mode: 'none',
entry: {
app: './src/index.js'
},
watch: true,
devtool: 'source-map',
output: {
filename: '[name].bundle.js',
path: path.resolve(__dirname, 'dist')
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: ['babel-loader']
}
]
},
resolve: {
extensions: [
'.js'
]
}
}
and index.js code I trying to compile and show some div tag on a webbrowser
import React from 'react'
import ReactDOM from 'react-dom'
class RootEl extends React.Component {
render() {
return <div><h1>This is JSX!</h1></div>;
}
}
ReactDOM.render(<RootEl />, document.getElementById("root"))
and when I try to compile this code and then webpack shows me error message
ERROR in ./src/index.js
Module build failed (from ./node_modules/babel-loader/lib/index.js):
SyntaxError: C:\blog\blog\static\src\index.js: Unexpected token (7:15)
5 |
6 | render() {
> 7 | return <div>This is XML!</div>;
| ^
8 | }
9 |
10 | }
JSX (like <div> inside javascript) is not a valid javascript syntax. You need babel to transpile those JSX into a valid javascript. To do it, you need to tell babel HOW to transpile those.
You should install #babel/preset-react and set babel up so it will use that preset.
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: [
{
loader: 'babel-loader',
options: {
presets: [
['#babel/preset-react'],
],
},
},
]
}
]
},
Presets are basically just a set of rules that babel can refer to when trying to transpile codes.
I'm an old hat coder but I'm new to react, and it looks like I need another pair of eyeballs. I'm trying to setup a very basic React app from scratch (for practice) and it fires up fine, but when I try to run webpack --watch it's giving me the following error:
Here's the error I'm getting:
ERROR in ./src/index.js 5:16
Module parse failed: Unexpected token (5:16)
You may need an appropriate loader to handle this file type.
| import App from "./components/App.js";
|
> ReactDOM.render(<App />, document.getElementById('root'));
|
Here's my webpack.dev.config file:
const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");
module.exports = {
entry: "./src/index.js",
output: {
path: path.join(__dirname, "/dist"),
filename: "index_bundle.js"
},
module: {
rules: [{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: "babel-loader"
},
},
{
test: /\.css$/,
use: ["style-loader", "css-loader"]
}
]
},
plugins: [
new HtmlWebpackPlugin({
template: "./src/index.html"
})
]
};
Here's my package.json file:
{
"name": "react-boilerplate",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"watch": "webpack --watch --open",
"dev": "./node_modules/.bin/webpack-dev-server --mode development --config ./webpack.dev.config.js",
"build": "webpack --mode production"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"#babel/core": "^7.3.3",
"#babel/preset-env": "^7.3.1",
"#babel/preset-react": "^7.0.0",
"babel-loader": "^8.0.5",
"babel-preset-es2015": "^6.24.1",
"babel-preset-stage-0": "^6.24.1",
"css-loader": "^2.1.0",
"html-webpack-plugin": "^3.2.0",
"style-loader": "^0.23.1",
"webpack": "^4.29.5",
"webpack-cli": "^3.2.3",
"webpack-dev-server": "^3.2.0"
},
"dependencies": {
"react": "^16.8.3",
"react-dom": "^16.8.3"
}
}
Here's my .babelrc file:
{
"presets": ["#babel/preset-env", "#babel/preset-react"]
}
And finally... my index.js file
import React from "react";
import ReactDOM from "react-dom";
import App from "./components/App.js";
ReactDOM.render(<App />, document.getElementById('root'));
Directory Structure
This ought to be pretty straight-forward but I guess I'm just not seeing something. I'd appreciate any help!!
I am following along with a relatively older tutorial (from 2014) on RGR. I had to use an updated version of React, Webpack, and Babel so there are some differences. Everything has been working thus far except when I tried to compile JSX into webpack, it is giving me a build error.
ERROR in ./public/js/app.js
Module build failed: SyntaxError: Unexpected token (7:15)
5 | class Hello extends React.Component {
6 | render() {
7 | return <h3>Hello Webpack!</h3>;
| ^
8 | }
9 | }
10 |
Below is my React file:
import React, {Component} from 'react';
import ReactDOM from 'react-dom';
class Hello extends React.Component {
render() {
return <h3>Hello Webpack!</h3>;
}
}
ReactDOM.render(<Hello />, document.getElementById('react'));
and this is my webpack.config.js file
module.exports = {
entry: "./public/js/app.js",
output: {
path: __dirname + "/public",
filename: "bundle.js"
},
module: {
loaders: [
{test: /\.js$/,
loader: 'babel-loader' }
]
}
}
Also, here is my package.json file
{
"name": "rgrjs",
"version": "1.0.0",
"description": "a collection of educational resources about React, GraphQL, and Relay",
"main": "index.js",
"scripts": {
"start": "node server.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/krisxcrash/rgr-stack.git"
},
"author": "kristine martin",
"license": "ISC",
"bugs": {
"url": "https://github.com/krisxcrash/rgr-stack/issues"
},
"homepage": "https://github.com/krisxcrash/rgr-stack#readme",
"dependencies": {
"babel-loader": "^7.1.2",
"create-react-class": "^15.6.2",
"express": "^4.16.2",
"react": "^16.2.0",
"react-dom": "^16.2.0"
},
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-preset-env": "^1.6.1"
}
}
Can anyone tell me why it is not reading the <h3> after return and causing an error when webpack is trying to bundle?
You're missing some presets:
First do npm install:
npm install babel-core babel-preset-es2015 babel-preset-react --save-dev
Also make a .babelrc in your root project directory, and include this:
{
"presets": ["es2015","react"]
}
Also in your loaders section of webpack config, include this for jsx:
{ test: /\.jsx?$/, exclude: /node_modules/, use: ['babel-loader'] }