[英]React - Module parse failed: Unexpected Token. You may need an appropriate loader to handle this file type
My first time using react, so apologies if I'm doing something obvious and incredibly wrong.我第一次使用 react,如果我做的事情明显而且非常错误,请道歉。 That said, I've read up on several similar-seeming bugs on here and on github and I can't find anything that works for mine.也就是说,我在这里和 github 上阅读了几个看起来相似的错误,但我找不到任何适合我的东西。 Here's the full error message:这是完整的错误消息:
ERROR in ./src/frontend/src/components/App.js 6:15
Module parse failed: Unexpected token (6:15)
You may need an appropriate loader to handle this file type.
| class App extends Component{
| render() {
> return <h1>React App</h1>
| }
| }
@ ./src/frontend/src/index.js 1:0-35
The full code from which that error message was drawn:从中提取该错误消息的完整代码:
import React, { Component } from 'react';
import ReactDOM from 'react-dom';
class App extends Component{
render() {
return <h1>React App</h1>
}
}
ReactDOM.render(<App />, document.getElementById('app'));
I feel like something is wrong with my webpack-config.js, but I copied that directly from a tutorial I was using so I'm not sure why it would be wrong.我觉得我的 webpack-config.js 有问题,但我直接从我正在使用的教程中复制了它,所以我不确定为什么会出错。 Here it is, for reference:在这里,供参考:
module.exports = {
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: "babel-loader"
}
}
]
}
}
And here are my package dependencies from package.json:这是我的 package 依赖项来自 package.json:
"dependencies": {
"prop-types": "^15.7.2",
"react": "^16.13.1",
"react-dom": "^16.13.1"
},
And finally my.babelrc最后是 my.babelrc
{
"presets": ["@babel/preset-env","@babel/preset-react"],
"plugins": ["transform-class-properties".js]
}
I really have no idea what's going on, so any help would be much appreciated.我真的不知道发生了什么,所以任何帮助将不胜感激。 Please let me know if I left out any relevant info that could be helpful.如果我遗漏了任何可能有用的相关信息,请告诉我。 Thank you.谢谢你。
The error comes from this line: return <h1>React App</h1>
, because <h1>...</h1>
is not valid javascript.错误来自这一行: return <h1>React App</h1>
,因为<h1>...</h1>
不是有效的 javascript。 Even if rename this is would be parsed as vanilla js and not jsx, because of your webpack-config.js
, so there are multiple things you should do to fix it:即使重命名它会被解析为 vanilla js 而不是 jsx,因为你的webpack-config.js
,所以你应该做很多事情来修复它:
App.js
to App.jsx
,将App.js
重命名为App.jsx
,test: /\\.js$/,
to test: /\\.(js|jsx)$/,
in webpack-config.js更新test: /\\.js$/,
test: /\\.(js|jsx)$/,
在 webpack-config.js 中test: /\\.(js|jsx)$/,
(js|jsx)$/.babelrc
: you don't want that .js
there, after "transform-class-properties"
.我认为您的.babelrc
也有一个错误:在"transform-class-properties"
之后,您不希望.js
出现在那里。webpack-config.js
to webpack.config.js
将webpack-config.js
重命名为webpack.config.js
Here is a tutorial that shows this: https://www.valentinog.com/blog/babel/ .这是一个显示这一点的教程: https : //www.valentinog.com/blog/babel/ 。 Also, you could use https://github.com/facebook/create-react-app that simplifies all of this and provides a great starting config.此外,您可以使用https://github.com/facebook/create-react-app来简化所有这些并提供一个很好的起始配置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.