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

# Execute Code

> Run custom JavaScript or TypeScript with Bun and return named exports.

The **Execute Code** action runs a JavaScript or TypeScript module in a
temporary Bun container. Use it when a workflow needs a transformation that
available actions cannot express.

<Warning>
  Custom code can make network requests, import packages, consume resources, and
  expose any values connected as inputs. Review the code and every input before
  running it, especially credentials or personal data.
</Warning>

## Inputs

| Input                          | Type                     | Required  | Description                                                             |
| ------------------------------ | ------------------------ | --------- | ----------------------------------------------------------------------- |
| **Code**                       | String                   | Yes       | The module source to execute.                                           |
| **Language**                   | JavaScript or TypeScript | Yes       | The source language. Defaults to **TypeScript**.                        |
| **Input Variables**            | Key/value entries        | No        | JSON-serializable values exposed to the module.                         |
| **Name**                       | String                   | Per entry | The property name in the input object.                                  |
| **Value**                      | JSON value               | Per entry | The value available under that name.                                    |
| **Include Timestamps In Logs** | Boolean                  | No        | Prefixes captured log lines with local timestamps. Defaults to `false`. |

## Outputs

| Output      | Type   | Description                                                  |
| ----------- | ------ | ------------------------------------------------------------ |
| **Outputs** | Object | The JSON-serializable named exports from the module.         |
| **Logs**    | String | Captured standard output and standard error, joined as text. |

You can expose properties from **Outputs** as individual node outputs, then
connect only the values later actions need.

## Read inputs and return outputs

Import the default export from `./inputs`, then export each result as a named
value:

```ts theme={null}
import inputs from "./inputs"

const subtotal = Number(inputs.subtotal)
const taxRate = Number(inputs.taxRate)

export const tax = subtotal * taxRate
export const total = subtotal + tax

console.log(`Calculated total for ${subtotal}`)
```

Configure **Input Variables** with `subtotal` and `taxRate`. The action returns:

```json theme={null}
{
  "tax": 8,
  "total": 108
}
```

The message written with `console.log` appears in **Logs**, not **Outputs**.

<Tip>
  Use identifier-like input names such as `customerId` or `lineItems`. They are
  easier to access as `inputs.customerId` and produce useful TypeScript editor
  hints.
</Tip>

## Import packages

Code runs with the Bun runtime, so standard imports are supported:

```ts theme={null}
import inputs from "./inputs"
import { groupBy } from "lodash-es"

export const grouped = groupBy(inputs.orders, "status")
```

Bun can resolve npm package imports during execution. Version specifiers may be
used when the exact package version matters.

## Serialization rules

Inputs and outputs cross the action boundary as JSON:

* Inputs may contain strings, numbers, booleans, `null`, lists, and objects.
* Exported functions, symbols, `undefined`, and other non-JSON values are not
  reliable outputs.
* `BigInt`, circular objects, and other values that JSON cannot serialize cause
  the run to fail.
* Class instances lose behavior when serialized; return plain data instead.

<Note>
  The code is executed as a module. Only exported values are returned. Local
  variables stay private unless you add a named export.
</Note>

## Troubleshooting

<AccordionGroup>
  <Accordion title="An input is undefined">
    Confirm that its **Input Variables** name matches the property read from the
    default `./inputs` export. Names are case-sensitive.
  </Accordion>

  <Accordion title="Outputs is empty">
    Add named exports such as `export const result = value`. Logging a value does
    not return it.
  </Accordion>

  <Accordion title="The run fails while saving results">
    Check every exported value for JSON compatibility. Remove functions, `BigInt`,
    circular references, and unsupported runtime objects.
  </Accordion>

  <Accordion title="A package import fails">
    Check the package name and import path. If reproducibility matters, specify
    a supported npm version in the import rather than relying on the latest
    resolved package.
  </Accordion>
</AccordionGroup>
