> ## Documentation Index
> Fetch the complete documentation index at: https://docs.declaw.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# node — TypeScript script

> Compile a TypeScript file with tsc and run it under Node.js 20 — the standard use of the node template.

The `node` template ships `Node.js 20 LTS`, `npm`, the `typescript` compiler,
and `yarn`. Pick it for TypeScript / Node scripts and any workflow that
needs to install npm packages at runtime.

## What you'll learn

* Picking `template="node"` to skip a Node install
* Writing a `.ts` file into the sandbox, compiling with `tsc`, running with `node`

## Prerequisites

<Snippet file="snippets/env-setup.mdx" />

## Code

<Tabs>
  <Tab title="Python">
    ```python theme={null}
    import textwrap

    from declaw import Sandbox


    SCRIPT = textwrap.dedent("""\
        declare const process: { version: string };
        type Order = { id: string; qty: number; price: number };

        const orders: Order[] = [
          { id: "A-1", qty: 3, price: 19.99 },
          { id: "A-2", qty: 1, price: 49.50 },
          { id: "B-1", qty: 5, price: 4.25 },
        ];

        const total = orders.reduce((s, o) => s + o.qty * o.price, 0);
        const summary = {
          orders: orders.length,
          units: orders.reduce((s, o) => s + o.qty, 0),
          total: Number(total.toFixed(2)),
          node: process.version,
        };
        console.log(JSON.stringify(summary, null, 2));
    """)


    def main() -> None:
        sbx = Sandbox.create(template="node", timeout=120)
        try:
            for tool in ("node", "npm", "tsc"):
                r = sbx.commands.run(f"{tool} --version")
                print(f"{tool}: {r.stdout.strip()}")

            sbx.files.write("/tmp/orders.ts", SCRIPT)

            r = sbx.commands.run("cd /tmp && tsc --target ES2020 orders.ts && node orders.js")
            if r.exit_code != 0:
                print("compile/run failed:", r.stderr)
                return
            print("\nscript output:")
            print(r.stdout)
        finally:
            sbx.kill()


    if __name__ == "__main__":
        main()
    ```
  </Tab>

  <Tab title="TypeScript">
    ```typescript theme={null}
    import "dotenv/config";
    import { Sandbox } from "@declaw/sdk";

    const SCRIPT = `declare const process: { version: string };
    type Order = { id: string; qty: number; price: number };

    const orders: Order[] = [
      { id: "A-1", qty: 3, price: 19.99 },
      { id: "A-2", qty: 1, price: 49.50 },
      { id: "B-1", qty: 5, price: 4.25 },
    ];

    const total = orders.reduce((s, o) => s + o.qty * o.price, 0);
    const summary = {
      orders: orders.length,
      units: orders.reduce((s, o) => s + o.qty, 0),
      total: Number(total.toFixed(2)),
      node: process.version,
    };
    console.log(JSON.stringify(summary, null, 2));
    `;

    async function main(): Promise<void> {
      const sbx = await Sandbox.create({ template: "node", timeout: 120 });
      try {
        for (const tool of ["node", "npm", "tsc"]) {
          const r = await sbx.commands.run(`${tool} --version`);
          console.log(`${tool}: ${r.stdout.trim()}`);
        }

        await sbx.files.write("/tmp/orders.ts", SCRIPT);

        const r = await sbx.commands.run(
          "cd /tmp && tsc --target ES2020 orders.ts && node orders.js",
        );
        if (r.exitCode !== 0) {
          console.log("compile/run failed:", r.stderr);
          return;
        }
        console.log("\nscript output:");
        console.log(r.stdout);
      } finally {
        await sbx.kill();
      }
    }

    main().catch(console.error);
    ```
  </Tab>
</Tabs>

## Expected output

```
node: v20.20.2
npm: 10.8.2
tsc: Version 6.0.2

script output:
{
  "orders": 3,
  "units": 9,
  "total": 130.72,
  "node": "v20.20.2"
}
```

<Note>
  Need an actual TypeScript Declaw client inside the sandbox? `npm install
      @declaw/sdk` works at runtime against the `node` template — but for an
  agent-in-sandbox pattern, the `ai-agent` template is usually a better
  starting point.
</Note>
