pau*_*l45 25 javascript reactjs babeljs babel-loader
我第一次使用 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
Run Code Online (Sandbox Code Playgroud)
得出该错误消息的完整代码:
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'));
Run Code Online (Sandbox Code Playgroud)
我觉得我的 webpack-config.js 有问题,但我直接从我正在使用的教程中复制了它,所以我不确定为什么会出错。这是,供参考:
module.exports = {
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: "babel-loader"
}
}
]
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的 package.json 中的包依赖项:
"dependencies": {
"prop-types": "^15.7.2",
"react": "^16.13.1",
"react-dom": "^16.13.1"
},
Run Code Online (Sandbox Code Playgroud)
最后是我的 .babelrc
{
"presets": ["@babel/preset-env","@babel/preset-react"],
"plugins": ["transform-class-properties".js]
}
Run Code Online (Sandbox Code Playgroud)
我真的不知道发生了什么事,所以任何帮助将不胜感激。如果我遗漏了任何可能有帮助的相关信息,请告诉我。谢谢。
Por*_*lus 10
错误来自这一行: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.babelrc:您不希望.js在 之后出现该错误"transform-class-properties"。webpack-config.js为webpack.config.js这是一个展示这一点的教程: https: //www.valentinog.com/blog/babel/。另外,您可以使用https://github.com/facebook/create-react-app来简化所有这一切并提供一个很好的起始配置。
| 归档时间: |
|
| 查看次数: |
92521 次 |
| 最近记录: |