Skip to content

[BUG] Type error during build when using RPC service binding to other worker #760

Open
@JoelYourstone

Description

@JoelYourstone

Describe the bug

When I try to add a service binding for RPC calls between an opennext worker and a normal worker - next build fails.

../my-first-worker/src/index.ts:16:61
Type error: Cannot find name 'Env'.

  14 | import { WorkerEntrypoint } from 'cloudflare:workers';
  15 |
> 16 | export default class MyFirstWorker extends WorkerEntrypoint<Env> {
     |                                                             ^
  17 |  async fetch(): Promise<Response> {
  18 |          return new Response('Hello World!');
  19 |  }
Next.js build worker exited with code: 1 and signal: null
npm error Lifecycle script `build` failed with error:
npm error code 1
npm error path C:\temp\wrangler-repro\workers\my-next-app
npm error workspace [email protected]
npm error location C:\temp\wrangler-repro\workers\my-next-app
npm error command failed
npm error command C:\WINDOWS\system32\cmd.exe /d /s /c next build

Steps to reproduce

https://github.com/JoelYourstone/cloudflare-next-repro

Using repo

  1. pnpm i
  2. cd workers/my-next-app
  3. pnpm run build

Set up from scratch

  1. Create a monorepo
  2. Create worker A as a normal worker, default settings pnpm create cloudflare@latest my-first-worker -> hello world -> worker only
  3. Create worker B as a next.js worker pnpm create cloudflare@latest my-next-app -> framework starter -> next.js
  4. Add a async callMe() { return 123; } in worker A. Refactor it to a class (extends WorkerEntrypoint<Env>) as per Cloudflare documentation for RPC
  5. Add a service binding in worker B's wrangler.jsonc, to worker A, with entrypoint "default" (also as per docs). E.g.
    "services": [
     {
       "binding": "MY_FIRST_WORKER",
       "service": "my-first-worker",
       "entrypoint": "default"
     }
    ]
    
  6. Generate types for Worker A (pnpm run cf-typegen)
  7. Generate types for Worker B, using the method as stated in docs, e.g. wrangler types --env-interface CloudflareEnv ./cloudflare-env.d.ts -c ./wrangler.jsonc -c ../my-first-worker/wrangler.jsonc, where the second -c is the worker you're binding to.
  8. Ensure you can call env.MY_FIRST_WORKER.callMe() (Worker A's function) from Worker B.
  9. Run next build pnpm run build in Worker B.

Expected behavior

It should build and not collide with Worker A's Env definition. It works fine in dev.

Excluding from tsconfig.json doesn't seem to work. Added "../my-first-worker/**/*" to the exclude array with no luck.

@opennextjs/cloudflare version

1.3.1

Wrangler version

4.22.0

next info output

npm error code ENOWORKSPACES
npm error This command does not support workspaces.
npm error A complete log of this run can be found in: C:\Users\joely\AppData\Local\npm-cache\_logs\2025-07-03T06_26_04_703Z-debug-0.log
warn  - Failed to fetch latest canary version. (Reason: Failed to get registry from "pnpm"..)
      Detected "15.3.4". Visit https://github.com/vercel/next.js/releases.
      Make sure to try the latest canary version (eg.: `npm install next@canary`) to confirm the issue still exists before creating a new issue.

Learn more: https://nextjs.org/docs/messages/opening-an-issue
This project is configured to use pnpm because C:\temp\wrangler-repro\package.json has a "packageManager" field

Operating System:
  Platform: win32
  Arch: x64
  Version: Windows 11 Pro
  Available memory (MB): 64730
  Available CPU cores: 32
Binaries:
  Node: 23.11.0
  npm: 11.4.2
  Yarn: N/A
  pnpm: 10.0.0
Relevant Packages:
  next: 15.3.4
  eslint-config-next: 15.3.4
  react: 19.1.0
  react-dom: 19.1.0
  typescript: 5.8.3
Next.js Config:
  output: N/A
Using vars defined in .dev.vars

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingtriage

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions