docs/docs/en/runjs/context/require-async.md
Asynchronously loads UMD/AMD or globally mounted scripts via URL, as well as CSS. It is suitable for RunJS scenarios that require UMD/AMD libraries such as ECharts, Chart.js, FullCalendar (UMD version), or jQuery plugins. If a library also provides an ESM version, prioritize using ctx.importAsync().
Can be used in any RunJS scenario where UMD/AMD/global scripts or CSS need to be loaded on demand, such as JSBlock, JSField, JSItem, JSColumn, Workflow, JSAction, etc. Typical uses: ECharts, Chart.js, FullCalendar (UMD), dayjs (UMD), jQuery plugins, etc.
requireAsync<T = any>(url: string): Promise<T>;
| Parameter | Type | Description |
|---|---|---|
url | string | The script or CSS address. Supports shorthand <package>@<version>/<file-path> (appends ?raw for the original UMD file when resolved via ESM CDN) or a full URL. Loads and injects styles if a .css file is passed. |
window (e.g., window.echarts), so the return value might be undefined. In such cases, access the global variable as per the library's documentation.loadCSS when a .css file is passed.echarts@5/dist/echarts.min.js. Under the default ESM CDN (esm.sh), it requests https://esm.sh/echarts@5/dist/echarts.min.js?raw. The ?raw parameter is used to fetch the original UMD file instead of an ESM wrapper.https://cdn.jsdelivr.net/npm/dayjs@1/dayjs.min.js..css will be loaded and injected into the page.window after loading; the return value may be the library object or undefined.ctx.importAsync() for better module semantics and Tree-shaking.// Shorthand path (resolved via ESM CDN as ...?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');
// Load CSS and inject into the page
await ctx.requireAsync('https://cdn.example.com/theme.css');
const container = document.createElement('div');
container.style.height = '400px';
container.style.width = '100%';
ctx.render(container);
const echarts = await ctx.requireAsync('echarts@5/dist/echarts.min.js');
if (!echarts) throw new Error('ECharts library not loaded');
const chart = echarts.init(container);
chart.setOption({
title: { text: ctx.t('Sales Overview') },
series: [{ type: 'pie', data: [{ value: 1, name: ctx.t('A') }] }],
});
chart.resize();
async function renderChart() {
const loaded = await ctx.requireAsync('[email protected]/dist/chart.umd.min.js');
const Chart = loaded?.Chart || loaded?.default?.Chart || loaded?.default;
if (!Chart) throw new Error('Chart.js not loaded');
const container = document.createElement('canvas');
ctx.render(container);
new Chart(container, {
type: 'bar',
data: {
labels: ['A', 'B', 'C'],
datasets: [{ label: ctx.t('Quantity'), data: [12, 19, 3] }],
},
});
}
await renderChart();
const dayjs = await ctx.requireAsync('dayjs@1/dayjs.min.js');
console.log(dayjs?.default || dayjs);
undefined. If undefined, access it via window according to the library's documentation.ctx.importAsync().