A Vite plugin to polyfill native Node modules for the browser
Install the package as a dev dependency.
# npm
npm install --save-dev vite-plugin-node-polyfills
# pnpm
pnpm install --save-dev vite-plugin-node-polyfills
# yarn
yarn add --dev vite-plugin-node-polyfills
Add the plugin to your vite.config.ts
file.
import { defineConfig } from 'vite'
import { nodePolyfills } from 'vite-plugin-node-polyfills'
// https://vitejs.dev/config/
export default defineConfig({
plugins: [
nodePolyfills(),
],
})
The following error can occur when a package references a native Node module without providing a polyfill. See this Vite issue for more info.
Module "stream" has been externalized for browser compatibility. Cannot access "stream.Readable" in client code.