reactjs - 使用 Vite 和 React 进行生产构建时出错 : Error in Uncaught TypeError: _ is not a function

标签 reactjs typescript axios vite production

我正在为我的网站使用 Vite、React、React Router 和 TypeScript。 我在运行生产构建时遇到问题,在开发模式下一切正常。

使用生产版本时,我的浏览器显示白色背景,并且我在浏览器控制台中收到以下错误:

enter image description here

上面的链接将我带到第 70 行中的以下(缩小的?)源代码:

enter image description here

那么也许它与 React Router 有关?

但我不确定到底是什么导致了这个错误。 因此,我将尽力在下面提供尽可能多的相关信息。

运行npm run build时的结果:

enter image description here

package.json:

{
    "name": "frontend",
    "private": true,
    "version": "0.0.0",
    "type": "module",
    "scripts": {
        "start": "vite",
        "build": "tsc && vite build",
        "lint": "eslint src --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
        "preview": "vite preview",
        "test": "jest"
    },
    "dependencies": {
        "@faker-js/faker": "^8.0.2",
        "@vitejs/plugin-react": "^4.0.3",
        "axios": "^1.4.0",
        "chart.js": "^4.3.0",
        "daisyui": "^3.1.7",
        "dayjs": "^1.11.9",
        "i18next": "^23.2.7",
        "i18next-browser-languagedetector": "^7.1.0",
        "lucide-react": "^0.258.0",
        "react": "^18.2.0",
        "react-chartjs-2": "^5.2.0",
        "react-dom": "^18.2.0",
        "react-i18next": "^13.0.1",
        "react-router-dom": "^6.14.2",
        "vite": "^4.4.6"
    },
    "devDependencies": {
        "@tailwindcss/typography": "^0.5.9",
        "@types/axios": "^0.14.0",
        "@types/node": "^20.1.1",
        "@types/react": "^18.0.28",
        "@types/react-dom": "^18.0.11",
        "@typescript-eslint/eslint-plugin": "^5.57.1",
        "@typescript-eslint/parser": "^5.57.1",
        "autoprefixer": "^10.4.14",
        "eslint": "^8.2.0",
        "eslint-config-airbnb": "^19.0.4",
        "eslint-config-prettier": "^8.8.0",
        "eslint-plugin-import": "^2.25.3",
        "eslint-plugin-jsx-a11y": "^6.5.1",
        "eslint-plugin-prettier": "^4.2.1",
        "eslint-plugin-react": "^7.28.0",
        "eslint-plugin-react-hooks": "^4.3.0",
        "eslint-plugin-react-refresh": "^0.3.4",
        "jest": "^29.5.0",
        "postcss": "^8.4.23",
        "prettier": "^2.8.8",
        "prettier-plugin-tailwindcss": "^0.2.8",
        "tailwindcss": "^3.3.2",
        "typescript": "^5.0.2"
    }
}

vite.config.ts:

import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";

// https://vitejs.dev/config/
export default defineConfig({
    plugins: [react()],
});

tsconfig.json:

{
    "compilerOptions": {
        "target": "ESNext",
        "lib": ["DOM", "DOM.Iterable", "ESNext"],
        "module": "ESNext",
        "skipLibCheck": true,

        /* Bundler mode */
        "moduleResolution": "bundler",
        "allowImportingTsExtensions": true,
        "resolveJsonModule": true,
        "isolatedModules": true,
        "noEmit": true,
        "jsx": "react-jsx",

        /* Linting */
        "strict": true,
        "noUnusedLocals": true,
        "noUnusedParameters": true,
        "noFallthroughCasesInSwitch": true
    },
    "include": ["src"],
    "references": [{ "path": "./tsconfig.node.json" }]
}

tsconfig.node.json:

{
    "compilerOptions": {
        "composite": true,
        "skipLibCheck": true,
        "module": "ESNext",
        "moduleResolution": "bundler",
        "allowSyntheticDefaultImports": true
    },
    "include": ["vite.config.ts"]
}

当使用npm start时,一切都按预期工作,但如果我使用npm run build,然后使用npm run Preview,我会收到提到的错误上面,我的浏览器显示白色背景。

我已经尝试过的

我已经尝试使用以下 vite.config.ts 禁用 Vite 中的代码分割:

import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";

// https://vitejs.dev/config/
export default defineConfig({
    plugins: [react()],
    build: {
        rollupOptions: {
            output: {
                manualChunks: {},
            },
        },
    },
});

但这没有任何效果。

我尝试替换 React 中的所有片段:

<>
    ...
</>

与:

<Fragment>
    ...
<Fragment/>

但这也没有任何效果。

最佳答案

我发现了问题。

@DrewReese 为我指明了正确的方向,而且我实际上仔细查看了缩小后的代码。

我意识到上面的代码是关于日期格式的,并且我使用了一个名为 Day.js 的库。 .

我通过更改导入来解决问题:

import * as dayjs from "dayjs";
import * as customParseFormat from "dayjs/plugin/customParseFormat";

至:

import dayjs from "dayjs";
import customParseFormat from "dayjs/plugin/customParseFormat";

旁注:

Day.js 实际上 seems to recommend为我的设置导入他们的库的第一种方法会破坏生产构建。

关于reactjs - 使用 Vite 和 React 进行生产构建时出错 : Error in Uncaught TypeError: _ is not a function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76754734/

相关文章:

reactjs - React 16 升级..错误: Can't resolve 'react/lib/ReactMount'

reactjs - 如何在使用 create-react-app 创建的 React 应用程序的生产版本中删除 console.log?

typescript - TS2339 : Property does not exist on union type - property string | undefined

javascript - 如何将简单值从 axios.post 发送到 asp.net 核心?

javascript - 创建一个 slate.js 编辑器组件,将其状态保持在 markdown 中

javascript - 从单独的模块响应状态更新。怎么做?

javascript - 可索引类型 - TypeScript

angular - 如何访问 Angular 2 组件和服务中的常量?

javascript - Axios 上的 setInterval get 和更新输出而不重复

javascript - React - WP_Mail 可与 jQuery 配合使用,但不能与 axios 配合使用