{"id":11989337,"url":"https://github.com/rspack-contrib/rsbuild-plugin-node-polyfill","last_synced_at":"2025-08-22T11:13:07.865Z","repository":{"id":246782796,"uuid":"822957824","full_name":"rspack-contrib/rsbuild-plugin-node-polyfill","owner":"rspack-contrib","description":"An Rsbuild plugin to automatically inject polyfills for Node.js builtin modules into the browser side.","archived":false,"fork":false,"pushed_at":"2025-04-01T05:37:45.000Z","size":174,"stargazers_count":11,"open_issues_count":2,"forks_count":2,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-04-12T07:48:05.435Z","etag":null,"topics":["rsbuild","rsbuild-plugin","rspack"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rspack-contrib.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-07-02T06:52:16.000Z","updated_at":"2025-04-01T05:37:47.000Z","dependencies_parsed_at":null,"dependency_job_id":"b97baf55-cf43-4097-b323-46dce87601bf","html_url":"https://github.com/rspack-contrib/rsbuild-plugin-node-polyfill","commit_stats":null,"previous_names":["rspack-contrib/rsbuild-plugin-node-polyfill"],"tags_count":5,"template":false,"template_full_name":"rspack-contrib/rsbuild-plugin-template","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rspack-contrib%2Frsbuild-plugin-node-polyfill","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rspack-contrib%2Frsbuild-plugin-node-polyfill/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rspack-contrib%2Frsbuild-plugin-node-polyfill/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rspack-contrib%2Frsbuild-plugin-node-polyfill/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rspack-contrib","download_url":"https://codeload.github.com/rspack-contrib/rsbuild-plugin-node-polyfill/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248860007,"owners_count":21173342,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["rsbuild","rsbuild-plugin","rspack"],"created_at":"2024-07-02T13:09:39.659Z","updated_at":"2025-08-22T11:13:07.851Z","avatar_url":"https://github.com/rspack-contrib.png","language":"TypeScript","readme":"# @rsbuild/plugin-node-polyfill\n\nAn Rsbuild plugin to automatically inject polyfills for [Node.js builtin modules](https://nodejs.org/api/modules.html#built-in-modules) into the browser side.\n\n\u003cp\u003e\n  \u003ca href=\"https://npmjs.com/package/@rsbuild/plugin-node-polyfill\"\u003e\n   \u003cimg src=\"https://img.shields.io/npm/v/@rsbuild/plugin-node-polyfill?style=flat-square\u0026colorA=564341\u0026colorB=EDED91\" alt=\"npm version\" /\u003e\n  \u003c/a\u003e\n  \u003cimg src=\"https://img.shields.io/badge/License-MIT-blue.svg?style=flat-square\u0026colorA=564341\u0026colorB=EDED91\" alt=\"license\" /\u003e\n  \u003ca href=\"https://npmcharts.com/compare/@rsbuild/plugin-node-polyfill?minimal=true\"\u003e\u003cimg src=\"https://img.shields.io/npm/dm/@rsbuild/plugin-node-polyfill.svg?style=flat-square\u0026colorA=564341\u0026colorB=EDED91\" alt=\"downloads\" /\u003e\u003c/a\u003e\n\u003c/p\u003e\n\n## When to use\n\nNormally, we don't need to use Node builtin modules on the browser side. However, it is possible to use some Node builtin modules when the code will run on both the Node side and the browser side, and this plugin provides browser versions of polyfills for these Node builtin modules.\n\nBy using the Node Polyfill plugin, polyfills for Node builtin modules are automatically injected into the browser-side, allowing you to use these modules on the browser side with confidence.\n\n## Usage\n\nInstall:\n\n```bash\nnpm add @rsbuild/plugin-node-polyfill -D\n```\n\nAdd plugin to your `rsbuild.config.ts`:\n\n```ts\n// rsbuild.config.ts\nimport { pluginNodePolyfill } from \"@rsbuild/plugin-node-polyfill\";\n\nexport default {\n  plugins: [pluginNodePolyfill()],\n};\n```\n\n## Node Polyfills\n\n### Globals\n\n- `Buffer`\n- `process`\n\nWhen you use the above global variables in your code, the corresponding polyfill will be automatically injected.\n\nFor instance, the following code would inject the `Buffer` polyfill:\n\n```ts\nconst bufferData = Buffer.from(\"abc\");\n```\n\nYou can disable this behavior through the `globals` option of the plugin:\n\n```ts\npluginNodePolyfill({\n  globals: {\n    Buffer: false,\n    process: false,\n  },\n});\n```\n\n### Modules\n\n- `assert`\n- `buffer`\n- `console`\n- `constants`\n- `crypto`\n- `domain`\n- `events`\n- `http`\n- `https`\n- `os`\n- `path`\n- `punycode`\n- `process`\n- `querystring`\n- `stream`\n- `_stream_duplex`\n- `_stream_passthrough`\n- `_stream_readable`\n- `_stream_transform`\n- `_stream_writable`\n- `string_decoder`\n- `sys`\n- `timers`\n- `tty`\n- `url`\n- `util`\n- `vm`\n- `zlib`\n\nWhen the above module is referenced in code via import / require syntax, the corresponding polyfill will be injected.\n\n```ts\nimport { Buffer } from \"buffer\";\n\nconst bufferData = Buffer.from(\"abc\");\n```\n\n### Fallbacks\n\n- `child_process`\n- `cluster`\n- `dgram`\n- `dns`\n- `fs`\n- `module`\n- `net`\n- `readline`\n- `repl`\n- `tls`\n\nCurrently there is no polyfill for the above modules on the browser side, so when you import the above modules, it will automatically fallback to an empty object.\n\n```ts\nimport fs from \"fs\";\n\nconsole.log(fs); // -\u003e {}\n```\n\n## Options\n\n### globals\n\nUsed to specify whether to inject polyfills for global variables.\n\n- **Type:**\n\n```ts\ntype GlobalsOption = {\n  /**\n   * Whether to inject the polyfill for direct identifier usage,\n   * e.g. `Buffer` or `process` (bare usage without any object prefix).\n   * @default true\n   */\n  bare?: boolean;\n  /**\n   * Whether to inject the polyfill for usage via the `global` object,\n   * e.g. `global.Buffer` or `global.process`.\n   * @default false\n   */\n  global?: boolean;\n  /**\n   * Whether to inject the polyfill for usage via the `globalThis` object,\n   * e.g. `globalThis.Buffer` or `globalThis.process`.\n   * @default false\n   */\n  globalThis?: boolean;\n};\n\ntype Globals = {\n  process?: boolean | GlobalsOption;\n  Buffer?: boolean | GlobalsOption;\n};\n```\n\n- **Default:**\n\n```ts\nconst defaultGlobals = {\n  Buffer: {\n    bare: true,\n    global: false,\n    globalThis: false,\n  },\n  process: {\n    bare: true,\n    global: false,\n    globalThis: false,\n  },\n};\n```\n\n### protocolImports\n\nWhether to polyfill Node.js builtin modules starting with `node:`.\n\n```ts\nconst defaultGlobals = {\n  Buffer: true,\n  process: true,\n};\n```\n\n### protocolImports\n\nWhether to polyfill Node.js builtin modules starting with `node:`.\n\n- **Type:** `boolean`\n- **Default:** `true`\n\nFor example, if you disable `protocolImports`, modules such as `node:path`, `node:http`, etc. will not be polyfilled.\n\n```ts\npluginNodePolyfill({\n  protocolImports: false,\n});\n```\n\n### include\n\nSpecify an array of modules for which polyfills should be injected. If this option is set, only the specified modules will be polyfilled. `include` is mutually exclusive with [`exclude`](#exclude).\n\n- **Type:** `string[]`\n- **Default:** `undefined`\n\n```ts\npluginNodePolyfill({\n  include: [\"buffer\", \"crypto\"], // Only \"buffer\" and \"crypto\" modules will be polyfilled.\n});\n```\n\n### exclude\n\nSpecify an array of modules for which polyfills should not be injected from the default. If this option is set, the specified modules will be excluded from polyfilled. `exclude` is mutually exclusive with [`include`](#include).\n\n- **Type:** `string[]`\n- **Default:** `undefined`\n\n```ts\npluginNodePolyfill({\n  exclude: [\"http\", \"https\"], // All modules except \"http\" and \"https\" will be polyfilled.\n});\n```\n\n### overrides\n\nOverride the default polyfills for specific modules.\n\n- **Type:** `Record\u003cstring, string\u003e`\n- **Default:** `{}`\n\n```ts\npluginNodePolyfill({\n  overrides: {\n    fs: \"memfs\",\n  },\n});\n```\n\n### force\n\nBy default, the plugin only polyfills the browser-side code. If you want to polyfill the server-side code as well (when `output.target` is `node`), you can set the `force` option to `true`.\n\n- **Type:** `boolean`\n- **Default:** `false`\n\n```ts\npluginNodePolyfill({\n  force: true,\n});\n```\n\n## Exported variables\n\n- `builtinMappingResolved`: A map of Node.js builtin modules to their resolved corresponding polyfills modules.\n- `resolvedPolyfillToModules`: A map of resolved polyfill modules to the polyfill modules before resolving.\n\n## License\n\n[MIT](./LICENSE).\n","funding_links":[],"categories":["Plugins"],"sub_categories":["Rsbuild Plugins"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frspack-contrib%2Frsbuild-plugin-node-polyfill","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frspack-contrib%2Frsbuild-plugin-node-polyfill","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frspack-contrib%2Frsbuild-plugin-node-polyfill/lists"}