Symptom. Building examples/nextjs-host prints a webpack warning and still compiles successfully:
⚠ Compiled with warnings
../../node_modules/.pnpm/mongodb@6.21.0/node_modules/mongodb/lib/deps.js
Module not found: Can't resolve 'aws4' in '.../mongodb/lib'
Import trace for requested module:
.../mongodb/lib/deps.js
.../mongodb/lib/client-side-encryption/client_encryption.js
.../mongodb/lib/index.js
../../packages/adapter-mongo/dist/index.js
./app/api/airside/[...path]/route.ts
Root cause. mongodb loads its optional native deps (aws4, kerberos, mongodb-client-encryption, snappy, @mongodb-js/zstd, …) through guarded dynamic require()s wrapped in try/catch (mongodb/lib/deps.js, loadAws4() etc.). None are installed, and at runtime the missing-module error is caught and ignored. webpack can't see the try/catch, so when it statically traces the module graph it reports the unresolved require('aws4') as a warning. The trace reaches mongodb because @airnauts/airside-adapter-mongo does a top-level value import — import { ..., MongoClient } from 'mongodb' in packages/adapter-mongo/src/repository.ts. (Db, Filter, UpdateFilter are import type and erased at build, so they contribute nothing.)
serverExternalPackages: ['mongodb'] in next.config.ts does not suppress the warning — webpack still bundles mongodb through the transitive pnpm-workspace import from the adapter's dist, so the externalization simply isn't matching.
Impact. None at runtime. The build completes; Mongo works normally. The warning is log noise only.
Proposed fix (validated in a throwaway worktree; deferred — not applied). Make the driver load lazily in the adapter so no bundler ever statically traces into mongodb. In packages/adapter-mongo/src/repository.ts:
-
Move MongoClient from the value import to the type-only import.
-
Load the constructor lazily at its only use site (connectMongo, already async):
const { MongoClient } = await import(/* webpackIgnore: true */ 'mongodb')
const client = new MongoClient(uri)
Verified: tsup/esbuild preserves the webpackIgnore comment in dist/index.js and emits no static from "mongodb"; examples/nextjs-host then builds with no aws4 warning; adapter-mongo tests stay green (33/33, including the mongodb-memory-server integration test exercising the dynamic-import path); typecheck clean.
Trade-off. Couples the library to a webpack-specific magic comment and defers the mongodb load to first connect (harmless for a server-only driver, arguably an improvement). It's a deliberate bundler-compat decision → land with a short ADR note. A host-side IgnorePlugin alternative works but only fixes this one example app, not every downstream consumer — hence the adapter-side fix is preferred.
Symptom. Building
examples/nextjs-hostprints a webpack warning and still compiles successfully:Root cause.
mongodbloads its optional native deps (aws4,kerberos,mongodb-client-encryption,snappy,@mongodb-js/zstd, …) through guarded dynamicrequire()s wrapped intry/catch(mongodb/lib/deps.js,loadAws4()etc.). None are installed, and at runtime the missing-module error is caught and ignored. webpack can't see thetry/catch, so when it statically traces the module graph it reports the unresolvedrequire('aws4')as a warning. The trace reachesmongodbbecause@airnauts/airside-adapter-mongodoes a top-level value import —import { ..., MongoClient } from 'mongodb'inpackages/adapter-mongo/src/repository.ts. (Db,Filter,UpdateFilterareimport typeand erased at build, so they contribute nothing.)serverExternalPackages: ['mongodb']innext.config.tsdoes not suppress the warning — webpack still bundles mongodb through the transitive pnpm-workspace import from the adapter'sdist, so the externalization simply isn't matching.Impact. None at runtime. The build completes; Mongo works normally. The warning is log noise only.
Proposed fix (validated in a throwaway worktree; deferred — not applied). Make the driver load lazily in the adapter so no bundler ever statically traces into
mongodb. Inpackages/adapter-mongo/src/repository.ts:Move
MongoClientfrom the value import to the type-only import.Load the constructor lazily at its only use site (
connectMongo, alreadyasync):Verified: tsup/esbuild preserves the
webpackIgnorecomment indist/index.jsand emits no staticfrom "mongodb";examples/nextjs-hostthen builds with noaws4warning;adapter-mongotests stay green (33/33, including themongodb-memory-serverintegration test exercising the dynamic-import path); typecheck clean.Trade-off. Couples the library to a webpack-specific magic comment and defers the mongodb load to first connect (harmless for a server-only driver, arguably an improvement). It's a deliberate bundler-compat decision → land with a short ADR note. A host-side
IgnorePluginalternative works but only fixes this one example app, not every downstream consumer — hence the adapter-side fix is preferred.