vite-plugin-node-polyfills
A Vite plugin to polyfill native Node modules for the browser
Getting Started
Install the package as a dev dependency.
npm install --save-dev vite-plugin-node-polyfills
pnpm install --save-dev vite-plugin-node-polyfills
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'
export default defineConfig({
plugins: [
nodePolyfills(),
],
})
Why?
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.
Attribution
Special thanks to @FbN for putting together this gist.