gpt4 book ai didi

javascript - Babel 显示意外错误,找不到解决方案

转载 作者:行者123 更新时间:2023-12-03 01:15:47 26 4
gpt4 key购买 nike

我正在尝试运行 JSX 代码,但收到此错误。我确实安装了npm install -g babel-clinpm install --save babel-preset-env babel-preset-react但我仍然收到错误。

这是我的代码index.html

 <html>
<body>
<div id="appRoot"></div>
<script src="https://unpkg.com/react@16.0.0/umd/react.production.min.js"></script>
<script src="https://unpkg.com/react-dom@16.0.0/umd/react-dom.production.min.js"></script>
<script src="scripts/app.js" type="text/jsx"></script>
</body>
</html>

src/app.js -> JSX 代码

console.log("App.js is running");

const appRoot = document.querySelector("#appRoot");
const template = <p>This is a JSX code!</p>;

ReactDOM.render(template, appRoot);

babel 命令 -> 将 JSX 转换为 ES5 的 babel 命令

babel src/app.js --out-file=public/scripts/app.js --preset=env,react -watch

错误

subhro@subhro-X550LD:~/React-Project$ babel src/app.js --out-file=public/scripts/app.js --preset=env,react -watch
SyntaxError: src/app.js: Unexpected token (4:17)
2 |
3 | const appRoot = document.querySelector("#appRoot");
> 4 | const template = <p>This is a JSX code!</p>;
| ^
5 |
6 | ReactDOM.render(template, appRoot);

最佳答案

你有错字:

预设 => 预设

babel src/app.js --out-file=public/scripts/app.js --presets=env,react -watch

关于javascript - Babel 显示意外错误,找不到解决方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52023891/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com