docs/docs/en/runjs/import-modules.md
In RunJS you can use two kinds of modules: built-in modules (via ctx.libs, no import needed) and external modules (loaded on demand via ctx.importAsync() or ctx.requireAsync()).
RunJS provides common libraries via ctx.libs; you can use them directly without import or async loading.
| Property | Description |
|---|---|
| ctx.libs.React | React core for JSX and Hooks |
| ctx.libs.ReactDOM | ReactDOM (e.g. for createRoot) |
| ctx.libs.antd | Ant Design components |
| ctx.libs.antdIcons | Ant Design icons |
| ctx.libs.math | Math.js: math expressions, matrix operations, etc. |
| ctx.libs.formula | Formula.js: Excel-like formulas (SUM, AVERAGE, etc.) |
const { Button } = ctx.libs.antd;
ctx.render(<Button>Click</Button>);
const result = ctx.libs.math.evaluate('2 + 3 * 4');
// result === 14
const values = [1, 2, 3, 4];
const sum = ctx.libs.formula.SUM(values);
const avg = ctx.libs.formula.AVERAGE(values);
For third-party libraries, choose the loader by module format:
ctx.importAsync()ctx.requireAsync()Use ctx.importAsync() to load ESM modules by URL at runtime. Suitable for JS Block, JS Field, JS Action, etc.
importAsync<T = any>(url: string): Promise<T>;
<package>@<version> or subpath <package>@<version>/<path> (e.g. [email protected], lodash@4/lodash.js), which is resolved with the configured CDN prefix; full URLs are also supported.When not configured, the shorthand form uses https://esm.sh as the CDN prefix. For example:
const Vue = await ctx.importAsync('[email protected]');
// equivalent to loading from https://esm.sh/[email protected]
For intranet or custom CDN, deploy an esm.sh-compatible service and set:
https://esm.sh)/+esm)Reference: https://github.com/nocobase/esm-server
Use ctx.requireAsync() to load UMD/AMD scripts or scripts that attach to the global object by URL.
requireAsync<T = any>(url: string): Promise<T>;
<package>@<version>/<path>, same as ctx.importAsync(); resolved with the current ESM CDN config; ?raw is appended to request the raw file (usually UMD). E.g. echarts@5/dist/echarts.min.js becomes https://esm.sh/echarts@5/dist/echarts.min.js?raw when using default esm.sh.https://cdn.jsdelivr.net/npm/xxx).Many UMD libraries attach to the global (e.g. window.xxx); use them as documented.
Example
// Shorthand (resolved via esm.sh with ...?raw)
const echarts = await ctx.requireAsync('echarts@5/dist/echarts.min.js');
// Full URL
const dayjs = await ctx.requireAsync('https://cdn.jsdelivr.net/npm/dayjs@1/dayjs.min.js');
Note: If a library provides both ESM and UMD, prefer ctx.importAsync() for better module semantics and tree-shaking.