I'm new in ReactJs. Now I'm developing a small project using reactJs and I'm trying to configure with Webpack. but getting following error. I'm not sure how to handle this? I tried with some solution but not worked for me?
webpack.config
var path = require('path');
var hwp = require('html-webpack-plugin');
module.exports = {
entry: path.join(__dirname, '/src/index.js'),
output: {
filename: 'build.js',
path: path.join(__dirname, '/dist')
},
module:{
rules:[{
exclude: /node_modules/,
test: /\.(js|jsx)$/,
loader: 'babel-loader',
query: {
presets: [,'#babel/preset-env','#babel/preset-react','#babel/preset-es2015'],
plugins: ['#babel/proposal-class-properties','#babel/plugin-proposal-object-rest-spread']
}
}]
},
plugins:[
new hwp({template:path.join(__dirname, '/public/index.html')})
]
}
.babelrc
{
"presets": [
"#babel/preset-env",
"#babel/preset-react",
"#babel/preset-es2015"
],
"plugins": [
"#babel/plugin-proposal-class-properties",
"#babel/plugin-proposal-object-rest-spread"
]
}
package.json
{
"name": "demo",
"version": "0.1.0",
"private": true,
"main": "index.js",
"dependencies": {
"node-sass": "^4.12.0",
"react": "^16.8.6",
"react-dom": "^16.8.6",
"react-scripts": "3.0.1",
"reactstrap": "^8.0.0"
},
"scripts": {
"start": "webpack-dev-server --open --mode development",
"build": "webpack --mode production"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"#babel/core": "^7.5.5",
"#babel/plugin-proposal-class-properties": "^7.5.5",
"#babel/plugin-proposal-object-rest-spread": "^7.5.5",
"#babel/preset-env": "^7.5.5",
"#babel/preset-es2015": "^7.0.0-beta.53",
"#babel/preset-react": "^7.0.0",
"babel-loader": "^8.0.6",
"html-loader": "^0.5.5",
"html-webpack-plugin": "^3.2.0",
"webpack": "^4.39.0",
"webpack-cli": "^3.3.6",
"webpack-dev-server": "^3.7.2",
"webpack-merge": "^4.2.1"
}
}
The problem is, when i hit npm start it brings me an error and the page displayed empty:
Error
ERROR in ./src/index.js
Module build failed (from ./node_modules/babel-loader/lib/index.js):
Error: Cannot find module '#babel/preset-es2015' from 'D:\projects\React\sample\demo'
at Function.module.exports [as sync] (D:\projects\React\sample\demo\node_modules\resolve\lib\sync.js:74:15)
Child html-webpack-plugin for "index.html":
1 asset
Entrypoint undefined = index.html
[./node_modules/html-webpack-plugin/lib/loader.js!./public/index.html] 1.39 KiB {0} [built]
[./node_modules/lodash/lodash.js] 528 KiB {0} [built]
[./node_modules/webpack/buildin/global.js] (webpack)/buildin/global.js 472 bytes {0} [built]
[./node_modules/webpack/buildin/module.js] (webpack)/buildin/module.js 497 bytes {0} [built]
i 「wdm」: Failed to compile.
Related
Trying to build this by "npm run build" but getting this error:
ERROR in ./src/index.js 9:2
Module parse failed: Unexpected token (9: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
| const root = ReactDOM.createRoot(document.getElementById('root'));
| root.render(
<React.StrictMode>
|
| </React.StrictMode>
webpack 5.75.0 compiled with 1 error in 349 ms
**webpack.config.js :
**
const port = process.env.PORT || 8080;
const path = require("path");
module.exports = {
output: {
path: path.join(__dirname, "/dist"),
filename: "main.[fullhash].js",
},
module: {
rules: [
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use: {
loader: "babel-loader",
},
},
],
},
devServer: {
host: "localhost",
port: port,
historyApiFallback: true,
open: true,
},
};
**.babelrc : **
{
"presets": ["#babel/preset-env", "#babel/preset-react"],
"plugins": [["#babel/transform-runtime"]]
}
package.json
{
"name": "webpack",
"version": "0.1.0",
"private": true,
"dependencies": {
"#babel/runtime": "^7.20.6",
"#testing-library/jest-dom": "^5.16.5",
"#testing-library/react": "^13.4.0",
"#testing-library/user-event": "^13.5.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4"
},
"scripts": {
"start": "webpack serve --mode production --open --hot",
"build": "webpack --mode production",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"#babel/core": "^7.20.5",
"#babel/plugin-transform-runtime": "^7.19.6",
"#babel/preset-env": "^7.20.2",
"#babel/preset-react": "^7.18.6",
"babel-loader": "^9.1.0",
"webpack": "^5.75.0",
"webpack-cli": "^5.0.1",
"webpack-dev-server": "^4.11.1"
}
}
I have webpack and babel loader installed, however, webpack is unable to recognise that Babel Loader is installed:
I installed babel loader and the presets using the following command: npm install #babel/core babel-loader #babel/preset-react --save-dev
Here is my
webpack.config.js:
const path = require('path')
const config = {
entry: './src/index.js',
output: {
path: path.resolve(__dirname, 'build'),
filename: 'main.js',
},
module: {
rules: [
{
test: /\.js$/,
loader: 'babel-loader',
options: {
presets: ['#babel/preset-react'],
},
},
],
},
}
module.exports = config
and package.json:
{
"name": "webpack",
"version": "0.1.0",
"private": true,
"dependencies": {
"#testing-library/jest-dom": "^5.16.4",
"#testing-library/react": "^13.3.0",
"#testing-library/user-event": "^13.5.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4"
},
"scripts": {
"start": "react-scripts start",
"build": "webpack --mode=development",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"#babel/core": "^7.18.6",
"#babel/preset-react": "^7.18.6",
"babel-loader": "^8.2.5",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.24.1",
"install": "^0.13.0",
"npm": "^8.13.2",
"webpack": "^5.73.0",
"webpack-cli": "^4.10.0"
}
}
Your Webpacks Babel module rule object has a wrong structure. Please, follow the Webpack official docs and wrap your 'loader' and 'options' properties in a 'use'.
Wrong rules:
module: {
rules: [
{
test: /\.js$/,
loader: 'babel-loader',
options: {
presets: ['#babel/preset-react'],
},
},
],
},
Correct rules:
module: {
rules: [
{
test: /\.m?js$/,
use: {
loader: 'babel-loader',
options: {
presets: ['#babel/preset-react']
}
}
}
]
}
I am setting up a React app with babel and webpack. The app works fine on chrome and other browsers except internet exlorer11.
Here is my package.json
{
"name": "xai",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"webpack": "webpack",
"start": "webpack serve"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all",
"not ie < 11",
"ie 11"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version",
"ie 11"
]
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"#babel/core": "^7.14.3",
"#babel/node": "^7.14.2",
"#babel/plugin-proposal-class-properties": "^7.13.0",
"#babel/preset-env": "^7.14.4",
"#babel/preset-react": "^7.13.13",
"#fortawesome/fontawesome-free": "^5.15.3",
"babel-loader": "^8.2.2",
"css-loader": "^5.2.6",
"file-loader": "^6.2.0",
"html-webpack-plugin": "^5.3.1",
"image-webpack-loader": "^7.0.1",
"path": "^0.12.7",
"style-loader": "^2.0.0",
"webpack": "^5.38.1",
"webpack-cli": "^4.7.0",
"webpack-dev-server": "^3.11.2"
},
"dependencies": {
"#material-ui/core": "^4.11.4",
"axios": "^0.21.1",
"bootstrap": "^5.0.1",
"copy-to-clipboard": "^3.3.1",
"fast-text-encoding": "^1.0.3",
"firebase": "^8.6.3",
"font-awesome": "^4.7.0",
"font-awesome-webpack": "0.0.5-beta.2",
"gh-pages": "^3.2.0",
"history": "^5.0.0",
"hookrouter": "^1.2.5",
"lodash": "^4.17.21",
"markdown-loader": "^6.0.0",
"max-validator": "^1.2.1",
"node-sass": "^6.0.0",
"raw-loader": "^4.0.2",
"react": "^17.0.2",
"react-animations": "^1.0.0",
"react-app-polyfill": "^2.0.0",
"react-beforeunload": "^2.5.1",
"react-beforeunload-component": "^1.2.3",
"react-dom": "^17.0.2",
"react-likert-scale": "^4.1.1",
"react-radio-buttons": "^1.2.2",
"react-router-dom": "^5.2.0",
"react-toastify": "^7.0.4",
"react-tooltip": "^4.2.21",
"reactstrap": "^8.9.0",
"sass-loader": "^11.1.1",
"url-loader": "^4.1.1",
"uuid": "^8.3.2"
}
}
And webpack.config.js
const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");
module.exports = {
entry: path.join(__dirname, "src", "index.js"),
output: { path: path.join(__dirname, "build"), filename: "index.bundle.js" },
mode: process.env.NODE_ENV || "development",
resolve: {
modules: [path.resolve(__dirname, "src/"), "node_modules"],
extensions: ["*", ".js", ".jsx", ".json"],
symlinks: false,
cacheWithContext: false,
},
devServer: { contentBase: path.join(__dirname, "src") },
module: {
rules: [
{
test: /\.woff(2)?(\?v=[0-9]\.[0-9]\.[0-9])?$/,
use: "url-loader?limit=10000&mimetype=application/font-woff",
},
{
test: /\.(ttf|eot|svg)(\?v=[0-9]\.[0-9]\.[0-9])?$/,
use: "file-loader",
},
{ test: /\.md$/, use: "raw-loader" },
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use: ["babel-loader"],
},
{
test: /\.(css|scss)$/,
use: ["style-loader", "css-loader"],
},
{
test: /\.(jpg|jpeg|png|gif|mp3|svg)$/,
use: ["file-loader"],
},
],
},
plugins: [
new HtmlWebpackPlugin({
template: path.join(__dirname, "src", "index.html"),
}),
],
};
and I use this config for .babelrc
{
"presets": ["#babel/preset-env", "#babel/react"],
"plugins": ["#babel/plugin-proposal-class-properties"]
}
The problem is that I used to have the app load(with broken routing when I use CRA). Now I only get a blank page with SCRIPT1006: Expected '('. I am wondering if you could have a workaround to solve this error
You can try to remove the browserslist and use target: ['web', 'es5'] to support IE 11. It will convert your code to ES5 which works in IE 11. For more information, you can refer to this answer and this doc.
I follow the steps in this blog to setup a React app with Webpack and Babel, then I add target: ['web', 'es5'] in webpack.config.js like below and it works well in IE 11. You can also try:
module.exports = {
// ...
target: ['web', 'es5'],
};
I have an issue that I'm spending a lot of time to seek a solution.
Previously my project was build by CRA and used react-scripts. Test script was working. For test I used react-testing-library (now implemented to react by default).
Actually it has changed and now app is built by webpack. I tried to implement jest and test, but I cannot configure it in proper way.
I tried:
install jest and babel-jest
add jest.config.js / jest.config.json
But still jest is sending me this information:
Jest encountered an unexpected token
This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.
By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".
Here's what you can do:
• To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
• If you need a custom transformation specify a "transform" option in your config.
• If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.
You'll find more details and examples of these config options in the docs:
https://jestjs.io/docs/en/configuration.html
Error shows whenever it meets React syntax
it("renders without crashing", () => {
6 | const div = document.createElement("div")
> 7 | ReactDOM.render(<App/>, div)
| ^
8 | })
My webpack config:
let path = require("path")
module.exports = {
entry: {
app: "./src/index.js",
},
output: {
filename: "[name].js",
path: path.resolve(process.cwd(), "name/static/js"),
},
module: {
rules: [
{
test: /\.m?js$/,
exclude: /(node_modules)/,
use: {
loader: "babel-loader",
options: {
presets: ["#babel/preset-env", "#babel/preset-react"],
},
},
},
{
test: /\.css$/,
use: ["style-loader", "css-loader"],
},
],
},
}
package.json
{
"name": "app-name",
"version": "0.1.0",
"private": true,
"dependencies": {
"#testing-library/user-event": "^7.1.2",
"axios": "^0.19.2",
"bootstrap": "^4.4.1",
"formik": "^2.1.4",
"json-server": "^0.16.1",
"lodash": "^4.17.15",
"react": "^16.13.0",
"react-input-mask": "^2.0.4",
"react-simple-timefield": "^3.0.0",
"reactstrap": "^8.4.1",
"yup": "^0.28.3"
},
"scripts": {
"test": "jest",
"start-dev": "webpack --progress --colors --watch --config webpack.dev.js",
"build": "webpack --progress --colors --config webpack.prod.js"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version",
"last 1 edge version"
]
},
"devDependencies": {
"#babel/core": "^7.9.0",
"#babel/plugin-transform-react-jsx": "^7.9.4",
"#babel/preset-env": "^7.9.0",
"#babel/preset-react": "^7.9.4",
"#testing-library/dom": "^7.1.0",
"#testing-library/jest-dom": "^4.2.4",
"#testing-library/react": "^9.5.0",
"babel-jest": "^25.5.0",
"babel-loader": "^8.1.0",
"css-loader": "^3.4.2",
"enzyme": "^3.11.0",
"enzyme-adapter-react-16": "^1.15.2",
"jest": "^25.5.0",
"react-dom": "^16.13.1",
"react-snapshot": "^1.3.0",
"react-test-renderer": "^16.13.1",
"style-loader": "^1.1.3",
"webpack": "^4.42.1",
"webpack-cli": "^3.3.11",
"webpack-merge": "^4.2.2"
}
}
},
My purpose is to be available to run tests with this configuration.
I found solution here: https://codesandbox.io/s/github/kentcdodds/react-testing-library-examples
as I script in package.json it should be just "test": "jest" and add in package.json
"babel": {
"presets": [
"react-app"
]
},
There is a need to create new file jest.config.js with code:
const path = require('path')
module.exports = {
roots: [path.resolve(__dirname, './src')],
testEnvironment: 'jest-environment-jsdom-sixteen',
displayName: 'tests',
testMatch: ['**//__tests__/**/*.js', '**/?(*.)+(spec|test).[jt]s?(x)'],
testURL: 'http://localhost',
setupFilesAfterEnv: [path.resolve(__dirname, './src/setupTests.js')],
}
My test files are in src/__ tests __/ folder and they have extension .test.js (i.e. app.test.js)
Add file setupTests.js, which contains:
import '#testing-library/jest-dom/extend-expect'
After that install jest-environment-jsdom-sixteen and babel-preset-react-app.
At last npm test should work and run all tests contained in __test__ folder.
I am using babel and Webpack first time to build a react app. Since morning I am trying, uninstalled and reinstalled packages many many times but babel gives one or other error when try to start server
I may have read all related questions on this platform but couldn't find solution that works.
This is how my webpack.config.js looks like:
var path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin')
module.exports = {
entry: path.resolve(__dirname, 'src/index.js') ,
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'bundle.js',
publicPath: '/src/'
},
plugins: [
new HtmlWebpackPlugin({
template: __dirname + '/public/index.html',
filename: 'index.html',
inject: 'body'
}), // Generates default index.html
// new HtmlWebpackPlugin({ // Also generate a test.html
// filename: 'test.html',
// template: 'src/assets/test.html'
// })
],
devServer: {
contentBase: "./public",
hot: true
},
module: {
rules: [
{
test: /.js$/,
loader: 'babel-loader',
query: {
presets: ['#babel/react', '#babel/es2015'],
plugins: ['#babel/proposal-class-properties']
}
}
]
}
};
package.json:
{
"name": "ondemnd-web",
"version": "0.1.0",
"private": true,
"dependencies": {
"babel-core": "7.0.0-bridge.0",
"react": "^16.10.2",
"react-dom": "^16.10.2",
"react-scripts": "3.2.0"
},
"scripts": {
"start": "webpack-dev-server --mode development --open --hot",
"build": "webpack --mode production",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"#babel/cli": "^7.6.2",
"#babel/core": "^7.6.2",
"#babel/plugin-proposal-class-properties": "^7.5.5",
"#babel/plugin-transform-react-jsx": "^7.3.0",
"#babel/preset-env": "^7.6.2",
"#babel/preset-es2015": "^7.0.0-beta.53",
"#babel/preset-react": "^7.0.0",
"babel-jest": "^24.9.0",
"babel-loader": "^7.1.5",
"html-webpack-plugin": "^3.2.0",
"regenerator-runtime": "^0.13.3",
"webpack": "^4.41.0",
"webpack-cli": "^3.3.9",
"webpack-dev-server": "^3.8.2"
}
}
.babelrc
{
"presets": [
"#babel/preset-env",
"#babel/preset-react"
],
"plugins": [
"transform-class-properties",
"transform-object-rest-spread"
]
}
Error:
ERROR in (webpack)/buildin/global.js
Module build failed (from ./node_modules/babel-loader/lib/index.js):
Error: Cannot find module '#babel/preset-es2015' from 'C:\repos\ondemandProject\ondemnd-web'
at Function.module.exports [as sync] (C:\repos\ondemandProject\ondemnd-web\node_modules\resolve\lib\sync.js:74:15)
at resolveStandardizedName (C:\repos\ondemandProject\ondemnd-web\node_modules\#babel\core\lib\config\files\plugins.js:101:31)
at resolvePreset (C:\repos\ondemandProject\ondemnd-web\node_modules\#babel\core\lib\config\files\plugins.js:58:10)
at loadPreset (C:\repos\ondemandProject\ondemnd-web\node_modules\#babel\core\lib\config\files\plugins.js:77:20)
at createDescriptor (C:\repos\ondemandProject\ondemnd-web\node_modules\#babel\core\lib\config\config-descriptors.js:154:9)
at items.map (C:\repos\ondemandProject\ondemnd-web\node_modules\#babel\core\lib\config\config-descriptors.js:109:50)
at Array.map (<anonymous>)
at createDescriptors (C:\repos\ondemandProject\ondemnd-web\node_modules\#babel\core\lib\config\config-descriptors.js:109:29)
at createPresetDescriptors (C:\repos\ondemandProject\ondemnd-web\node_modules\#babel\core\lib\config\config-descriptors.js:101:10)
at passPerPreset (C:\repos\ondemandProject\ondemnd-web\node_modules\#babel\core\lib\config\config-descriptors.js:58:96)
# ./node_modules/lodash/lodash.js 1:0-41
# ./node_modules/html-webpack-plugin/lib/loader.js!./public/index.html
babel-error
In node_modules also, no matter how many times I try to reinstall preet-es2015, I don't see the package. Plus, in the document https://babeljs.io/docs/en/v7-migration it states that The "env" preset has been out for more than a year now, and completely replaces some of the presets we've had and suggested earlier.
babel-preset-es2015
babel-preset-es2016
babel-preset-es2017
babel-preset-latest
A combination of the above ^
These presets should be substituted with the "env" preset.
#babel/preset-es2015
Not sure how to solve this problem
If anyone else is running into this issue, this can be fixed by uninstalling #babel/preset-es2015 completely. This is replaced by #babel/preset-env