简体   繁体   English

从 CRA(创建反应应用程序)迁移到 vite 后,未定义缓冲区

[英]Buffer is not defined, after migrating from CRA(create react app) to vite

After having declared and done all the config files, when I start the server I get Buffer not defined and the error points to an npm module.在声明并完成所有配置文件后,当我启动服务器时,我得到 Buffer not defined 并且错误指向 npm 模块。 Uncaught ReferenceError: Buffer is not definedat node_modules/jsesc/jsesc.js

You can make the following changes to fix the issue, in vite.config.ts, index.html and adding packages您可以在 vite.config.ts、index.html 和添加包中进行以下更改以解决此问题

1.Install Packages 1.安装包

yarn add process util buffer events
yarn add @esbuild-plugins/node-modules-polyfill

2.Update vite.config 2.更新vite.config

import GlobalPolyFill from "@esbuild-plugins/node-globals-polyfill";
import react from "@vitejs/plugin-react";
import { resolve } from "path";
import rollupNodePolyFill from "rollup-plugin-node-polyfills";
import { defineConfig } from "vite";

export default defineConfig({
    plugins: [react()],
    optimizeDeps: {
        esbuildOptions: {
            define: {
                global: "globalThis",
            },
            plugins: [
                GlobalPolyFill({
                    process: true,
                    buffer: true,
                }),
            ],
        },
    },
    resolve: {
        alias: {
            process: "process/browser",
            stream: "stream-browserify",
            zlib: "browserify-zlib",
            util: "util",
        },
    },
});

3.Update index.html 3.更新索引.html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <link rel="icon" type="image/svg+xml" href="/src/assets/images/favicon.svg" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>Vite App</title>
  <script>
    window.global = window;
  </script>
  <script type="module">
    import process from "process";
    import EventEmitter from "events";
    import {Buffer} from "buffer";
    window.Buffer = Buffer;
    window.process = process;
    window.EventEmitter = EventEmitter;
  </script>
</head>

<body>
  <div id="root"></div>
  <script type="module" src="./src/index.js"></script>
</body>
</html>

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

相关问题 ReferenceError: React 未定义 - 从 CRA 迁移到 Vite 和 NX - ReferenceError: React is not defined - Migrating from CRA to Vite and NX React-vite 中没有定义 Buffer - Buffer is not defined in React-vite 从 CRA 迁移到 Vite 时找不到模块 - Modules are not being found when migrating from CRA to Vite 从 create-react-app (CRA) 转换为 CRA + Next.Js 应用 - Conversion from an create-react-app (CRA) to CRA + Next.Js app 在没有 CRA(create-react-app)的情况下从 react 到 express 获取错误 - Error fetch from react to express without CRA(create-react-app) 从 create-app-app 迁移到通常的 React 应用程序 - Migrating from create-app-app to usual react application Create-react-app + TypeScript + CSS 模块:自动生成类型定义而不从 CRA 中弹出 - Create-react-app + TypeScript + CSS Modules: Auto-generating type definitions without ejecting from CRA 反应由 CRA(create-react-app) 创建的 web 总是重定向到 '/' - react web created by CRA(create-react-app) always redirect to '/' 登录前和登录后用于 SEO 的 React SSR(服务器端渲染)创建 React 应用程序(CRA)。 如何在一个项目文件夹中执行此操作? - React SSR(Server Side Rendering) for SEO before login and after login Create React App (CRA). How to do this in one project folder? 将 create-react-app 从 javascript 迁移到 typescript - Migrating create-react-app from javascript to typescript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM