Node.js
In this document, you will learn how to build a Node.js library using Rslib. You can check out Node.js related example projects in Examples.
Create Node.js project
You can use create-rslib to create a project with Rslib + Node.js. Just execute the following command:
Then select Node.js when prompted to "Select template".
Use Rslib in an existing project
Rslib offers seamless support for Node.js projects, allowing you to build Node.js project effortlessly with minimal configuration.
For example, in rslib.config.ts:
Target for Node.js
Rslib sets target to "node" by default, which is different from the default target of Rsbuild.
When target is set to "node", Rslib adjusts many configurations for Node.js. For example, output.externals will exclude built-in Node.js modules, and shims will add a shim for import.meta.url in CJS output by default.
Externals
All Node.js built-in modules are externalized by default.
Shims
global: leave it as it is, while it's recommended to use globalThis instead.__filename: When outputting in ESM format, replace__filenamewith the result offileURLToPath(import.meta.url).__dirname: When outputting in ESM format, replace__dirnamewith the result ofdirname(fileURLToPath(import.meta.url)).
