Skip to content

AiCodeRunner WebContainer Runner

The AiCodeRunner component encapsulates @webcontainer/api, providing a powerful sandbox for running a Node.js environment directly in the browser. It is typically used for real-time previews of AI-generated code, logic validation, and interactive tutorials.

Basic Usage

Pass a code string to execute it in the terminal. The component automatically handles the initialization of the WebContainer.

Click Run to execute the code...

Auto Run

Set autorun to true to automatically re-execute whenever the code changes.

Click Run to execute the code...

API

Props

PropertyDescriptionTypeDefault
codeCode string to executestring''
languageProgramming language (primarily supports javascript/typescript)string'javascript'
heightHeight of the terminal display areastring | number200
autorunWhether to execute automatically when code changesbooleanfalse
themeOverridesTheme override variablesComponentThemeVarsundefined

Events

Event NameDescriptionParameters
runTriggered when code execution starts(code: string) => void
stopTriggered when execution is manually stopped() => void
outputTriggered when there is new terminal output(data: string) => void
errorTriggered when an error occurs during execution(error: string) => void
readyTriggered when the WebContainer environment is ready(instance: WebContainer) => void

Methods

Method NameDescriptionParameters
runManually trigger code execution() => void
stopStop the currently running process() => void
resetReset the WebContainer instance() => void
clearClear the terminal output() => void

Environment Requirements

WebContainer relies on SharedArrayBuffer. Ensure your server is configured with the necessary security headers (Cross-Origin Isolation):

  1. Cross-Origin-Opener-Policy: same-origin
  2. Cross-Origin-Embedder-Policy: require-corp

Released under the MIT License.