如何解决vite上“配置的目标环境中不可用大整型文字”错误?

Cod*_*een 4 optimization typescript reactjs vite vitest

我正在使用vitereacttypescript然后我收到此错误,有人知道如何解决吗?

\n

这是我的 package.json:

\n
{\n  "name": "",\n  "private": true,\n  "version": "0.0.0",\n  "scripts": {\n    "dev": "vite",\n    "build": "tsc && vite build",\n    "preview": "vite preview"\n  },\n  "dependencies": {\n    "@apollo/client": "^3.7.9",\n    "@composedb/cli": "^0.3.1",\n    "@composedb/client": "^0.3.1",\n    "@didtools/pkh-ethereum": "^0.0.3",\n    "@emotion/react": "^11.9.0",\n    "@emotion/styled": "^11.8.1",\n    "@mui/icons-material": "^5.8.4",\n    "@mui/material": "^5.8.2",\n    "@mui/x-date-pickers": "^5.0.0-alpha.5",\n    "cytoscape": "^3.22.1",\n    "date-fns": "^2.28.0",\n    "did-session": "^1.0.0",\n    "graphql": "^16.6.0",\n    "graphql-tag": "^2.12.6",\n    "moment": "^2.29.4",\n    "react": "^18.0.0",\n    "react-dom": "^18.0.0",\n    "react-router-dom": "^6.3.0",\n    "web3": "^1.8.2"\n  },\n  "devDependencies": {\n    "@types/cytoscape": "^3.19.6",\n    "@types/react": "^18.0.0",\n    "@types/react-dom": "^18.0.0",\n    "@vitejs/plugin-react": "^1.3.0",\n    "axios": "^0.27.2",\n    "typescript": "^4.6.3",\n    "vite": "^2.9.9"\n  }\n}\n
Run Code Online (Sandbox Code Playgroud)\n

日志:

\n
\n
\xe2\x9c\x98 [ERROR] Big integer literals are not available in the configured target environment ("chrome87", "edge88", "es2019", "firefox78", "safari13.1")\n\nnode_modules/bigint-mod-arith/dist/esm/index.browser.js:231:16:\n  231 \xe2\x94\x82         e = e / 2n;\n      \xe2\x95\xb5                 ~~\n\n\xe2\x9c\x98 [ERROR] Big integer literals are not available in the configured target environment ("chrome87", "edge88", "es2019", "firefox78", "safari13.1")\n\nnode_modules/bigint-mod-arith/dist/esm/index.browser.js:232:17:\n  232 \xe2\x94\x82         b = b ** 2n % n;\n      \xe2\x95\xb5                  ~~\n\n7:04:56 AM [vite] error while updating dependencies: Error: Build failed with 68 errors:\nnode_modules/bigint-mod-arith/dist/esm/index.browser.js:21:14: ERROR:\nBig integer literals are not available in the configured target environment ("chrome87", "edge88", "es2019", "firefox78", "safari13.1")\nnode_modules/bigint-mod-arith/dist/esm/index.browser.js:27:20: \nERROR:\nBig integer literals are not available in the configured target environment ("chrome87", "edge88", "es2019", "firefox78", "safari13.1")\nnode_modules/bigint-mod-arith/dist/esm/index.browser.js:27:26: ERROR:\nBig integer literals are not available in the configured target environment ("chrome87", "edge88", "es2019", "firefox78", "safari13.1")\nnode_modules/bigint-mod-arith/dist/esm/index.browser.js:47:13: ERROR:\nBig integer literals are not available in the configured target environment ("chrome87", "edge88", "es2019", "firefox78", "safari13.1")\nnode_modules/bigint-mod-arith/dist/esm/index.browser.js:47:24: ERROR:\nBig integer literals are not available in the configured target environment ("chrome87", "edge88", "es2019", "firefox78", "safari13.1") ...\nat failureErrorWithLog \n[...]\n
Run Code Online (Sandbox Code Playgroud)\n
\n

我尝试删除node_modulespnpm-lock-yaml并重新安装它们,但问题仍然存在。

\n

JEd*_*dot 8

我通过定义以下 vite 配置选项在vite 3.2.x4.1.x上解决了这个问题 (使用 Svelte 而不是 React,但它应该可以工作) :

  • optimizeDeps.esbuildOptions.target
  • optimizeDeps.global
  • optimizeDeps.esbuildOptions.supported.bigint
  • build.target
import { defineConfig } from "vite";
// import react, svelte and other needs...

// https://vitejs.dev/config/
export default ({ mode }) => {

  return defineConfig({
    
    optimizeDeps: { //  optimizedeps
      esbuildOptions: {
        target: "esnext", 
        // Node.js global to browser globalThis
        define: {
          global: 'globalThis'
        },
        supported: { 
          bigint: true 
        },
      }
    }, 

    build: {
      target: ["esnext"], //  build.target
    },
  })
}
Run Code Online (Sandbox Code Playgroud)