fix(builders): remove external option from workflow bundle to fix require() errors#830
fix(builders): remove external option from workflow bundle to fix require() errors#830technopahadi wants to merge 2 commits intovercel:mainfrom
Conversation
…uire() errors
When packages are listed in serverExternalPackages and marked as external
in the workflow bundle, esbuild generates require() calls for them (since
format is 'cjs'). However, the workflow VM created via vm.runInContext()
does not have require() defined - it only provides module.exports and exports.
This causes runtime errors like:
ReferenceError: require is not defined
at anonymous (../../node_modules/ai/src/telemetry/assemble-operation-name.ts:19:4)
The fix removes the external option from the workflow bundle esbuild config.
Instead of externalizing packages (which generates broken require() calls),
we bundle everything and rely on createNodeModuleErrorPlugin() to catch
Node.js builtin imports at build time with helpful error messages.
This restores the behavior from before beta.40 when the external option
was not present in the workflow bundle config.
|
|
@technopahadi is attempting to deploy a commit to the Vercel Labs Team on Vercel. A member of the Team first needs to authorize it. |
|
@technopahadi thanks for finding this and fixing it. Could you please resolve the DCO check and include a changeset? Also it would be awesome to add an e2e test that ensures this doesn't regress again |
|
also this change might in turn cause a regression to #731 (@TooTallNate)? |
There was a problem hiding this comment.
Pull request overview
This PR fixes a workflow bundling regression where externalized packages caused require is not defined errors inside the workflow VM.
Changes:
- Remove the
externaloption from the workflow esbuild config so that all dependencies are bundled into the workflow bundle. - Add detailed inline comments explaining why
externalis not used for workflows and howcreateNodeModuleErrorPlugin()is relied on to catch Node.js builtin imports.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Adds e2e test to verify packages in serverExternalPackages work correctly in workflow code. The test uses lodash.chunk to ensure the workflow bundle doesn't generate require() calls that would fail in the VM.
|
@pranaygp Can you review the test? |
| external: this.config.externalPackages || [], | ||
| // NOTE: We intentionally do NOT use the external option here for workflow bundles. | ||
| // When packages are marked external with format: 'cjs', esbuild generates require() calls. | ||
| // However, the workflow VM (vm.runInContext) does not have require() defined - it only |
|
Thanks for submitting a PR. This needed a bit more handling and has been fixed with this change |
Problem
When using packages like ai (Vercel AI SDK) in workflow code, users encounter a runtime error:
This regression was introduced in
beta.40when theexternaloption was added to the workflow bundle esbuild config.Root Cause
The workflow bundle is configured with:
format: 'cjs'- because the VM expects CommonJSplatform: 'neutral'- because it's neither Node.js nor browserexternal: this.config.externalPackages || []- to externalize packages fromserverExternalPackagesWhen a package is marked as external with format: 'cjs', esbuild generates require() calls for those packages:
However, the workflow VM created via
vm.runInContext()does not haverequire()defined. Looking atpackages/core/src/vm/index.ts, the VM context only provides:There is no
requirefunction, so any externalized package fails at runtime.Solution
Remove the
externaloption from the workflow bundle esbuild config increateWorkflowsBundle(). This restores the behavior from beforebeta.40.Instead of externalizing packages (which generates broken
require()calls), we:createNodeModuleErrorPlugin()to catch Node.js builtin imports at build time with helpful error messagesVerification
Before fix (beta.40+):
After fix:
Breaking Change?
No. This restores the original behavior from
beta.39and earlier. Users who were affected by therequire is not definederror will now have working workflows.Test Case
serverExternalPackagesin Next.js confignext dev --turbopack -p 3001ornext buildand trigger the workflowReferenceError: require is not defined