Skip to content

[BUG] migrate-mongo.cjs is not a proper wrapper for the ESM code #475

@Simon-Hayden-Dev

Description

@Simon-Hayden-Dev

Bug Description

We are using NestJS (which doesn't support ESM) and on app boot run migrate-mongo in-code rather than as a CLI step.

In other words we import * as migrateMongo from 'migrate-mongo'; in our TypeScript code, which gets compiled down to CommonJS require('migrate-mongo'). Hence, at runtime our app will load the migrate-mongo.cjs as defined in the package.json#exports.

However, the code in migrate-mongo.cjs is not the same as migrate-mongo.js. It exposes promises for everything rather than functions. As a result, we get this error:

TypeError: migrateMongo.config.set is not a function

I solved the issue locally by applying a pnpm patch that simply removes the exports from the package.json. That way when we require('migrate-mongo') NodeJS will recognize that it is an ES module and handles the inter-op for us.


There is also a second issue with the provided CJS when testing with Jest (and potentially others). Jest doesn't fully support ESM yet. Additionally, Jest uses import/require hooks in order to provide mocked versions of libraries. When you run the dynamic import() inside your .cjs file, Jest will throw an error.

We did fix this by mocking migrate-mongo, such that the actual files aren't even executed at all. But it's not really elegant. And I still need to find a solution for our integration tests.

Environment

  • migrate-mongo version: 14.0.7
  • Node.js version: 22.22.0
  • MongoDB version: Docker 8.2, npm package 6.21.0
  • Operating System: Fedora Linux

Steps to Reproduce

  1. Create a CommonJS project.
  2. Import migrate-mongo
  3. Try to use it like you would the ESM version

Expected Behavior

The CJS version should have the same interface as the ESM version.

Actual Behavior

The CJS version exposes Promises for everything, while the ESM version exposes functions.

Error Message

See above.

Additional Context

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions