简体   繁体   English

我的控制台显示此错误“未捕获的语法错误:无法在模块外使用导入语句(位于 bundle.js:47878:2)”。 我正在构建一个 React 应用程序

[英]My console shows this error "Uncaught SyntaxError: Cannot use import statement outside a module (at bundle.js:47878:2)". I am building a React app

 import axios from 'axios';
import React, { useEffect, useState } from 'react'

import Navbar from './Navbar'

    const Home = () => {
        var [Employeelist,setEmployeelist] = useState([]);
        useEffect(()=>{
          getData();
        },[])
        const getData=()=>{
            axios.get('https://jsonplaceholder.typicode.com/users')
            .then((response)=>{
              console.log(response.data)
                    setEmployeelist(response.data)
            })
            .catch()
        }
        
      return (
        <div>
            <Navbar/>
            <br /><br />
            <div className="container">
                <div className="row">
                    <div className="col col-12 col-sm-12 col-md-12 col-lg-12 col-xl-12 col-xxl-12">
                        <div className="row g-4">
                            <div className="col col-12 col-sm-12 col-md-12 col-lg-12 col-xl-12 col-xxl-12">
                            <h2 style={{textAlign:"center",textDecoration:"underline",fontStyle:'italic'}}>Employee data</h2>
                            <table class="table table-success table-striped  table-hover" style={{border:'solid 2px black', borderRadius:'20px !important'}}>
      <thead className="table-dark">
        <tr>
          <th scope="col">id</th>
          <th scope="col">Name</th>
          <th scope="col">Email</th>
        </tr>
      </thead>
     {Employeelist.map((value,index)=>{
        return  <tbody class="table-group-divider">
        <tr>
          <th scope="row">{value.id}</th>
          <td>{value.name}</td>
          <td>{value.email}</td>
        </tr>
      </tbody>
    
     })}
     </table>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
      )
    }

export default Home

This is my code这是我的代码

When i start the server it shows compiled successfully but nothing shows on the screen and when i open the console it shows "Uncaught SyntaxError: Cannot use import statement outside a module (at bundle.js:47878:2)" I tried adding "type":"module" into the package.json file as shown inn somethreads but still not useful当我启动服务器时,它显示编译成功但屏幕上没有任何显示,当我打开控制台时它显示“未捕获的语法错误:无法在模块外使用导入语句(在 bundle.js:47878:2)”我尝试添加“类型":"module" 进入 package.json 文件,如 inn somethreads 所示,但仍然没有用

The issue is, the browser cannot use modules so you need a bundler like ( Browserify or Vite ) which bundles your modules and code into a single file so it can be used by the browser.问题是,浏览器不能使用模块,所以你需要一个像( BrowserifyVite )这样的捆绑器,它将你的模块和代码捆绑到一个文件中,以便浏览器可以使用它。 More information about modules and bundlers here有关模块和捆绑器的更多信息,请参见此处

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 parcel v2 捆绑 JavaScript 时出现“Uncaught SyntaxError: Cannot use import statement outside a module”错误 - "Uncaught SyntaxError: Cannot use import statement outside a module" error while using parcel v2 to bundle JavaScript 未捕获的语法错误:无法在 React 模块外使用导入语句 - Uncaught SyntaxError: Cannot use import statement outside a module for React 未捕获的语法错误:无法在节点 js 中的模块外使用导入语句 - Uncaught SyntaxError: Cannot use import statement outside a module in node js JS:未捕获的语法错误:无法在模块外使用导入语句 - JS : Uncaught SyntaxError: Cannot use import statement outside a module REACT-APP 未捕获的语法错误:无法在模块外使用导入语句 - REACT-APP Uncaught SyntaxError: Cannot use import statement outside a module 为什么在使用 cloudinary 模块时出现“语法错误:无法在模块外使用导入语句”错误? - Why am I getting the 'SyntaxError: Cannot use import statement outside a module' error while using the cloudinary module? 尝试在 Django 应用程序的 js 文件中导入 vue.js 时出现“未捕获的语法错误:无法在模块外使用导入语句” - “Uncaught SyntaxError: Cannot use import statement outside a module” when trying to import vue.js inside js file in Django app Jest.js 和 Create-react-app:运行测试时出现“语法错误:无法在模块外使用导入语句” - Jest.js and Create-react-app: I get "SyntaxError: Cannot use import statement outside a module" when running test 当我尝试为 Vue.js 导入插件时出现“未捕获的语法错误:无法在模块外使用导入语句” - Getting "Uncaught SyntaxError: Cannot use import statement outside a module" when i try to import a plugin for Vue.js 当我尝试在我的 React 应用程序中运行 npm 测试时出现错误“SyntaxError: Cannot use import statement outside a module” - The error "SyntaxError: Cannot use import statement outside a module" appears when I try to run npm test in my react application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM