【发布时间】:2021-02-21 03:35:50
【问题描述】:
每当我尝试在浏览器上打印出来时,我总是在标题上看到错误 “未捕获的语法错误:无法在模块外使用 import 语句” 在问这个问题之前,我尝试了所有与此问题类似的解决方案,包括在我的 json 包中添加“type”:“module”,但似乎没有一个工作,我还添加了“pack”,但仍然没有打印出我的所有脚本都已正确配置我也使用 ecma6script 和最新版本的 react,
下面是我的hmtl代码
<head>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div id="root"></div>
<script src="indexx.js"></script>
</body>
</html>```
and here is my reactcode
```import React from "react";
import ReactDom from "react-dom";
ReactDom.render(<h1>hello world</h1>, document.getElementById("root"));```
below is my json package
```{
"name": "asa-app",
"version": "0.1.0",
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^5.11.5",
"@testing-library/react": "^11.1.1",
"@testing-library/user-event": "^12.2.0",
"react": "^17.0.1",
"react-dom": "^17.0.1",
"react-scripts": "4.0.0",
"web-vitals": "^0.2.4"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}```
【问题讨论】:
-
这看起来不像可以编译。还是您的代码 sn-ps 格式不正确?
-
1)
<script src="indexx.js"></script>是什么意思?不应该是index.js而不是indexx.js吗? 2)听起来你试图在浏览器上打开未编译的代码。当您启动开发模式时会发生什么(npm start或yarn start来自应用根目录的命令)? -
我将索引文件重命名为 indexx 以避免混淆,因为我最初认为它可能会尝试链接到我的其他 index.js 文件而且它只是一个名称,我想这应该不是问题,为什么它看起来不能编译?
-
当我运行(npm start)服务器正常启动时,所有npm功能都正常工作|)
-
在下面的答案中回复了
标签: javascript html node.js reactjs