Skip to content

ReferenceError: require is not defined #1161

Closed

Description

@pensono

I often get an error message like this one.
ReferenceError: require is not defined

Why does it happen? What should I do to work around it?

[Workflow] Error while running workflow {
  workflowRunId: 'wrun_01KJ34497C1A7Z97YD8KS2T8VY',
  errorName: 'ReferenceError',
  errorStack: './workflows/backfill:22445\n' +
    'var import_os = __toESM(require("os"), 1);\n' +
    '                ^\n' +
    '\n' +
    'ReferenceError: require is not defined\n' +
    '    at anonymous (node_modules/.pnpm/postgres@3.4.8/node_modules/postgres/src/index.js:1:15)\n' +
    '    at Script.runInContext (node:vm:149:12)\n' +
    '    at runInContext (node:vm:301:6)\n' +
    '    at F:\\Data\\Code\\Website\\my-project\\.next\\dev\\server\\chunks\\04ddb_@workflow_core_dist_4d58fc8f._.js:4041:158\n' +
workflow: 4.1.0-beta.57,
turbo: 2.5.8,
next: 16.1.1,

Activity

  1. pensono commented on Feb 22, 2026

    @pensono
    Author

    I traced the issue down to an import from the posgres package.

    I was able to fix the issue by reorganizing my steps so that the function I imported from this package was only used in a step and not in a workflow.

    The error message is quite confusing though, there's no context as to why this happened. For example, it would have been useful to see somewhere that this error occurred when evaluating in the workflow's context.

  2. Niapya commented on Mar 4, 2026

    @Niapya

    same issues. I don't even know which step or which library caused the problem.

  3. pensono commented on Mar 5, 2026

    @pensono
    Author

    Noting that this can happen if there's a bad import in any of the files any workflows, not just the one which you are trying to start.

  4. VaguelySerious commented on Apr 3, 2026

    @VaguelySerious
    Member

    @pensono @Niapya Are you still seeing this on the latest version?

  5. lra commented on Sep 30, 2026

    @lra

    This still happens on workflow 4.8.1 (@workflow/builders 4.1.6, @workflow/swc-plugin 4.1.2). Could this be reopened? It took down every workflow in our production app, and the build gave no warning.

    Minimal repro (Next.js + withWorkflow):

    // workflows/steps.ts
    import pg from 'pg'
    
    // A non-step export keeps the `pg` import alive in the workflow bundle
    export function makePool() {
      return new pg.Pool()
    }
    
    export async function myStep() {
      'use step'
      return 1
    }
    // workflows/my-workflow.ts
    import { myStep } from './steps'
    
    export async function myWorkflow() {
      'use workflow'
      return await myStep()
    }

    next build / next typegen succeed with no workflow-node-module-error. At runtime, every workflow run fails when the bundle loads, including workflows that never touch steps.ts:

    ReferenceError: require is not defined
    

    Why it gets through: in workflow mode, the SWC transform replaces step bodies and removes unused code, but it keeps non-step exports and everything they reference. So makePool keeps pg in the bundle. pg is CommonJS, so esbuild emits require(...) calls, which the node:vm context can't run. The workflow-node-module-error check (after #1821) only looks at node:/bun: built-ins that survive into the bundle, so it never sees a require coming from inside an npm package. Removing the makePool export makes pg drop out of the bundle.

    Suggestions, in order of impact:

    1. Fail the build when the final workflow bundle still contains a CommonJS require call, and use the esbuild metafile to name the import chain that brought it in. This would have caught both this issue and our incident.
    2. Document that non-step exports of a step file stay in the workflow bundle together with their imports. Today the docs describe the step placeholders but not this.
    3. Say where the error came from (related: Bad error messages when attempting to run invalid libraries in workflow context #153): the current message doesn't say it happened while loading the workflow bundle, or which module failed.

    As a workaround, we now run a CI check that loads the generated flow-route bundle through runWorkflow from @workflow/core with a workflow name that doesn't exist. If it throws anything other than WorkflowNotRegisteredError, the bundle can't load. A public API for that ("evaluate the workflow bundle and report load errors") would let other users do the same without importing an internal file.

  6. VaguelySerious commented on Sep 30, 2026

    @VaguelySerious
    Member

    Linking to #4268

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions