Run JavaScript

Last updated Sep 30, 2026 You will POST JavaScript to a Worker and read {"result":["Ada"]}.

Prerequisites

  1. Sign up for a Cloudflare account ↗︎.
  2. Install Node.js ↗︎.

<details>

<summary>

Node.js version manager

</summary>

Use <a href="https://viteplus.dev/guide/env">Vite+ (<code>vp env</code>) ↗︎</a> to install and manage Node.js versions.

For supported Node.js versions, refer to the <a href="https://developers.cloudflare.com/workers/wrangler/install-and-update/#install-wrangler">Wrangler system requirements</a>.

</details>

Run JavaScript

  1. Create a Worker project:npmyarnpnpm

npm create cloudflare@latest -- sandbox-dynamic-worker --category=hello-world --type=hello-world --lang=ts --no-deploy

yarn create cloudflare sandbox-dynamic-worker --category=hello-world --type=hello-world --lang=ts --no-deploy

pnpm create cloudflare@latest sandbox-dynamic-worker --category=hello-world --type=hello-world --lang=ts --no-deploy

  1. Change into the project directory:

sh cd sandbox-dynamic-worker

  1. Replace wrangler.jsonc to add a Worker Loader binding:

jsonc { "$schema": "node_modules/wrangler/config-schema.json", "name": "sandbox-dynamic-worker", "main": "src/index.ts", // Set this to today's date "compatibility_date": "2026-10-27", "observability": { "enabled": true, }, "upload_source_maps": true, "worker_loaders": [ { "binding": "LOADER", }, ], }

```toml "$schema" = "node_modules/wrangler/config-schema.json" name = "sandbox-dynamic-worker" main = "src/index.ts" # Set this to today's date compatibility_date = "2026-10-27" upload_source_maps = true

[observability] enabled = true

[[worker_loaders]] binding = "LOADER" ```

  1. Generate types for the binding:npmyarnpnpm

npx wrangler types

yarn wrangler types

pnpm wrangler types

  1. Replace src/index.ts. Your Worker reads code from the JSON body and runs it in the sandbox:

src/index.jsjs

```js export default { async fetch(request, env) { const { code } = await request.json();

    const sandbox = env.LOADER.load({
        compatibilityDate: "2026-10-27",
        mainModule: "code.js",
        modules: {
            "code.js": `
                import { WorkerEntrypoint } from "cloudflare:workers";

                export class Code extends WorkerEntrypoint {
                    evaluate() {
                        ${code}
                    }
                }
            `,
        },
        // Block `fetch()` and `connect()`
        globalOutbound: null,
        // Stop code that uses more than 50 milliseconds of CPU time
        limits: { cpuMs: 50 },
    });

    const result = await sandbox.getEntrypoint("Code").evaluate();
    return Response.json({ result });
},

}; ```

src/index.tsts

```ts import type { WorkerEntrypoint } from "cloudflare:workers";

type CodeEntrypoint = WorkerEntrypoint & { evaluate(): Promise; };

export default { async fetch(request: Request, env: Env): Promise { const { code } = (await request.json()) as { code: string };

    const sandbox = env.LOADER.load({
        compatibilityDate: "2026-10-27",
        mainModule: "code.js",
        modules: {
            "code.js": `
                import { WorkerEntrypoint } from "cloudflare:workers";

                export class Code extends WorkerEntrypoint {
                    evaluate() {
                        ${code}
                    }
                }
            `,
        },
        // Block `fetch()` and `connect()`
        globalOutbound: null,
        // Stop code that uses more than 50 milliseconds of CPU time
        limits: { cpuMs: 50 },
    });

    const result = await sandbox
        .getEntrypoint<CodeEntrypoint>("Code")
        .evaluate();
    return Response.json({ result });
},

}; ```

code is the body of evaluate(), so it needs a return statement. Code that waits without using CPU can still hold the request open, so add a timeout before you run code from other people. For an example, refer to Build an AI code interpreter. 6. Run wrangler dev:npmyarnpnpm

npx wrangler dev

yarn wrangler dev

pnpm wrangler dev

  1. POST JavaScript to the URL Wrangler prints. The default is http://localhost:8787:

sh curl http://localhost:8787 --request POST --json '{ "code": "const users = [{ name: \"Ada\", role: \"admin\" }, { name: \"Grace\", role: \"user\" }]; return users.filter((u) => u.role === \"admin\").map((u) => u.name);" }'

The response body is {"result":["Ada"]}. The Worker ran the JavaScript you sent.

Next steps

Was this helpful?

YesNo

On this page

Run JavaScript - 图1Docs

{"@context":"https://schema.org","@type":"TechArticle","@id":"https://developers.cloudflare.com/sandbox/get-started/dynamic-workers/#page","headline":"Run JavaScript","description":"POST JavaScript to your Worker and read the sandboxed result.","url":"https://developers.cloudflare.com/sandbox/get-started/dynamic-workers/","inLanguage":"en","image":"https://developers.cloudflare.com/sandbox/get-started/dynamic-workers/og.png?v=4ac2191cd371fe40","dateModified":"2026-09-30","publisher":{"@type":"Organization","name":"Cloudflare","description":"One platform for your apps, agents, and workforce. Build, secure, and scale without managing infrastructure","url":"https://www.cloudflare.com/","sameAs":["https://github.com/cloudflare","https://www.linkedin.com/company/cloudflare","https://x.com/cloudflare"],"logo":{"@type":"ImageObject","url":"https://developers.cloudflare.com/logo.svg"},"address":{"@type":"PostalAddress","streetAddress":"101 Townsend St","addressLocality":"San Francisco","addressRegion":"CA","postalCode":"94107","addressCountry":"US"},"contactPoint":[{"@type":"ContactPoint","contactType":"Customer Support","url":"https://support.cloudflare.com/","availableLanguage":["English"]},{"@type":"ContactPoint","contactType":"Sales","url":"https://www.cloudflare.com/contact/","availableLanguage":["English"]}]},"isPartOf":{"@type":"WebSite","@id":"https://developers.cloudflare.com/#website","name":"Cloudflare Docs","url":"https://developers.cloudflare.com/"}}


description: Compare Containers and Dynamic Workers for code that needs Linux or only calls methods that your Worker provides. title: Choose a sandbox environment image: https://developers.cloudflare.com/sandbox/concepts/og.png?v=c938c077dd21d345


Skip to content

Documentation Index
Fetch the complete documentation index at: https://developers.cloudflare.com/sandbox/llms.txt
Use this file to discover all available pages before exploring further.