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
- Create a CommonJS project.
- Import
migrate-mongo
- 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
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 CommonJSrequire('migrate-mongo'). Hence, at runtime our app will load themigrate-mongo.cjsas defined in thepackage.json#exports.However, the code in
migrate-mongo.cjsis not the same asmigrate-mongo.js. It exposes promises for everything rather than functions. As a result, we get this error:TypeError: migrateMongo.config.set is not a functionI solved the issue locally by applying a
pnpm patchthat simply removes theexportsfrom thepackage.json. That way when werequire('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.cjsfile, 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
Steps to Reproduce
migrate-mongoExpected 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