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.
I need a bit of help with initial setup of webpack to serve my app with hot reloading. I've followed this guide to set up React+Webpack with Typescript, but I'd like to know how I can go about setting it up so I can call "npm start" and have it compile and host the app, with hot reloading.
My webpack.config.js looks like the following:
module.exports = {
mode: "production",
// Enable sourcemaps for debugging webpack's output.
devtool: "source-map",
resolve: {
// Add '.ts' and '.tsx' as resolvable extensions.
extensions: [".ts", ".tsx"]
},
module: {
rules: [
{
test: /\.ts(x?)$/,
exclude: /node_modules/,
use: [
{
loader: "ts-loader"
}
]
},
// All output '.js' files will have any sourcemaps re-processed by 'source-map-loader'.
{
enforce: "pre",
test: /\.js$/,
loader: "source-map-loader"
}
]
},
// When importing a module whose path matches one of the following, just
// assume a corresponding global variable exists and use that instead.
// This is important because it allows us to avoid bundling all of our
// dependencies, which allows browsers to cache those libraries between builds.
externals: {
"react": "React",
"react-dom": "ReactDOM"
}
};
And my package.json looks like this:
{
"name": "client",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "test"
},
"author": "DMW",
"license": "ISC",
"devDependencies": {
"#types/react": "^16.9.15",
"#types/react-dom": "^16.9.4",
"source-map-loader": "^0.2.4",
"ts-loader": "^6.2.1",
"typescript": "^3.7.3",
"webpack": "^4.41.2",
"webpack-cli": "^3.3.10",
"webpack-dev-server": "^3.9.0"
},
"dependencies": {
"react": "^16.12.0",
"react-dom": "^16.12.0"
},
"keywords": []
}
I'd really appreciate it if someone could link me to something to get this working. I'm a bit intimidated by the search results I've found.
EDIT: I ended up using npx create-react-app app --template typescript instead, as detailed here: https://create-react-app.dev/docs/adding-typescript/
Welcome to the community.
Regarding the settings for webpack try adding this to your package.json
"scripts": {
"start": "webpack-dev-server --mode development --open --hot"
},
When you do npm start, start will be taken from this script and would run app as webpack. Also --hot will ensure that hot reload is true, i.e. reload on any changes that you do to the app automatically.
Be sure to change the mode to production when pushing the app to production so that you don't get all the errors and warnings in the production. Hope it helps !!
I get the following error: If you are migrating from Babylon/Babel 6 or want to use the old decorators proposal, you should use the 'decorators-legacy' plugin instead of 'decorators'.
package.json
"#babel/plugin-proposal-decorators": {
"version": "7.1.2",
"resolved": "https://registry.npmjs.org/#babel/plugin-proposal-decorators/-/plugin-proposal-decorators-7.1.2.tgz",
"integrity": "sha512-YooynBO6PmBgHvAd0fl5e5Tq/a0pEC6RqF62ouafme8FzdIVH41Mz/u1dn8fFVm4jzEJ+g/MsOxouwybJPuP8Q==",
"requires": {
"#babel/helper-plugin-utils": "^7.0.0",
"#babel/helper-replace-supers": "^7.1.0",
"#babel/helper-split-export-declaration": "^7.0.0",
"#babel/plugin-syntax-decorators": "^7.1.0"
}
},
"#babel/plugin-syntax-decorators": {
"version": "7.1.0",
"resolved": "https://registry.npmjs.org/#babel/plugin-syntax-decorators/-/plugin-syntax-decorators-7.1.0.tgz",
"integrity": "sha512-uQvRSbgQ0nQg3jsmIixXXDCgSpkBolJ9X7NYThMKCcjvE8dN2uWJUzTUNNAeuKOjARTd+wUQV0ztXpgunZYKzQ==",
"requires": {
"#babel/helper-plugin-utils": "^7.0.0"
}
},
"babel-plugin-syntax-decorators": {
"version": "6.13.0",
"resolved": "http://registry.npmjs.org/babel-plugin-syntax-decorators/-/babel-plugin-syntax-decorators-6.13.0.tgz",
"integrity": "sha1-MSVjtNvePMgGzuPkFszurd0RrAs=",
"dev": true
},
"babel-plugin-transform-decorators-legacy": {
"version": "1.3.5",
"resolved": "https://registry.npmjs.org/babel-plugin-transform-decorators-legacy/-/babel-plugin-transform-decorators-legacy-1.3.5.tgz",
"integrity": "sha512-jYHwjzRXRelYQ1uGm353zNzf3QmtdCfvJbuYTZ4gKveK7M9H1fs3a5AKdY1JUDl0z97E30ukORW1dzhWvsabtA==",
"dev": true,
"requires": {
"babel-plugin-syntax-decorators": "^6.1.18",
"babel-runtime": "^6.2.0",
"babel-template": "^6.3.0"
}
},
"requires": {
"#babel/plugin-proposal-decorators": "7.1.2",
}
tsconfig.json
{
"compilerOptions": {
"experimentalDecorators": true,
"allowJs": true
}
}
The error message is a little bit confusing however with some little bit deep searching, you can resolve it using the following approach.
I make no assumptions, except that you are using webpack in this guide.
You need to add babel proposal decorators to your dev dependencies (You only need it during dev time) (which you have added already).
if using yarn
yarn add --dev #babel/plugin-proposal-decorators
otheriwse for npm
npm install --save-dev #babel/plugin-proposal-decorators
then in your package.json file, locate babel config section or add one if not there. The config name is strictly "babel".
"babel": {
"presets": [
"react-app"
],
"plugins": [
[
"#babel/plugin-proposal-decorators",
{
"legacy": true
}
]
]
}
Pay extra attention to the indentation if typing it by hand. notice the object "#babel/plugin-proposal-decorators" is deeply nested inside two arrays, so it has to be as such to work.
and just for sanity check, your devDependencies would at a minimum be as
"devDependencies": {
"#babel/plugin-proposal-decorators": "^7.1.2"
}
Now you can build your application with either yarn or npm and live happily ever after.
React native 0.59
babel.config.js:
{
"presets": ["module:metro-react-native-babel-preset"],
"plugins": [
["#babel/plugin-transform-flow-strip-types"],
["#babel/plugin-proposal-decorators", { "legacy": true}],
["#babel/plugin-proposal-class-properties", { "loose": true}]
]
}
npm install #babel/plugin-transform-flow-strip-types #babel/plugin-proposal-decorators #babel/plugin-proposal-class-properties --save
Source: https://github.com/facebook/react-native/issues/20588#issuecomment-448218111
{
"presets": ['#babel/preset-env', '#babel/preset-react'],
"plugins": [
["import", { "libraryName": "antd", "libraryDirectory": "es", "style": "less" }],
[
"#babel/plugin-proposal-decorators",
{
"decoratorsBeforeExport":true
}
]
]
}
"babel": {
"presets": [
"react-app"
],
"plugins": [
[
"#babel/plugin-proposal-decorators",
{
"legacy": true
}
],
[
"#babel/plugin-proposal-class-properties",
{
"loose": true
}
]
]
},
"devDependencies": {
"#babel/plugin-proposal-decorators": "^7.3.0"
}
Answer are in the official document:
https://mobx.js.org/best/decorators.html
you can find many ways to enable it in section "Enabling decorator syntax"
take the Babel 7 for example, create a project using mobx+create-react-app from scratch:
npx create-react-app hello-mobx
//This moves files around and makes your app’s configuration accessible.
npm run eject
npm install --save-dev babel-plugin-transform-decorators-legacy
npm install --save-dev #babel/plugin-proposal-decorators
npm install --save-dev #babel/plugin-proposal-class-properties
edit package.json:
package.json:
"babel": {
"plugins":[
[
"#babel/plugin-proposal-decorators",
{
"legacy":true
}
],
[
"#babel/plugin-proposal-class-properties",
{
"loose":true
}
]
],
"presets":[
"react-app"
]
}
install mobx:
npm install mobx --save
npm install mobx-react --save
enjoy!
While I was setting up React within Django project I came across this error
ModuleBuildError in
Module build failed (from ./node_modules/babel-loader/lib/index.js):
SyntaxError: C:\Users\1Sun\Cebula3\cebula_react\assets\js\index.js: Support
for the experimental syntax 'classProperties' isn't currently enabled (17:9):
15 |
16 | class BodyPartWrapper extends Component {
> 17 | state = {
| ^
18 |
19 | }
20 |
Add #babel/plugin-proposal-class-properties (https://git.io/vb4SL) to the
'plugins' section of your Babel config to enable transformation.
So, I installed #babel/plugin-proposal-class-properties and put this in babelrc
package.json
{
"name": "cebula_react",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "webpack-dev-server --config ./webpack.config.js --mode development",
"test": "echo \"Error: no test specified\" && exit 1",
"build": "webpack --config prod.config.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"babel": {
"presets": [
"#babel/preset-env",
"#babel/preset-react"
]
},
"devDependencies": {
"#babel/cli": "^7.0.0",
"#babel/core": "^7.0.0",
"#babel/plugin-proposal-class-properties": "^7.0.0",
"#babel/preset-env": "^7.0.0",
"#babel/preset-react": "^7.0.0",
"babel-loader": "^8.0.2",
"babel-plugin-transform-class-properties": "^6.24.1",
"react-hot-loader": "^4.3.6",
"webpack": "^4.17.2",
"webpack-bundle-tracker": "^0.3.0",
"webpack-cli": "^3.1.0",
"webpack-dev-server": "^3.1.8"
},
"dependencies": {
"react": "^16.5.0",
"react-dom": "^16.5.0"
}
}
babelrc
{
"presets": [
"#babel/preset-env",
"#babel/preset-react"
],
"plugins": [
"#babel/plugin-proposal-class-properties"
]
}
However the error is still existed, What is the problem??
Change
"plugins": [
"#babel/plugin-proposal-class-properties"
]
To
"plugins": [
[
"#babel/plugin-proposal-class-properties",
{
"loose": true
}
]
]
This worked for me
First install the: #babel/plugin-proposal-class-properties as dev dependency:
npm install #babel/plugin-proposal-class-properties --save-dev
Then edit your .babelrc so it will be exact like this:
{
"presets": [
"#babel/preset-env",
"#babel/preset-react"
],
"plugins": [
[
"#babel/plugin-proposal-class-properties"
]
]
}
.babelrc file located in the root directory, where package.json is.
Note that you should re-start your webpack dev server to changes take affect.
Solution for webpack project
I just solve this problem by adding #babel/plugin-proposal-class-properties into webpack config plugin.
The module section of my webpack.config.js looks like this
module: {
rules: [
{
test: path.join(__dirname, '.'),
exclude: /(node_modules)/,
loader: 'babel-loader',
options: {
presets: ['#babel/preset-env',
'#babel/react',{
'plugins': ['#babel/plugin-proposal-class-properties']}]
}
}
]
}
{
"presets": [
"#babel/preset-env",
"#babel/preset-react"
],
"plugins": [
[
"#babel/plugin-proposal-class-properties"
]
]
}
replace your .babelrc file with above code. it fixed the issue for me.
In my work environment root, .babelrc file was not there. However, following entry in package.json solved the issue.
"babel": {
"presets": [
"#babel/preset-env",
"#babel/preset-react"
],
"plugins": [
"#babel/plugin-proposal-class-properties"
]}
Note: Don't forget to exit the console and reopen before executing the npm or yarn commands.
There are two ways to work with react state:
Option 1:
Just add to package.json:
"babel": {
"presets": [
"#babel/preset-env",
"#babel/preset-react"
],
"plugins": [
"#babel/plugin-proposal-class-properties"
]
}
Option 2:
1. Creta a file called .babelrc in the root folder.
Write in .babelrc:
{ "plugins": ["#babel/plugin-proposal-class-properties"] }
Run:
npm i #babel/plugin-proposal-class-properties
3. Run:
npm run dev
or
npm run watch
After almost 3 hours of searching and spending time on the same error, I found that I'm using name import for React:
import { React } from 'react';
which is totally wrong. Just by switching it to:
import React from 'react';
all the error are gone.
I hope this helps someone. This is my .babelrc:
{
"presets": [
"#babel/preset-env",
"#babel/preset-react"
],
"plugins": [
"#babel/plugin-proposal-class-properties"
]
}
the webpack.config.js
const path = require('path');
const devMode = process.env.Node_ENV !== 'production';
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
module.exports = {
entry: './src/App.js',
devtool: 'source-map',
output: {
path: path.resolve(__dirname, 'public'),
filename: 'App.js'
},
mode: 'development',
devServer: {
contentBase: path.resolve(__dirname, 'public'),
port:9090,
open: 'google chrome',
historyApiFallback: true
},
module: {
rules: [
{
test: /\.m?js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader'
}
},{
test: /\.(sa|sc|c)ss$/,
use: [
devMode ? 'style-loader' : MiniCssExtractPlugin.loader,
{
loader: 'css-loader',
options: {
modules: true,
localIdentName: '[local]--[hash:base64:5]',
sourceMap: true
}
},{
loader: 'sass-loader'
}
]
}
]
},
plugins: [
new MiniCssExtractPlugin({
filename: devMode ? '[name].css' : '[name].[hash].css',
chunkFilename: devMode ? '[id].css' : '[id].[hash].css'
})
]
}
the package.json
{
"name": "expense-app",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"build": "webpack",
"serve": "webpack-dev-server"
},
"author": "",
"license": "ISC",
"devDependencies": {
"#babel/cli": "^7.1.2",
"#babel/core": "^7.1.2",
"#babel/plugin-proposal-class-properties": "^7.1.0",
"#babel/preset-env": "^7.1.0",
"#babel/preset-react": "^7.0.0",
"babel-loader": "^8.0.4",
"css-loader": "^1.0.0",
"mini-css-extract-plugin": "^0.4.3",
"node-sass": "^4.9.3",
"react-router-dom": "^4.3.1",
"sass-loader": "^7.1.0",
"style-loader": "^0.23.1",
"webpack": "^4.20.2",
"webpack-cli": "^3.1.2",
"webpack-dev-server": "^3.1.9"
},
"dependencies": {
"normalize.css": "^8.0.0",
"react": "^16.5.2",
"react-dom": "^16.5.2"
}
}
Moving the state inside the constructor function worked for me:
...
class MyComponent extends Component {
constructor(man) {
super(man)
this.state = {}
}
}
...
Good Luck...
Install the plugin-proposal-class-properties
npm install #babel/plugin-proposal-class-properties --save-dev
Update your webpack.config.js by adding
'plugins': ['#babel/plugin-proposal-class-properties']}]
I find the problem that my .babelrc was ignored, However I create babel.config.js and add the following:
module.exports = {
plugins: [
['#babel/plugin-proposal-decorators', { legacy: true }],
['#babel/plugin-proposal-class-properties', { loose: true }],
'#babel/plugin-syntax-dynamic-import',
'#babel/plugin-transform-regenerator',
[
'#babel/plugin-transform-runtime',
{
helpers: false,
regenerator: true,
},
],
],
presets: [
"#babel/preset-flow",
'module:metro-react-native-babel-preset',
],
};
And it works for me on React Native application, I think this also would help React apps as well.
According to this GitHub issue if you using create-react-app you should copy your .babelrc or babel.config.js configurations to webpack.config.js and delete those.because of htis two line of code babelrc: false,configFile: false, your config in babelrc,.. are useless.
and your webpack.config.js is in your ./node_madules/react-scripts/config folder
I solved my problem like this:
{
test: /\.(js|mjs)$/,
exclude: /#babel(?:\/|\\{1,2})runtime/,
loader: require.resolve('babel-loader'),
options: {
babelrc: false,
configFile: false,
compact: false,
presets: [
[
require.resolve('babel-preset-react-app/dependencies'),
{ helpers: true },
],
'#babel/preset-env', '#babel/preset-react'
],
plugins: ['#babel/plugin-proposal-class-properties'],
.
.
.
I am using the babel parser explicitly. None of the above solutions worked for me. This worked.
const ast = parser.parse(inputCode, {
sourceType: 'module',
plugins: [
'jsx',
'classProperties', // '#babel/plugin-proposal-class-properties',
],
});
I'm using yarn. I had to do the following to overcome the error.
yarn add #babel/plugin-proposal-class-properties --dev
Adding
"plugins": [
[
"#babel/plugin-proposal-class-properties"
]
]
into .babelrc works for me.
yarn add --dev #babel/plugin-proposal-class-properties
or
npm install #babel/plugin-proposal-class-properties --save-dev
.babelrc
For ejected create-react-app projects
I just solved my case adding the following lines to my webpack.config.js:
presets: [
[
require.resolve('babel-preset-react-app/dependencies'),
{ helpers: true },
],
/* INSERT START */
require.resolve('#babel/preset-env'),
require.resolve('#babel/preset-react'),
{
'plugins': ['#babel/plugin-proposal-class-properties']
}
/* INSERTED END */
],
If some one working on monorepo following react-native-web-monorepo than you need to config-overrides.js file in packages/web. you need to add resolveApp('../../node_modules/react-native-ratings'), in that file...
My complete config-override.js file is
const fs = require('fs');
const path = require('path');
const webpack = require('webpack');
const appDirectory = fs.realpathSync(process.cwd());
const resolveApp = relativePath => path.resolve(appDirectory, relativePath);
// our packages that will now be included in the CRA build step
const appIncludes = [
resolveApp('src'),
resolveApp('../components/src'),
resolveApp('../../node_modules/#react-navigation'),
resolveApp('../../node_modules/react-navigation'),
resolveApp('../../node_modules/react-native-gesture-handler'),
resolveApp('../../node_modules/react-native-reanimated'),
resolveApp('../../node_modules/react-native-screens'),
resolveApp('../../node_modules/react-native-ratings'),
resolveApp('../../node_modules/react-navigation-drawer'),
resolveApp('../../node_modules/react-navigation-stack'),
resolveApp('../../node_modules/react-navigation-tabs'),
resolveApp('../../node_modules/react-native-elements'),
resolveApp('../../node_modules/react-native-vector-icons'),
];
module.exports = function override(config, env) {
// allow importing from outside of src folder
config.resolve.plugins = config.resolve.plugins.filter(
plugin => plugin.constructor.name !== 'ModuleScopePlugin'
);
config.module.rules[0].include = appIncludes;
config.module.rules[1] = null;
config.module.rules[2].oneOf[1].include = appIncludes;
config.module.rules[2].oneOf[1].options.plugins = [
require.resolve('babel-plugin-react-native-web'),
require.resolve('#babel/plugin-proposal-class-properties'),
].concat(config.module.rules[2].oneOf[1].options.plugins);
config.module.rules = config.module.rules.filter(Boolean);
config.plugins.push(
new webpack.DefinePlugin({ __DEV__: env !== 'production' })
);
return config
};
I faced the same issue while trying to transpile some jsx with babel. Below is the solution that worked for me. You can add the following json to your .babelrc
{
"presets": [
[
"#babel/preset-react",
{ "targets": { "browsers": ["last 3 versions", "safari >= 6"] } }
]
],
"plugins": [["#babel/plugin-proposal-class-properties"]]
}
For the react projects with webpack:
Do: npm install #babel/plugin-proposal-class-properties --save-dev
Create .babelrc (if not present) file in the root folder where package.json and webpack.config.js are present and add below code to that:
{
"presets": ["#babel/preset-env", "#babel/preset-react"],
"plugins": [
[
"#babel/plugin-proposal-class-properties",
{
"loose": true
}
]
]
}
Add below code to the webpack.config.js file:
{
test: /\.jsx?$/,
exclude: /node_modules/,
loader: 'babel-loader',
query: {
presets: ["#babel/preset-env", "#babel/preset-react"]
},
resolve: {
extensions: ['.js', '.jsx']
}
}
Close the terminal and run npm start again.
you must install
npm install #babel/core #babel/plugin-proposal-class-properties #babel/preset-env #babel/preset-react babel-loader
and
change entry and output
const path = require('path')
module.exports = {
entry: path.resolve(__dirname,'src', 'app.js'),
output: {
path: path.resolve(__dirname, "public","dist",'javascript'),
filename: 'bundle.js'
},
module: {
rules: [
{
test: /\.(jsx|js)$/,
exclude: /node_modules/,
use: [{
loader: 'babel-loader',
options: {
presets: [
['#babel/preset-env', {
"targets": "defaults"
}],
'#babel/preset-react'
],
plugins: [
"#babel/plugin-proposal-class-properties"
]
}
}]
}
]
}
}
I created a symlink for src/components -> ../../components that caused npm start to go nuts and stop interpreting src/components/*.js as jsx, thus giving that same error. All instructions to fix it from official babel were useless.
When I copied back the components directory everything was BACK TO NORMAL!
Ensure you have not mistakenly imported import BrowserRouter from "react-router-dom/modules/BrowserRouter"; instead of import {BrowserRouter} from "react-router-dom";
If this happens after typescript update, just add useDefineForClassFields: false to tsconfig file.
See: https://www.typescriptlang.org/tsconfig#useDefineForClassFields