React and webpack appropriate loader error - reactjs

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

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.

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.

webpack not bundling with babel-loader and react

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

Webpack - Typescript cannot find file /node_modules/react/package.json

UPDATE:
It seems likely that it is a TypeScript issue after all, probably coming for a PR merged earlier today.
I originally posted this to https://github.com/Microsoft/TypeScript/issues but was advised that this might not be a TypeScript bug.
I am using typescript#next with webpack and ts-loader.
When running Webpack I am receiving the following error from typescript.js:43445
C:\Users\Michael\Development\Replicate>webpack
Using config file at C:\Users\Michael\Development\Replicate\tsconfig.json
C:\Users\Michael\Development\Replicate\node_modules\typescript\lib\typescript.js:43445
throw new Error("Could not find file: '" + fileName + "'.");
^
Error: Could not find file: 'C:/Users/Michael/Development/Replicate/node_modules/react/package.json'.
at getValidSourceFile (C:\Users\Michael\Development\Replicate\node_modules\typescript\lib\typescript.js:43445:23)
at Object.getSyntacticDiagnostics (C:\Users\Michael\Development\Replicate\node_modules\typescript\lib\typescript.js:43615:52)
at C:\Users\Michael\Development\Replicate\node_modules\ts-loader\index.js:186:42
at Array.forEach (native)
at Compiler.<anonymous> (C:\Users\Michael\Development\Replicate\node_modules\ts-loader\index.js:185:14)
at Compiler.applyPlugins (C:\Users\Michael\AppData\Roaming\npm\node_modules\webpack\node_modules\tapable\lib\Tapable.js:26:37)
at Compiler.<anonymous> (C:\Users\Michael\AppData\Roaming\npm\node_modules\webpack\lib\Compiler.js:193:12)
at Compiler.emitRecords (C:\Users\Michael\AppData\Roaming\npm\node_modules\webpack\lib\Compiler.js:282:37)
at Compiler.<anonymous> (C:\Users\Michael\AppData\Roaming\npm\node_modules\webpack\lib\Compiler.js:187:11)
at C:\Users\Michael\AppData\Roaming\npm\node_modules\webpack\lib\Compiler.js:275:11
at Compiler.applyPluginsAsync (C:\Users\Michael\AppData\Roaming\npm\node_modules\webpack\node_modules\tapable\lib\Tapable.js:60:69)
at Compiler.afterEmit (C:\Users\Michael\AppData\Roaming\npm\node_modules\webpack\lib\Compiler.js:272:8)
at Compiler.<anonymous> (C:\Users\Michael\AppData\Roaming\npm\node_modules\webpack\lib\Compiler.js:267:14)
at C:\Users\Michael\AppData\Roaming\npm\node_modules\webpack\node_modules\async\lib\async.js:52:16
at done (C:\Users\Michael\AppData\Roaming\npm\node_modules\webpack\node_modules\async\lib\async.js:248:21)
at C:\Users\Michael\AppData\Roaming\npm\node_modules\webpack\node_modules\async\lib\async.js:44:16
at FSReqWrap.oncomplete (fs.js:95:15)
my package.json
{
"name": "Replicate",
"version": "1.0.0",
"description": "Reproducing typescript file not found bug",
"main": "webpack.config.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Michael Ross",
"license": "MIT",
"dependencies": {
"react": "^0.13.3",
"ts-loader": "^0.5.0",
"typescript": "^1.6.0-dev.20150825"
}
}
my webpack.config.js
module.exports = {
entry: "./app.tsx",
output: {
filename: "bundle.js"
},
resolve: {
extensions: ["", ".tsx", ".ts", ".js"]
},
module: {
loaders: [
{ test: /\.ts(x?)$/, loader: "ts-loader" }
]
}
}
my app.tsx
import React = require('react');
class Demo extends React.Component<any, any> {
render() {
return <div>Trying to replicate</div>
}
}
React.render(<Demo />, document.getElementById("demo"));
and just in case, my tsconfig.json
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"jsx": "react",
"sourceMap": true
},
"files": [
"./typings/tsd.d.ts",
"./app.tsx"
]
}
and tsd.json
{
"version": "v4",
"repo": "borisyankov/DefinitelyTyped",
"ref": "master",
"path": "typings",
"bundle": "typings/tsd.d.ts",
"installed": {
"react/react.d.ts": {
"commit": "71a7d5306ae4f9893aafd2d85d38aac8789ebf33"
}
}
}
If I have a configuration error, help and guidance is much appreciated. Thanks!
Change:
import React = require('react');
To:
var React = require('react');
Unless your going for the es6 way which should be:
import React from "react";
That could be the problem.
This issue has been marked as a TypeScript bug for Milestone 1.6 https://github.com/Microsoft/TypeScript/issues/4445

Resources