In RunJS, you can use two types of modules: Built-in modules (accessed directly via ctx.libs without importing) and External modules (loaded on demand via ctx.importAsync() or ctx.requireAsync()).
RunJS includes several built-in libraries that can be accessed directly through ctx.libs. You do not need to use import or asynchronous loading for these.
| Property | Description |
|---|---|
| ctx.libs.React | React core, used for JSX and Hooks |
| ctx.libs.ReactDOM | ReactDOM (can be used for createRoot, etc.) |
| ctx.libs.antd | Ant Design component library |
| ctx.libs.antdIcons | Ant Design icons |
| ctx.libs.math | Math.js: Mathematical expressions, matrix operations, etc. |
| ctx.libs.formula | Formula.js: Excel-like formulas (SUM, AVERAGE, etc.) |
When you need third-party libraries, choose the loading method based on the module format:
ctx.importAsync()ctx.requireAsync()Use ctx.importAsync() to dynamically load ESM modules by URL. This is suitable for scenarios like JS blocks, JS fields, and JS actions.
<package>@<version> or subpaths like <package>@<version>/<file-path> (e.g., vue@3.4.0, lodash@4/lodash.js). These will be prefixed with the configured CDN base URL. Full URLs are also supported.If not configured otherwise, shorthand forms will use https://esm.sh as the CDN prefix. For example:
If you need to use an internal network or a self-built CDN, you can deploy a service compatible with the esm.sh protocol and specify it via environment variables:
https://esm.sh)/+esm for jsDelivr)For self-hosting, refer to: https://github.com/nocobase/esm-server
Use ctx.requireAsync() to asynchronously load UMD/AMD modules or scripts that attach to the global object.
<package>@<version>/<file-path>, similar to ctx.importAsync(), resolved according to the current ESM CDN configuration. When resolving, ?raw is appended to request the raw file directly (usually a UMD build). For example, echarts@5/dist/echarts.min.js actually requests https://esm.sh/echarts@5/dist/echarts.min.js?raw (when using the default esm.sh).https://cdn.jsdelivr.net/npm/xxx).After loading, many UMD libraries attach themselves to the global object (e.g., window.xxx). You can use them as described in the library's documentation.
Example
Note: If a library provides an ESM version, prefer using ctx.importAsync() for better module semantics and Tree-shaking.