【问题标题】:REACT-APP Uncaught SyntaxError: Cannot use import statement outside a moduleREACT-APP Uncaught SyntaxError:无法在模块外使用导入语句
【发布时间】: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) &lt;script src="indexx.js"&gt;&lt;/script&gt; 是什么意思?不应该是index.js 而不是indexx.js 吗? 2)听起来你试图在浏览器上打开未编译的代码。当您启动开发模式时会发生什么(npm startyarn start 来自应用根目录的命令)?
  • 我将索引文件重命名为 indexx 以避免混淆,因为我最初认为它可能会尝试链接到我的其他 index.js 文件而且它只是一个名称,我想这应该不是问题,为什么它看起来不能编译?
  • 当我运行(npm start)服务器正常启动时,所有npm功能都正常工作|)
  • 在下面的答案中回复了

标签: javascript html node.js reactjs


【解决方案1】:

您正在尝试直接从浏览器启动未编译的源代码。要启动一个可行的应用程序,您可以执行以下操作:

  1. npm start - DEV 服务器在 localhost:3000 上启动
  2. npm run build - PROD 构建,将源代码编译到 build/ 文件夹中。在那里你会找到编译好的 JS、CSS 和 index.html 来启动。

【讨论】:

    猜你喜欢
    • 2023-02-08
    • 1970-01-01
    • 2021-10-02
    • 1970-01-01
    • 1970-01-01
    • 2020-12-15
    • 2021-07-08
    • 2022-09-28
    • 1970-01-01
    相关资源
    最近更新 更多