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

# Generating code

> Generate JavaScript source code in Bazel with Yeoman generators, scaffolding new Fastify routes and React components from templates to enforce conventions.

Organizations often set specific standards and conventions for engineers to follow. For example:
"Creating a new React component with a certain design standard."

## Templating new source code

Code generators help teams maintain consistent patterns and standards across a codebase. Instead of manually creating files from scratch, you can use templates to generate standardized components.

### Example: Using a Yeoman generator

This example uses a [Yeoman generator](https://www.npmjs.com/package/generator-bazel-fastify-route) to create a new Fastify route following a predefined template.

<Steps>
  <Step title="Run the generator" titleSize="h3">
    Run the generator from the Bazel module root (the directory containing `MODULE.bazel`). `npx` downloads the two packages to its cache and runs them without adding them to your workspace:

    ```bash theme={null}
    npx --yes \
      --package yo@4.3.1 \
      --package generator-bazel-fastify-route \
      yo bazel-fastify-route
    ```

    This generator uses Yeoman Generator 3, so pin the compatible Yeoman 4 runner shown above. Current Yeoman 7 releases are not compatible with it on Node.js 22.

    When prompted for a folder, enter `packages/routes`. The generator creates `index.js`, `package.json`, and `BUILD.bazel` in that folder.

    Open the generated `BUILD.bazel`. Its main target is an `npm_package` from `rules_js`:

    ```python theme={null}
    npm_package(
        name = "pkg",
        srcs = ["package.json", "index.js"],
        visibility = ["//visibility:public"],
    )
    ```

    Unlike `js_binary`, which launches an application, `npm_package` assembles an npm-compatible directory tree. That tree can be consumed by other Bazel targets or extended later with publishing rules. The generator therefore captures both the source convention and its Bazel packaging boundary.
  </Step>

  <Step title="Update the lockfile and build" titleSize="h3">
    The new folder matches the starter's `packages/*` workspace pattern. Update only the pnpm lockfile so Bazel discovers the new workspace package, then build it:

    ```bash theme={null}
    pnpm install --lockfile-only
    bazel build //packages/routes/...
    ```

    Bazel should report `//packages/routes:pkg` up to date and write the assembled package under `bazel-bin/packages/routes/pkg`.
  </Step>
</Steps>
