React tests are not working after building it by Webpack - reactjs

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.

Related

Eslint import/no-extraneous-dependencies error on path

I am building a React app with Eslint setup.
In my app, I am using GraphQL #apollo/client dependency.
When I tried to do import { setContext } from '#apollo/client/link/context'
I got an eslint error of
'#apollo/client/link/context' should be listed in the project's dependencies. Run 'npm i -S #apollo/client/link/context' to add it import/no-extraneous-dependencies
I do have '#apollo/client' dependency in my package.json.
And import from '#apollo/client/link/context' is the proper way to get 'setContext' according to Apollo documentation.
Seems like import/no-extreaneous-dependencies is not recognize the nested path from '#apollo/client'.
When I set "import/no-extraneous-dependencies": 0 in my .eslintrc.js rules, it will work fine.
But for a proper solution, instead of just turning off the eslint checking, I am assuming that I probably need to set something up with .eslintrc.js rules.
What other set ups should I write for my rules in my .eslintrc.js in this case for properly solving the problem?
my package.json:
{
"name": "my-app",
"version": "0.1.0",
"private": true,
"dependencies": {
"#apollo/client": "^3.3.19",
"#auth0/auth0-react": "^1.4.0",
"#testing-library/jest-dom": "^5.11.4",
"#testing-library/react": "^11.1.0",
"#testing-library/user-event": "^12.1.10",
"eslint": "^7.26.0",
"eslint-config-airbnb": "^18.2.1",
"eslint-plugin-import": "^2.22.1",
"eslint-plugin-jsx-a11y": "^6.4.1",
"eslint-plugin-react": "^7.23.2",
"eslint-plugin-react-hooks": "^4.2.0",
"graphql": "^15.5.0",
"react": "^17.0.2",
"react-big-calendar": "^0.33.3",
"react-dom": "^17.0.2",
"react-router-dom": "^5.2.0",
"react-scripts": "4.0.3",
"web-vitals": "^1.0.1"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"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": {}
}
and my .eslintrc.js file:
module.exports = {
env: {
browser: true,
es2021: true,
},
extends: [
'plugin:react/recommended',
'airbnb',
],
parserOptions: {
ecmaFeatures: {
jsx: true,
},
ecmaVersion: 12,
sourceType: 'module',
},
plugins: [
'react',
],
rules: {
'no-console': 1,
'react/prop-types': 0,
'no-underscore-dangle': ['error', { allow: ['__raw'] }],
},
overrides: [
{
files: ['**/*.test.jsx'],
env: { jest: true },
},
],
};
I had the same issue with rxjs/operators as demonstrated in question 67587146. A solution is to add the path to the core modules setting in .eslintrc.js. This solution isn't great, but it's better than disabling the rule.
settings: {
'import/core-modules': ['#apollo/client/link/context']
}
A few others are finding this issue in a recent version of eslint-plugin-import, but there was a fix in v2.23.4, to the package resolution algorithm.
npm update eslint-plugin-import
Read more about the issue

Eslint error causing create-react-app failed to compile

I am building a website using create-react-app and have just installed eslint to it.
For some reason what was supposed to be shown as eslint warnings are showing up as errors and causing npm run start to fail.
How can I bypass this issue and have them shown as warnings like before ?
My .eslintrc.js
env: {
browser: true,
es6: true,
jest: true,
},
extends: [
'airbnb-typescript',
'plugin:#typescript-eslint/recommended',
'prettier/react',
'prettier/#typescript-eslint',
'plugin:prettier/recommended',
],
globals: {
Atomics: 'readonly',
SharedArrayBuffer: 'readonly',
},
parser: '#typescript-eslint/parser',
parserOptions: {
ecmaFeatures: {
jsx: true,
},
ecmaVersion: 2018,
sourceType: 'module',
project: './tsconfig.json',
},
plugins: ['react', '#typescript-eslint'],
rules: {
'class-methods-use-this': 'off',
'additional-rule': 'warn',
},
ignorePatterns: ['**/node_modules/*', '**/build/*', 'config-overrides.js'],
};
My package.json
"name": "device-protection-renewal-web",
"version": "0.1.0",
"private": true,
"dependencies": {
"#testing-library/jest-dom": "^5.11.5",
"#testing-library/react": "^11.1.0",
"#testing-library/user-event": "^12.1.10",
"#types/jest": "^26.0.15",
"#types/node": "^12.19.3",
"#types/react": "^16.9.55",
"#types/react-dom": "^16.9.9",
"babel-polyfill": "^6.26.0",
"core-js": "^3.6.5",
"i18next": "^19.8.3",
"react": "^17.0.1",
"react-app-polyfill": "^2.0.0",
"react-dom": "^17.0.1",
"react-i18next": "^11.7.3",
"react-scripts": "4.0.0",
"web-vitals": "^0.2.4"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"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",
"ie >= 9"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version",
"ie >= 9"
]
},
"devDependencies": {
"#babel/plugin-transform-arrow-functions": "^7.12.1",
"#typescript-eslint/eslint-plugin": "^4.6.1",
"#typescript-eslint/parser": "^4.6.1",
"eslint": "^7.11.0",
"eslint-config-airbnb-typescript": "^9.0.0",
"eslint-config-prettier": "^6.11.0",
"eslint-plugin-import": "^2.22.0",
"eslint-plugin-jsx-a11y": "^6.3.1",
"eslint-plugin-prettier": "^3.1.4",
"eslint-plugin-react": "^7.20.6",
"eslint-plugin-react-hooks": "^4.1.0",
"prettier": "^2.1.2",
"typescript": "^4.0.5"
}
}```
[1]: https://i.stack.imgur.com/WUKcz.png
I assume that you have installed ESLint using npm install eslint --save-dev and defined a default configuration with node_modules/.bin/eslint --init answering the questions in the prompt.
I noticed that in your .eslintrc.js file, the ESLint settings is missing in the extends option:
extends: [
'eslint:recommended',
'airbnb-typescript',
'plugin:#typescript-eslint/recommended',
'prettier/react',
'prettier/#typescript-eslint',
'plugin:prettier/recommended',
],
Also in the package.json is recommended ESLint to have its own script that you run using npm run lint and use it combined with a eslint-plugin in your favorite code editor:
{
"scripts": {
"start": "react-scripts start",
// ...
"lint": "eslint ."
},
}
Probably you will build your application at some point, so you should create a .eslintignore file and inside of it add build since files in the build directory also get checked by default when the command is ran.
Source: https://fullstackopen.com/en/part3/validation_and_es_lint#lint
This part in your package.json is unnecessary; since you have an aslant config file, it should be moved to .eslintrc.js.
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
}
Which would then turn into this:
extends: [
'react-app',
'react-app/jest',
'airbnb-typescript',
'plugin:#typescript-eslint/recommended',
'prettier/react',
'prettier/#typescript-eslint',
'plugin:prettier/recommended'
]
However; in the latest versions of the eslint-config-react-app plugin at this time (7.31.11), I'm getting a jest plugin conflict error with a project that worked prior to updating unless I remove react-app/jest from my eslint configs extends section.
Which is how I ended up here, currently trying to find out what caused this.
Update: So my issue was caused because eslint-config-react-app depends on eslint-plugin-jest^25.7.0 and I was using the latest eslint-plugin-jest^27.1.6. I removed my package.json's dependency and will use the version included with eslint-config-react-app so there aren't any conflicts there, but if I need features of the newer plugin version, a quick npm i -D on it, changing eslint configuration from automatic to manual and specifying the path to the local node_modules version and .eslintrc.js should work as well; aside from any conflicts with the config plugin.

Babel throwing Support for the experimental syntax 'jsx' isn't currently enabled

I started newly writing unit test cases using Jest and Enzyme for the react application and when try to run test cases using jest like "test": "jest --watch" rather "test": "react-scripts test" tests going through babel for the runner to understand react syntax.
And have been doing setup step by step using babel but this error Support for the experimental syntax 'jsx' isn't currently enabled stopping me to go further. And as suggested in some threads I have been trying with npm install --save-dev #babel/plugin-transform-react-jsx and tried to add the same plugin into babel configuration like shown in below package.json file but still no luck.
And this is my package.json
{
"name": "multitheme-app",
"version": "0.1.0",
"private": true,
"dependencies": {
"#material-ui/core": "^3.9.0",
"#material-ui/icons": "^3.0.2",
"axios": "^0.18.0",
"babel-plugin-transform-export-extensions": "^6.22.0",
"jest-css-modules": "^2.1.0",
"react": "^16.8.1",
"react-dom": "^16.8.1",
"react-redux": "^6.0.0",
"react-router-dom": "^4.3.1",
"react-scripts": "2.1.5",
"redux": "^4.0.1",
"redux-thunk": "^2.3.0"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": [
">0.2%",
"not dead",
"not ie <= 11",
"not op_mini all"
],
"babel": {
"presets": [
"#babel/preset-react",
"#babel/preset-env",
"#babel/preset-flow"
],
"plugins": [
"#babel/plugin-transform-modules-commonjs",
"#babel/plugin-transform-react-jsx"
]
},
"devDependencies": {
"#babel/cli": "^7.10.4",
"#babel/core": "^7.10.4",
"#babel/plugin-proposal-class-properties": "^7.10.4",
"#babel/plugin-syntax-dynamic-import": "^7.8.3",
"#babel/plugin-transform-modules-commonjs": "^7.10.4",
"#babel/plugin-transform-react-jsx": "^7.10.4",
"#babel/preset-env": "^7.10.4",
"#babel/preset-es2015": "^7.0.0-beta.53",
"#babel/preset-flow": "^7.10.4",
"#babel/preset-react": "^7.10.4",
"#babel/runtime": "^7.10.4",
"babel-eslint": "^10.1.0",
"babel-jest": "^26.1.0",
"babel-loader": "^7.1.5",
"babel-plugin-transform-decorators-legacy": "^1.3.5",
"babel-preset-stage-0": "^6.24.1",
"enzyme": "^3.11.0",
"enzyme-adapter-react-16": "^1.15.2",
"enzyme-to-json": "^3.5.0",
"jest": "^23.6.0",
"jest-cli": "^26.1.0"
},
"jest": {
"verbose": true,
"clearMocks": true,
"collectCoverage": true,
"setupTestFrameworkScriptFile": "<rootDir>/src/setupTest.js",
"transform": {
"^.+\\.js$": "babel-jest"
},
"moduleNameMapper": {
"\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/__mocks__/fileMock.js",
"\\.(css|scss)$": "identity-obj-proxy"
}
}
}
and here is my test case file
import React from 'react';
import Adapter from 'enzyme-adapter-react-16';
import { shallow, configure } from 'enzyme';
import App from './App';
configure({adapter: new Adapter()});
describe('MyComponent', () => {
it('should render correctly in "debug" mode', () => {
const component = shallow(<App debug />);
expect(component).toMatchSnapshot();
});
});
If this issue is related to using jest,
jest config for your package.json should look like
with typescript -
"jest": {
...
"transform": {
"^.+\\.(ts|tsx|js|jsx)$": "ts-jest"
},
...
}
with js -
"jest": {
...
"transform": {
"^.+\\.(js|jsx)$": "babel-jest"
},
...
}
Adding my current solution as things constantly evolve.
I had a vanilla cra with jest and test dependencies
"devDependencies": {
"#babel/preset-env": "^7.14.9",
"#testing-library/jest-dom": "^5.11.4",
"#testing-library/react": "^11.1.0",
"#testing-library/user-event": "^12.1.10",
"jest": "^26.6.3"
},
the solution was simple
install yarn add #babel/preset-react
then correctly setup babel.config.js
module.exports = {
presets: [
'#babel/preset-react',
[
'#babel/preset-env',
{
targets: {
node: 'current',
},
},
],
],
};
note
in a .babelrc you use a different syntax for the presets array compared to the babel.config.js
Looking at the error it looks its not able to load the preset for react. Switch to the config file and move the configuration of babel from package.json to it.
A sample file would look like below and will be located at the same level as package.json and called as babel.config.js
module.exports = function (api) {
const presets = [
'#babel/preset-env',
'#babel/preset-react',
'#babel/preset-flow'
];
const plugins = [
'#babel/plugin-transform-runtime',
];
/** this is just for minimal working purposes,
* for testing larger applications it is
* advisable to cache the transpiled modules in
* node_modules/.bin/.cache/#babel/register* */
api.cache(false);
return {
presets,
plugins
};
};
I'm not sure if helps because for me wasn't related to jest. But I had the same error and after a lot of google I found this https://stackoverflow.com/a/52693007/10952640
TL;DR you could be missing to set the babel/preset-react on webpack also:
rules: [
{
test: /\.(js|jsx)$/,
exclude: /(node_modules|bower_components)/,
loader: 'babel-loader',
options: { presets: ['#babel/env', '#babel/preset-react'] },
},
I fixed this by removing the builds/ folder which was created by npm run build on the same level as my package.json. After the removal I ran jest --coverage again and it worked.
It can also have to do something with this.
If you are using a jest.config.js file with Webpack, that file needs to be in the root directory, same level as package.json.

CSS and Images files not coming after transpiling package with babel in new CRA

I am importing CSS as import './style.css'; and images in CSS with background URL property. What I want is to make a package, publish it to npm without building and then install it into a new CRA and use it there. Using self made npm package in react. Failed to compile. From here I got to know that to use that installed package now I have to transpile it. But the issue is my js/jsx are getting transpiled from ES6 to ES5 but the images and CSS aren't coming into the new transpiled folder.
DETAILS
I made a package in Reactjs and was not able to use it after publishing the source, not by making the build.
Then I posted a question on it: Using self made npm package in react. Failed to compile.
Now I am able to use it by following the steps in the accepted answer i.e. by transpiling ./src using babel.
The issue on which I am still stuck is that I don't get my CSS and images in the new transpiled location i.e. ./lib/src. If I copy all the images and CSS folders in the respective places in ./lib/src. It works but I don't want to do it manually.
Any suggestions on how to achieve this.
WEBPACK.CONFIG.JS
module.exports = {
overrides: [
{
test: ["./node_modules/<component_name>"],
presets: ["#babel/preset-env", "#babel/preset-react"],
plugins: ["#babel/plugin-proposal-class-properties"]
}
]
};
PACKAGE.json
{
"name": "package-test",
"version": "0.1.0",
"private": true,
"dependencies": {
"#babel/preset-es2015": "^7.0.0-beta.53",
"#babel/preset-react": "^7.8.3",
"#material-ui/core": "^1.5.1",
"#testing-library/jest-dom": "^4.2.4",
"#testing-library/react": "^9.4.0",
"#testing-library/user-event": "^7.2.1",
"active-event-stack": "^1.0.0",
"babel-loader": "^8.0.6",
"babel-plugin-webpack-loaders": "^0.9.0",
"classnames": "^2.2.3",
"css-loader": "^3.4.2",
"file-loader": "^5.0.2",
"material-ui": "^0.20.0",
"path": "^0.12.7",
"prop-types": "^15.6.0",
"react": "^16.12.0",
"react-dom": "^16.12.0",
"react-draggable": "^2.2.6",
"react-onclickoutside": "^5.10.0",
"react-redux": "^7.1.3",
"react-resizable": "^1.7.1",
"react-router-dom": "^5.1.2",
"react-scripts": "3.4.0",
"redux": "^3.7.2",
"style-loader": "^1.1.3",
"styled-components": "^4.3.2",
"url-loader": "^3.0.0",
"wolfy87-eventemitter": "^5.2.9"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"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.8.4"
}
}
Ok if you want to publish a component library then there are plenty of tutorials available online, and if your specific issue is about not been able to publish your static resources such as css images and scss etc then you can just append a cp command (linux & macos) to your build script
"scripts": {
"build": "babel src --out-dir lib --ignore src/__tests__/ && cp -R src/assets lib/",
},
Notice this towards the end of the command cp -R src/assets this will manually copy your static assets to the desired location when your run npm run build
You should use file-loader to load image files and css-loader to load your css files.
Your webpack will then know how to deal with your files
If you have created your project with create-react-app. In that case, you just need to import your CSS file and image file to your components.
You should do this while importing css files :
import './style.css';
If you want to create this from scratch.
Inside your webpack.config.js , insert this (change the path according to your folder structure):
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"] },
devServer: {
contentBase: path.join(__dirname, "src"),
hot: true,
port: 3000
},
module: {
rules: [
{
// this is so that we can compile any React,
// ES6 and above into normal ES5 syntax
test: /\.(js|jsx)$/,
// we do not want anything from node_modules to be compiled
exclude: /node_modules/,
use: {
loader: "babel-loader"
}
},
{
test: /\.(css|scss)$/,
use: [
"style-loader", // creates style nodes from JS strings
"css-loader", // translates CSS into CommonJS
"sass-loader" // compiles Sass to CSS, using Node Sass by default
]
},
{
test: /\.(jpg|jpeg|png|gif|mp3|svg)$/,
loaders: ["file-loader"]
}
]
},
plugins: [
new HtmlWebpackPlugin({
template: path.join(__dirname, "src", "index.html")
})
]
};
and edit your .babelrc to contain just this:
{
"presets": ["#babel/env", "#babel/react"],
"plugins": ["#babel/plugin-proposal-class-properties"]
}
also your package.json should list these dev-dependencies:
#babel/core,#babel/node,#babel/plugin-proposal-class-properties,#babel/preset-env,#babel/preset-react,babel-jest,babel-loader,css-loader,file-loader,html-webpack-plugin,path,style-loader,webpack,webpack-cli,webpack-dev-server
If this doesn't help please paste the link to your github repo. I will take a look.

Cannot find module '#babel/preset-es2015' from 'C:\repos\ondemandProject\ondemnd-web'

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

Resources