Shows how to use Sandbox SDK to provide build tools that Workers don't include.
Workers execute JavaScript and WASM instantly, but don't include build tools like npm or bundlers. This example demonstrates using Sandbox SDK to bundle npm dependencies in a container, then loading the output into a Dynamic Worker for execution. Dynamic Workers let you load and run user-provided code at runtime without redeploying—enabling interactive experiences like this playground.
Workers are designed to execute JavaScript and WebAssembly instantly. They don't include build tools:
- npm (can't install dependencies at runtime)
- Bundlers (esbuild, webpack, rollup)
- Compilers (rustc, emscripten, TinyGo)
Sandbox SDK provides build tools in isolated containers. Workers execute the output:
- Build once: Run npm install + esbuild in a container
- Execute many times: Load the bundle into Workers
- Rebuild only when needed: Cache output until code changes
User writes TypeScript with npm dependency:
import { z } from 'zod';
export const schema = z.object({
name: z.string().min(1),
email: z.string().email()
});Sandbox SDK bundles the npm dependency:
- Writes code to container
- Runs
esbuild --bundleto inline zod dependency - Returns bundled JavaScript
Dynamic Worker executes the bundled code:
- Loads bundle into isolate
- Runs validation instantly
- Reuses same bundle until schema changes
- Docker running locally
- Node.js 16.17.0+
- Cloudflare account (for deployment)
npm install
npm run devVisit http://localhost:8787 and:
- Write a TypeScript schema using zod
- Provide test data as JSON
- Click "Validate"
First validation: Bundles npm dependencies with Sandbox SDK Subsequent validations: Instant (uses cached bundle)
npm run deployNote: Dynamic Workers are in closed beta. Sign up here
This pattern works for any build step:
npm dependencies: Bundle JavaScript libraries (this example) Native code to WASM: Compile Rust/C++/Go with rustc/emscripten/TinyGo Custom builds: Run webpack, rollup, or custom toolchains
Sandbox SDK provides the build environment. Workers execute the output.
User Code (with npm dependencies)
↓ Sandbox SDK (build tools in container)
JavaScript Bundle
↓ Workers (execute in isolate)
Result