axios 使用Vite & React进行生产构建时出错:未捕获的TypeError中的错误:_不是函数

fhg3lkii  于 2023-08-04  发布在  iOS
关注(0)|答案(1)|浏览(133)

我在我的网站上使用Vite,React,React Router和TypeScript。我在运行生产构建时遇到了一个问题,在开发模式下一切正常。
当使用生产构建时,我的浏览器显示白色背景,我在浏览器控制台中得到以下错误:


的数据
上面的链接把我带到下面(缩小?)第70行的源代码:



这可能与React Router有关。
但我不确定到底是什么原因导致了这个错误。因此,我将在下面尽可能多地提供相关信息。
运行npm run build时的结果:



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/>


但这也没有任何效果。

relj7zay

relj7zay1#

我发现问题了。
@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实际上似乎推荐了第一种导入库的方法,这在生产构建中会中断。

相关问题