ctx.importAsync()
Dynamically load ESM modules or CSS via URL, applicable to various RunJS scenarios. Use ctx.importAsync() when third-party ESM libraries are required, and ctx.requireAsync() for UMD/AMD libraries. Passing a .css address will load and inject the styles into the page.
Use Cases
Type Definition
Parameters
Return Value
- A Promise that resolves to the module's namespace object.
URL Format Description
- ESM and CSS: In addition to ESM modules, loading CSS is also supported (pass a
.cssURL to load and inject it into the page). - Shorthand Format: Defaults to https://esm.sh as the CDN prefix if not configured. For example,
vue@3.4.0actually requestshttps://esm.sh/vue@3.4.0. - ?deps: Libraries that depend on React (such as
@dnd-kit/core,react-big-calendar) should append?deps=react@18.2.0,react-dom@18.2.0to avoid conflicts with the page's React instance, which could lead to "Invalid hook call" errors. - Self-hosted CDN: You can specify an internal network or self-hosted service via environment variables:
- ESM_CDN_BASE_URL: The base URL for the ESM CDN (default is
https://esm.sh). - ESM_CDN_SUFFIX: Optional suffix (e.g.,
/+esmfor jsDelivr). - For self-hosted services, refer to: nocobase/esm-server
- ESM_CDN_BASE_URL: The base URL for the ESM CDN (default is
Difference from ctx.requireAsync()
- ctx.importAsync(): Loads ESM modules and returns the module namespace. Suitable for modern libraries (ESM builds like Vue, dayjs, etc.).
- ctx.requireAsync(): Loads UMD/AMD modules or scripts that attach to the global scope. Often used for UMD libraries like ECharts or FullCalendar. If a library provides both ESM and UMD,
ctx.importAsync()is preferred.
Examples
Basic Usage
Demonstrates the most basic usage of dynamically loading ESM modules and CSS by package name or full URL.
ECharts Example
Use ECharts to draw a sales overview chart with bar and line graphs.
Tabulator Example
Demonstrates rendering a data table with pagination and row click events in a block using Tabulator.
FullCalendar (ESM) Example
Shows how to load FullCalendar and its plugins via ESM and render a basic monthly view calendar.
dnd-kit Simple Drag-and-Drop Example
Uses @dnd-kit/core to implement a minimal example of dragging a Box to a target area within a block.
This example relies only on @dnd-kit/core to trigger a notification when a Box is dropped into a specific area, demonstrating the simplest drag-and-drop interaction combining ctx.importAsync and React in RunJS.
dnd-kit Sortable List Example
Implements a vertical sortable list using dnd-kit's core, sortable, and utilities.
This example uses @dnd-kit/core, @dnd-kit/sortable, and @dnd-kit/utilities to implement a sortable list that updates its order and displays a "List reordered" message after dragging.
react-big-calendar Example
Renders a calendar component supporting event display in the current block using react-big-calendar and date-fns for localization.
frappe-gantt Example
Uses frappe-gantt to render a Gantt chart view showing task start/end times and progress.
@asseinfo/react-kanban Example
Utilizes @asseinfo/react-kanban to render a basic Kanban board with columns like Backlog and Doing within a block.
Notes
- This feature depends on an external network or CDN. In internal network environments, ESM_CDN_BASE_URL must be configured to point to a self-hosted service.
- When a library provides both ESM and UMD, prefer
ctx.importAsync()for better module semantics. - For libraries depending on React, ensure you append
?deps=react@18.2.0,react-dom@18.2.0. The version must match the React version used by the page, otherwise, an "Invalid hook call" error may occur.
Related
- ctx.requireAsync(): Load UMD/AMD or globally attached scripts, suitable for UMD libraries like ECharts and FullCalendar.
- ctx.render(): Render content into a container.

