Buffer is not defined

Here’s an improved version of your description:


I am using the @otplib/preset-browser package, which requires the buffer. I’ve polyfilled buffer using vite-plugin-node-polyfills. This works in development mode (vite dev), but I encounter an error buffer is not defined in the production build.

Here is my vite.config.js:

import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
import { nodePolyfills } from "vite-plugin-node-polyfills";
import inject from "@rollup/plugin-inject";

// https://vitejs.dev/config/
export default defineConfig(async () => ({
  plugins: [
    react(),
    nodePolyfills({
      include: ["buffer"],
    }),
  ],

  // Vite options for Tauri development, applied in `tauri dev` or `tauri build`
  clearScreen: false, // Prevents Vite from obscuring Rust errors
  server: {
    port: 1420, // Tauri expects a fixed port, fails if unavailable
    strictPort: true,
    watch: {
      ignored: ["**/src-tauri/**"], // Tells Vite to ignore the `src-tauri` directory
    },
  },
}));

I’ve tried every solution found in google with query buffer is not defined vite.