[英]"Uncaught SyntaxError: Cannot use import statement outside a module" When Importing ReactJS
Today I decided to try to learn React, but whenever I try to import the two modules below:今天我决定尝试学习 React,但每当我尝试导入以下两个模块时:
import React from "react"
import ReactDOM from "react-dom"
I get the error:我收到错误:
Uncaught SyntaxError: Cannot use import statement outside a module未捕获的语法错误:无法在模块外使用导入语句
Here's the steps I've taken to try to create my React program:以下是我尝试创建 React 程序所采取的步骤:
Why am I getting this error, and how can I fix it?为什么会出现此错误,我该如何解决?
I assume this is your first time working with React?我想这是你第一次使用 React? I see that you have installed NodeJs, which is great!我看到你已经安装了 NodeJs,太棒了!
I recommend that you get started with a complete React app such as Create React App.我建议您开始使用完整的 React 应用程序,例如 Create React App。
You need to add this to your package.json and it should work.您需要将此添加到您的 package.json 中,它应该可以工作。 If you are going to use es6 import statements you need to tell node you are using modules inside of your package.json like this.如果您要使用 es6 导入语句,您需要告诉节点您正在使用 package.json 内部的模块,就像这样。
"type": "module",
I am also new to react and using visual studio2022 to write the react code in MVC project.我也是新来反应和使用 visual studio2022 在 MVC 项目中编写反应代码。 I created login page exactly same as https://github.com/narendra315/yt-210911-react-login-page/blob/master/src/LoginComponent2.js and was getting same error as我创建的登录页面与https://github.com/narendra315/yt-210911-react-login-page/blob/master/src/LoginComponent2.js完全相同,并且出现与
Uncaught SyntaxError: Cannot use import statement outside a module未捕获的语法错误:无法在模块外使用导入语句
fixed it by removing the import as well as export line at the end.通过删除最后的导入和导出行来修复它。 I replaced the export line with我将导出行替换为
ReactDOM.render(<LoginComponent />, document.getElementById('content'));
where LoginComponent is my class and content is id of my Index.cshtml page This fixed my issue.其中 LoginComponent 是我的 class,内容是我的 Index.cshtml 页面的 ID 这解决了我的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.