babel-loader
babel-loader copied to clipboard
JSX is not transpiled in babel-loader when babel/preset-react is specified in .babelrc
i really tried to find the problem before posting here but i couldn't, i hope you can help me
i'm using Babel and webpack in React project and i'm getting this ERROR message
:
zacnnnt@zacnnnt:~/projects/VenvDjango/django-react-Tuto/project$ npm run dev
ERROR in ./project/frontend/src/components/App.js 8:2
Module parse failed: Unexpected token (8: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 App = () => (
|
> <DataProvider endpoint="api/lead/" render={data => <Table data={data} />} />
| );
| const wrapper = document.getElementById("app");
@ ./project/frontend/src/index.js 1:0-35
npm ERR! code ELIFECYCLE
npm ERR! errno 2
npm ERR! [email protected] dev: `webpack --mode development ./project/frontend/src/index.js --output ./project/frontend/static/frontend/main.js`
npm ERR! Exit status 2
npm ERR!
npm ERR! Failed at the [email protected] dev script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /home/zacnnnt/.npm/_logs/2019-11-20T21_04_44_609Z-debug.log
App.js
import ReactDOM from "react-dom";
import DataProvider from "./DataProvider";
import Table from "./Table";
const App = () => (
<DataProvider endpoint="api/lead/" render={data => <Table data={data} />} />
);
const wrapper = document.getElementById("app");
wrapper ? ReactDOM.render(<App />, wrapper) : null;
DataProvider.js
import PropTypes from "prop-types";
class DataProvider extends Component {
static propTypes = {
endpoint: PropTypes.string.isRequired,
render: PropTypes.func.isRequired
}
state = {
data: [],
loaded: false,
placeholder: "Loading..."
}
componentDidMount() {
fetch(this.props.endpoint)
.then(response => {
if (response.status !== 200) {
return this.setState({ placeholder: "Something went wrong" });
}
return response.json();
})
.then(data => this.setState({ data: data, loaded: true }));
}
render() {
const { data, loaded, placeholder } = this.state;
return loaded ? this.props.render(data) : <p>{placeholder}</p>;
}
}
export default DataProvider;
@AbdouTlili can you post your webpack/babel config please?
@existentialism package.json
{
"name": "django-react-Tuto",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "webpack --mode development ./project/frontend/src/index.js --output ./project/frontend/static/frontend/main.js",
"build": "webpack --mode production ./project/frontend/src/index.js --output ./project/frontend/static/frontend/main.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@babel/core": "^7.7.2",
"@babel/preset-env": "^7.7.1",
"@babel/preset-react": "^7.7.0",
"babel-loader": "^8.0.6",
"babel-plugin-transform-class-properties": "^6.24.1",
"prop-types": "^15.7.2",
"react": "^16.12.0",
"react-dom": "^16.12.0",
"webpack": "^4.41.2",
"webpack-cli": "^3.3.10"
},
"dependencies": {
"weak-key": "^1.0.1"
}
}
.babelrc
{
"presets": [
"@babel/preset-env","@babel/preset-react"
],
"plugins": [
"transform-class-properties"
]
}
webpack.config.js
module.exports = {
module :{
rules :[
{
test : /\.js$/,
exclude : /node_modules/,
use:{
loader: "bubel-loader"
}
}
]
}
};
loader: "bubel-loader"
Is that a typo?
"transform-class-properties"
You'll want to use the babel 7 compatible version @babel/plugin-proposal-class-properties
still getting same error message :(
webpack.config.js
module.exports = {
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: "babel-loader"
}
}
]
}
};
.babelrc
{
"presets": [
"@babel/preset-env","@babel/preset-react"
],
"plugins":[
"@babel/plugin-proposal-class-properties"
]
}
Do you have multiple package.json
? If so, you should use babel.config.json
instead of .babelrc
(babel.config.*
is always recommended if you are not sure about what file you need).
@AbdouTlili
I did the same tutorial and ran in the same problem. You can solve it by making sure that webpack.config.js and .babelrc are in the top folder of the project (django-drf-react-quickstart).
Hope this helps and have a nice day, Tim
Thanks ! I had the same problem, your solution worked just fine :)
What is the solution if you are in the mono repo?
I have this structure:
/repo /services /myapp webpack.config.js .babelrc /common
Then when I run under myapp: yarn webpack --config webpack.config.js
I get this error:
ERROR in /Users/me/code/repo/node_modules/@libs/common/index.jsx
Module build failed (from /Users/me/code/repo/node_modules/happypack/loader.js):
SyntaxError: /Users/me/code/repo/node_modules/@libs/common/index.jsx: Unexpected token (8:11)
6 | }
7 |
> 8 | return <button type="button" onClick={sayHello}>Click me!</button>;
| ^
9 | };
10 |
at Parser.raise (/Users/me/code/repo/node_modules/@babel/parser/lib/index.js:7044:17)
at Parser.unexpected (/Users/me/code/repo/node_modules/@babel/parser/lib/index.js:8422:16)
at Parser.parseExprAtom (/Users/me/code/repo/node_modules/@babel/parser/lib/index.js:9701:20)
at Parser.parseExprSubscripts (/Users/me/code/repo/node_modules/@babel/parser/lib/index.js:9287:23)
at Parser.parseMaybeUnary (/Users/me/code/repo/node_modules/@babel/parser/lib/index.js:9267:21)
at Parser.parseExprOps (/Users/me/code/repo/node_modules/@babel/parser/lib/index.js:9137:23)
at Parser.parseMaybeConditional (/Users/me/code/repo/node_modules/@babel/parser/lib/index.js:9110:23)
at Parser.parseMaybeAssign (/Users/me/code/repo/node_modules/@babel/parser/lib/index.js:9065:21)
at Parser.parseExpression (/Users/me/code/repo/node_modules/@babel/parser/lib/index.js:9017:23)
at Parser.parseReturnStatement (/Users/me/code/repo/node_modules/@babel/parser/lib/index.js:11091:28)
@ ./src/index.jsx 13:0-49 29:0-17
@timinho111 I am going through that tutorial too, and I have the .babelrc and package.json files both in the root folder (django-react for me) and I am getting the same issue.
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] dev: `webpack --mode development ./leadmanager/frontend/src/index.js --output ./leadmanager/frontend/static/frontend/main.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] dev script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
Is there anything I should look into??
I have the same error, but with babel-plugin
ERROR in ./src/components/button2.tsx 7:6
Module parse failed: Unexpected token (7:6)
File was processed with these loaders:
* ./node_modules/babel-loader/lib/index.js
You may need an additional loader to handle the result of these loaders.
| children: children,
| ...rest
> }) => <Component {...rest} size="l" view="action" width="max">{children}</Component>;
@ ./src/index.tsx 14:0-47 24:36-43
https://github.com/babel/babel/issues/12468
It fixed with the latest @babe/*