简体   繁体   English

“未捕获的语法错误:无法在模块外使用导入语句”导入时 ReactJS

[英]"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 程序所采取的步骤:

  1. Install NodeJS安装 Node.js
  2. Add NodeJS to environmental variables将 NodeJS 添加到环境变量
  3. Create new folder for my program为我的程序创建新文件夹
  4. Create HTML, CSS, and JS files in my folder在我的文件夹中创建HTML、CSS、JS文件

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。

https://create-react-app.dev/ https://create-react-app.dev/

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.

相关问题 导入类时“不能在模块外使用导入语句”和“未捕获的语法错误:意外的标识符” - "Cannot use import statement outside a module" and "Uncaught SyntaxError: Unexpected identifier" when importing a class 将已编译的 Typescript 文件相互导入时出现“未捕获的语法错误:无法在模块外使用 import 语句” - “Uncaught SyntaxError: Cannot use import statement outside a module” when importing compiled Typescript files to each other 未捕获的语法错误:导入 React 时无法在模块外使用导入语句 - Uncaught SyntaxError: Cannot use import statement outside a module" When Importing React 导入 ECMAScript 6 时出现“Uncaught SyntaxError: Cannot use import statement outside a module” - "Uncaught SyntaxError: Cannot use import statement outside a module" when importing ECMAScript 6 未捕获的语法错误:无法在使用 reactjs CDN 链接的模块外部使用导入语句 - Uncaught SyntaxError: Cannot use import statement outside a module using reactjs CDN Links 导入 Axios 时出错:未捕获语法错误:无法在模块外使用导入语句 - Error While Importing Axios: Uncaught SyntaxError: Cannot use import statement outside a module “SyntaxError: Cannot use import statement outside a module” 导入模块时导入 Jest 中的副作用模块 - "SyntaxError: Cannot use import statement outside a module" when importing a module which imports a module for side effects in Jest 未捕获的语法错误:无法在模块外使用导入语句 - Javascript - Uncaught SyntaxError: Cannot use import statement outside a module - Javascript 未捕获的语法错误:无法在节点 js 中的模块外使用导入语句 - Uncaught SyntaxError: Cannot use import statement outside a module in node js 未捕获的语法错误:无法在具有 OpenLayers 的模块外部使用 import 语句 - Uncaught SyntaxError: Cannot use import statement outside a module with OpenLayers
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM