Skip to content

Adapters: mongodb optional-dep aws4 build warning when bundled by Next.js #54

Description

@MateuszPaulski

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.

Activity

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

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions