webpack not bundling with babel-loader and react - reactjs

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'] }

Related

No appropriate loader to handle js WebPack babel-loader

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.

How to make npm module written in es6 point to source when opening to declaration

I have published a npm module written in ES6. I have used Babel to transpile it to ES5 and webpack to bundle it.
Now when I import the module in my React app and when I go to the declaration, ES5 code is opened.
module.exports = (function (e) {
var t = {};
function r(n) {}
...
I want to show the ES6 code when opening declaration in an IDE, not the ES5 doe.
import React from 'react';
export class RerenderTracker extends React.Component {
componentWillUnmount() {}
...
Here is my package.json
{
"name": "rerender-tracker",
"version": "1.0.10",
"description": "Component for tracking react rerenders",
"main": "./build/index.js",
"scripts": {
"build": "webpack --mode production",
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "https://github.com/mtroskot/rerender-tracker.git"
},
"keywords": [
"react",
"render",
"rerender",
"tracker"
],
"author": "Marko Troskot",
"license": "MIT",
"bugs": {
"url": "https://github.com/mtroskot/rerender-tracker/issues"
},
"homepage": "https://github.com/mtroskot/rerender-tracker#readme",
"peerDependencies": {
"react": ">=16"
},
"dependencies": {
"react": "^16.13.1"
},
"devDependencies": {
"#babel/core": "^7.9.6",
"#babel/plugin-proposal-class-properties": "^7.8.3",
"#babel/preset-env": "^7.9.6",
"#babel/preset-react": "^7.9.4",
"babel-loader": "^8.1.0",
"babel-runtime": "^6.26.0",
"webpack": "^4.43.0",
"webpack-cli": "^3.3.11"
}
}
my .babelrc
{
"presets": ["#babel/preset-env", "#babel/preset-react"],
"plugins": ["#babel/plugin-proposal-class-properties"]
}
my webpack.config.js
var path= require("path");
module.exports = {
entry:"./src/index.js",
output:{
path:path.resolve("build"),
filename:"index.js",
libraryTarget:"commonjs2"
},
module: {
rules: [
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use: {
loader: "babel-loader"
}
}
]
},
externals:{
react:"react"
}
};
Note
This is my first npm module so if there is something wrong in the configs or doesn't follow best practices, please let me know.

I'm trying to build a new react.js project and have a compile error

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.

Can't create a React component in node_modules and use it in an application

I just created a new application with create-react-app.
I created a new project with a component and used webpack to transpile the JSX with babel and bundle everything to /dist/bundle.js.
I linked with npm link the application and the components I created in the new project.
In the application I imported the components but I'm always getting the error
Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.
Printing the components in the application they are all undefined.
This is the component:
index.js
import Root from './Root';
export { Root };
Root.js
import React, { Component } from 'react';
class Root extends Component {
render() {
return (<div>{this.props.children}</div>);
}
}
export default Root;
webpack.config.js
const path = require('path');
module.exports = {
entry: './src/index.js',
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'dist')
},
module: {
rules: [
{
test: /\.jsx?$/,
exclude: /(node_modules|bower_components)/,
use: {
loader: 'babel-loader',
query: {
presets:['react']
}
}
}
]
}
};
package.json
{
"name": "my-comp",
"version": "0.0.1",
"description": "",
"main": "dist/bundle.js",
"dependencies": {
"babel-core": "^6.26.0",
"babel-loader": "^7.1.2",
"babel-preset-env": "^1.6.1",
"babel-preset-react": "^6.24.1",
"react": "^16.2.0"
},
"devDependencies": {
"webpack": "^3.11.0"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"watch": "webpack --watch",
"build": "webpack"
},
"keywords": [],
"author": "",
"license": "ISC"
}
In the application
App.js
import { Root } from 'my-comp';
render() {
return (
<Root>
<span>Hello World</span>
</Root>
);
}
Anyone knows what is going on? Can't I import from bundle.js?
I found the solution.
In the webpack.config.js, change the output object to this:
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'dist'),
libraryTarget: 'commonjs2' //THIS IS IMPORTANT
}
It tells the engine that this is an exportable module.

React and webpack appropriate loader error

Hi I'm new in react I just starting to learn reactjs.
I have some problems and i don't know how to fix it.
This is my package.json file
{
"name": "react",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"babel-core": "5.8.*",
"babel-loader": "5.3.*",
"react": "0.13.*",
"webpack": "1.12.*",
"webpack-dev-server": "1.10.*"
}
}
webpack.config,js file
module.exports = {
entry: [
'./src/App.js'
],
output: {
path: __dirname,
filename: 'app.js'
},
module: {
loader: [{
test: /\.jsx?$/,
loader: 'babel'
}]
}
};
and app.js file
import React from 'react';
class App extends React.Component {
render() {
return (
<div>
<h1>alo</h1>
</div>
)
}
}
React.render(<App />, document.getElementById('app'));
and the error when I run webpack
ERROR in ./src/App.js
Module parse failed: C:\Users\mkrtc\Desktop\react\src\App.js Line 1: Unexpected token
You may need an appropriate loader to handle this file type.
| import React from 'react';
|
| class App extends React.Component {
# multi main
You need to set up your babel configuration. Unlike babel 5, Babel 6 doesn't do anything out of the box. plugins and presets need to set. Create a .bablerc file at the root level like this:
"presets": [
"react",
"stage-0"
],
this enables the features from presets react and stage-0

Resources