ts-jest: [Bug]: error TS1343: The 'import.meta' meta-property is only allowed when the '--module' option is 'es2020', 'es2022', 'esnext', 'system', 'node16', or 'nodenext'.
[For context, I originally opened this issue in the jest repo, but @SimenB said I should open a ticket here.]
Version
29.0.3
Steps to reproduce
- Open up this SSCCE: https://replit.com/@broofa/JestImportMetaTest
- Click “Run”
Expected behavior
Test should run w/out errors.
Actual behavior
Test fails with the error shown in the title.
Specifically, here’s what I see:

Additional context
You can click “show files” in the repl.it window to see the tsconfig.json and package.json#jest setup.
As you’ll see, I have the following configuration, so it’s not clear what else I could/should do to work around this error:
{
"compilerOptions": {
"module": "es2020",
"moduleResolution": "node",
"target": "es2020",
"esModuleInterop": true,
"types": [
"jest"
]
}
}
Environment
~/JestImportMetaTest$ npx envinfo --preset jest.
Need to install the following packages:
envinfo
Ok to proceed? (y) y
No "jest." preset found.
npm notice
npm notice New major version of npm available! 7.20.3 -> 8.19.2
npm notice Changelog: https://github.com/npm/cli/releases/tag/v8.19.2
npm notice Run npm install -g npm@8.19.2 to update!
npm notice
~/JestImportMetaTest$
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 36
- Comments: 25
Commits related to this issue
- build: resolve design-tokens dependency related test errors (#8447) **Related Issue:** # ## Summary Fix design token test errors by adding [`ts-jest-resolver`](https://github.com/VitorLuizC/ts... — committed to Esri/calcite-design-system by benelan 3 years ago
- Fix node-extensions tests (#6849) Jest only has experimental support for ESM and is failing when parsing `import.meta` (ESM only) as it's assuming CJS. This PR mocks `import.meta` as suggested [he... — committed to AzureAD/microsoft-authentication-library-for-js by tnorling 3 years ago
I’ve linked the example repos. You can check them.
Okay, I think I have this figured out. Debugging this was complicated by the fact there were two issues, either one of which would result in the same (not-at-all obvious) error message. Leaving breadcrumbs for anyone else who struggles with this…
The two missing pieces (for me) were the following:
jesthas to be run withnode’s--experimental-vm-modulesflag. I.e. InvokejestasNODE_OPTIONS="--experimental-vm-modules" jest.jestwith thets-jest/presets/default-esmpreset:Sorry for the trouble @ahnpnl . I appreciate your patience. 🙌
@ahnpnl I’ve just spent the last half-hour reading the docs you linked to and trying every combination of
tsconfigandjest.config.jsI could find therein. Including …jestconfig out of package.json and into jest.config.js"presets": "ts-jest"to"presets": "ts-jest/presets/default-esm"presets"useESM": true"module": "CommonJS"in tsconfigtsconfig: 'tsconfig.json',in tsconfigtypes,target, andmoduleResolutionin tsconfing… and nothing helps. I keep getting the same error message.
I apologize for the trouble, but can you please tell me what configuration exactly I should be using here to get this to work?
Alternatively, you can use ts-jest-mock-import-meta npm package to fix this issue.
Install
In
jest.config.jsthis did solve the issue. 👌
tried it in an esModule project and the package references above causes an error saying the __filename has already been defined.
There is an open issue on his site with no fix/answer:
https://github.com/ThomZz/ts-jest-mock-import-meta/issues/5
I can’t believe after all these years, using modules is still so hard and cumbersome/broken 👎
@broofa Your first suggestion does not work for me. With that setting I now get the error:
so as if jest would try with common js modules, instead of ESM.
Configuring
jest-ts-webcompat-resolverhelped me in the same situation, I have no idea howHello, Did anyone find a solution to this issue? I have been trying to solve it for 5 days now. I would really appreciate some help .
I have tried the configurations that was suggested in this discussion. I am leaving my configurations below, let me know if I am doing anything wrong.
Error I am receiving
My jest.config.cjs File
My tsconfig.json file
cc: @ahnpnl
Update: I have solve the issue by using babel with babel-plugin-transform-import-meta.
Hey folks,
Shockingly enough, following what was in the example folders fixed the issue in minutes for me. The example folder now uses a function from
ts-jestto generate the right preset info, and in that function you ought to pass thetsconfig.jsonfile to use.With minor adjustments to match my project structure, I ended up with the following:
It worked for me! thank you very much 🫡
type: moduleinpackage.json,type-moduleis the example project.type: modulethe other examples are the one.In each example project, there is a
jest-esm.config.jswhich already points to thetsconfig.jsonit is using. Besides, inpackage.json, the script to run test already contains the flag to use ESM with NodeJS.Besides, if you are specifying
transform, please removepresetfrom your Jest config.The easiest way is you can clone one of the example apps and try out to compare with your repo. These example apps are included in CI so they are a good basic ones to start with.
Why do I still have to use
NODE_OPTIONS="--experimental-vm-modules" jestin (almost) 2025? Looking at the example repo intype-moduleI see six configuration files. SIX!Is all this really necessary to run a simple
"test": "jest"script and file likeexample.test.tswithIf you encounter this issue, I found that the simplest solution was to mock a constants file, as described in this stack overflow thread.
I am getting this same error, even with the following:
and