fix: resolve ESM-only serverExternalPackages in Node.js middleware - #3561
Open
adamhake wants to merge 2 commits into
Open
fix: resolve ESM-only serverExternalPackages in Node.js middleware#3561adamhake wants to merge 2 commits into
adamhake wants to merge 2 commits into
Conversation
Node.js middleware runs as a Deno edge function. CJS externals work via the virtual require() registry, but ESM-only packages are loaded with import() of a bare specifier (hashed under Turbopack). Deno's eszip cannot evaluate that dynamic import, which surfaced as HTTP 500 "nextHandler is not a function". Materialize those packages as real ESM files, statically import them, map both the package name and the Turbopack hashed specifier onto the namespace, and patch Turbopack's externalImport to use that table. Also emit an import_map on the internal edge-functions manifest so the bundler can include the files. Fixes opennextjs#3560
Satisfy unicorn/better-regex, no-plusplus, consistent-function-scoping, and explicit-length-check on the Node middleware ESM path.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Node.js middleware (
proxy.ts/runtime: 'nodejs') is served as a Deno edge function. CJSserverExternalPackageswork through the virtualrequire()registry, but ESM-only packages are loaded withimport()of a bare specifier (hashed under Turbopack). Deno's eszip cannot evaluate that dynamic import, so the handler never becomes a function and matched routes return HTTP 500nextHandler is not a function.This change:
externalImportto use the table instead ofimport(id)import_mapon the internal edge-functions manifest (supported by edge-bundler deploy_config)Documentation
No doc change in this PR. Netlify's Node.js middleware limitations currently list only C++ addons and filesystem; ESM externals should be called out if maintainers want that.
Tests
Added fixture
middleware-node-esm-externals(nanoidinserverExternalPackages) and an integration test that asserts:import_map.jsonis generated/protectedreturns 200 withx-request-idfromnanoid()npm run build node tests/prepare.mjs middleware-node-esm-externals npx vitest run --project integration tests/integration/middleware.test.ts -t "ESM-only"Minimal standalone repro: https://github.com/adamhake/netlify-node-middleware-esm-repro
Relevant links
Fixes #3560