REACT-APP未捕获的SyntaxError:无法在模块外部使用import语句

时间:2020-11-09 08:10:53

标签: javascript html node.js reactjs

每当我尝试在浏览器上打印时,总是在标题上出现错误 “未捕获的SyntaxError:无法在模块外部使用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"
    ]
  }
}```

1 个答案:

答案 0 :(得分:0)

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

  1. npm start-在本地主机上启动DEV服务器:3000
  2. npm run build-PROD构建,将源代码编译到build/文件夹中。在那里,您会找到要启动的已编译JS,CSS和index.html。