未捕获的引用错误:未在 index-a136b960.js:3888:2474 中定义 require

Uncaught ReferenceError: require is not defined at index-a136b960.js:3888:2474

提问人:Adil Mahmoud Rion 提问时间:10/5/2023 更新时间:10/5/2023 访问量:55

问:

当我托管我的网站时,它没有显示任何内容并给出错误

“未捕获的 ReferenceError:未定义 require 在索引-A136B960.js:3888:2474”

但是这个文件在我的根文件夹或项目中不可用,当我在这个站点 localhos 中运行时,它按预期运行并且没有显示任何错误。

在此之下,我在这个项目中的 JSON 文件。

在此处输入图像描述

在此处输入图像描述

实时站点链接: https://nasa-space-apps-challenge-2023.vercel.app/

{
  "name": "first-3d-app",
  "private": true,
  "version": "0.0.0",
  "type": "module",
  "scripts": {
    "dev": "vite",
    "build": "vite build",
    "lint": "eslint . --ext js,jsx --report-unused-disable-directives --max-warnings 0",
    "preview": "vite preview"
  },
  "dependencies": {
    "@canvasjs/react-charts": "^1.0.0",
    "@heroicons/react": "^2.0.18",
    "@material-tailwind/react": "^2.1.2",
    "@react-three/drei": "^9.85.2",
    "@react-three/fiber": "^8.14.4",
    "@tanstack/react-query": "^4.35.7",
    "axios": "^1.5.1",
    "leaflet": "^1.9.4",
    "react": "^18.2.0",
    "react-dom": "^18.2.0",
    "react-leaflet": "^4.2.1",
    "react-router-dom": "^6.16.0",
    "react-three-fiber": "^6.0.13",
    "three": "^0.156.1",
    "vite-plugin-require": "^1.1.11",
    "zustand": "^4.4.2"
  },
  "devDependencies": {
    "@types/react": "^18.2.15",
    "@types/react-dom": "^18.2.7",
    "@vitejs/plugin-react": "^4.0.3",
    "autoprefixer": "^10.4.16",
    "eslint": "^8.45.0",
    "eslint-plugin-react": "^7.32.2",
    "eslint-plugin-react-hooks": "^4.6.0",
    "eslint-plugin-react-refresh": "^0.4.3",
    "postcss": "^8.4.31",
    "tailwindcss": "^3.3.3",
    "vite": "^4.4.5"
  }
}

我该如何解决这个问题

JavaScript ReactJS的 React-Leaflet React-Three-Fiber CanvasJS

评论


答:

0赞 Kamran 10/5/2023 #1

根据您提供的详细信息,我认为您没有使用任何服务器来托管前端构建。尝试添加一个基本的快速服务器,并在其上提供您的构建内容。

评论

0赞 Adil Mahmoud Rion 10/5/2023
我怎样才能添加这个?
0赞 Kamran 10/6/2023
正如我所说,创建一个基本的 Express 应用程序并提供静态文件,您可以检查以下内容:expressjs.com/en/starter/hello-world.htmlexpressjs.com/en/starter/static-files.html
0赞 Adil Mahmoud Rion 10/6/2023
const express = require('express');常量路径 = require('路径');const app = express();const port = process.env.PORT ||3001;从 'dist' 文件夹(假设 Vite 构建输出文件夹)提供静态文件:app.use(express.static(path.join(__dirname, 'dist')));为所有请求提供 index.html 文件 app.get('*', (req, res) => { res.sendFile(path.join(__dirname, 'dist', 'index.html'));启动服务器 app.listen(port, () => { console.log();我正在创建这样的快速服务器,但无法正常工作Server is running on port ${port}
0赞 Kamran 10/6/2023
您是否在 env 中设置了 PORT?
0赞 Kamran 10/6/2023
如果 Vercel 生成 https URL,则保留 443,如果 http 生成,则保留 80