-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Feature]: Allow require
for pure ESM packages with --experimental-require-module
#15275
Comments
This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days. |
Not stale |
In node 23 the flag is enabled by default so require a pure ESM module is something allowed by default. Include this behavior in jest it will be very useful for projects can not be migrated to ESM |
This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days. |
unstale |
My temporary workaround is to use ts-jest for transpilation of ESM to CommonJS on the fly, example: jest config{
"jest": {
"globals": {
"ts-jest": {
"tsConfig": "tsconfig.test.json"
}
},
"moduleFileExtensions": [
"js",
"json",
"ts"
],
"roots": [
"<rootDir>/test/"
],
"testRegex": ".*\\.spec\\.ts$",
"transform": {
"^.+\\.(t|j)s$": [
"ts-jest",
{
"isolatedModules": true
}
],
"node_modules/get-port/index.js": [
"ts-jest",
{
"isolatedModules": true
}
],
"node_modules/p-do-whilst/index.js": [
"ts-jest",
{
"isolatedModules": true
}
],
"node_modules/p-forever/index.js": [
"ts-jest",
{
"isolatedModules": true
}
]
},
"transformIgnorePatterns": [
"/node_modules/(?!(get-port|p-do-whilst|p-forever)/)"
]
},
} tsconfig.test.json{
"compilerOptions": {
"allowJs": true
},
"extends": "./tsconfig.json"
} tsconfig.json{
"compilerOptions": {
"module": "commonjs",
"// some another options": "// some another options"
}
} |
This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days. |
Not stale |
in 22.13.0 (LTS), the flag is also now enabled by default. |
I had trouble using jest after requiring an ESM-only package in Node 22.13.0. I found a similar workaround to #15275 (comment) In my case, I had to use babel-jest to transform an ESM-only package from node_modules, and ts-jest was not handling the *.mjs source (probably some config issue on my side as it should handle it with the settings explained above). jest.config.ts ...
transform: {
'^.+\\.mjs$': 'babel-jest',
},
transformIgnorePatterns: ['/node_modules/(?!(@acme/package)/).*/'], // Force the transform of this package inside node_modules
... babel.config.js // Needed for jest.config.ts to use babel-jest, babel.config file name seems important
module.exports = {
presets: ['@babel/preset-env'],
}; |
🚀 Feature Proposal
Allow using
require
for pure ESM packages. This can be either the default behavior, or enabled by reading the--experimental-require-module
Node option, or by having a setting in the config (e.g.allowEsmRequire: true
).Motivation
There's an experimental
--experimental-require-module
flag in Node 22 and 20.17 that allows using CJSrequire
for pure ESM packages. This is great for CJS projects that cannot migrate to ESM yet (e.g. NestJS projects), because they can use modern versions of pure ESM dependencies.I was able to make it work by adding
babel-jest
to the project and configuring it to apply@babel/plugin-transform-modules-commonjs
plugin to my specific pure ESM dependency. But of course it's more desirable that this works out of the box.Example
jest.config.mjs
package.json
index.js
index.spec.js
Running the code ✅
Running Jest ❌
Pitch
Jest includes its own module resolution system. Lack of ESM
require
makes it not on par with Node's module resolution system, meaning that even if I can make the runtime code work with native Node, I have include some sort of transformations to make it work with Jest.The text was updated successfully, but these errors were encountered: