[英]React/Zustand - Module parse failed: Unexpected token. You may need an appropriate loader to handle this file type
[英]React - Module parse failed: Unexpected Token. You may need an appropriate loader to handle this file type
我第一次使用 react,如果我做的事情明显而且非常错误,请道歉。 也就是说,我在这里和 github 上阅读了几个看起来相似的错误,但我找不到任何适合我的东西。 这是完整的错误消息:
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
从中提取该错误消息的完整代码:
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'));
我觉得我的 webpack-config.js 有问题,但我直接从我正在使用的教程中复制了它,所以我不确定为什么会出错。 在这里,供参考:
module.exports = {
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: "babel-loader"
}
}
]
}
}
这是我的 package 依赖项来自 package.json:
"dependencies": {
"prop-types": "^15.7.2",
"react": "^16.13.1",
"react-dom": "^16.13.1"
},
最后是 my.babelrc
{
"presets": ["@babel/preset-env","@babel/preset-react"],
"plugins": ["transform-class-properties".js]
}
我真的不知道发生了什么,所以任何帮助将不胜感激。 如果我遗漏了任何可能有用的相关信息,请告诉我。 谢谢你。
错误来自这一行: return <h1>React App</h1>
,因为<h1>...</h1>
不是有效的 javascript。 即使重命名它会被解析为 vanilla js 而不是 jsx,因为你的webpack-config.js
,所以你应该做很多事情来修复它:
App.js
重命名为App.jsx
,test: /\\.js$/,
test: /\\.(js|jsx)$/,
在 webpack-config.js 中test: /\\.(js|jsx)$/,
(js|jsx)$/.babelrc
也有一个错误:在"transform-class-properties"
之后,您不希望.js
出现在那里。webpack-config.js
重命名为webpack.config.js
这是一个显示这一点的教程: https : //www.valentinog.com/blog/babel/ 。 此外,您可以使用https://github.com/facebook/create-react-app来简化所有这些并提供一个很好的起始配置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.