question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Native ESM] [ts-jest@next] ReferenceError: exports is not defined

See original GitHub issue

🐛 Bug Report

Under a certain condition with native ESM support, the test fails with ReferenceError: exports is not defined.

To Reproduce

package.json:

{
  "private": true,
  "type": "module",
  "devDependencies": {
    "@babel/core": "^7.13.1",
    "jest": "^27.0.0-next.3",
    "ts-jest": "^27.0.0-next.6",
    "typescript": "^4.1.5"
  },
  "scripts": {
    "test": "node --experimental-vm-modules node_modules/.bin/jest --no-cache"
  },
  "jest": {
    "transform": {
      "\\.[jt]sx?$": "ts-jest"
    },
    "extensionsToTreatAsEsm": [
      ".ts"
    ]
  }
}

lib.ts:

export const X = 42;

lib.test.ts:

import { X } from "./lib";

test("X", () => {
  expect(X).toBe(42);
});

tsconfig.json:

{
  "compilerOptions": {
    "target": "ESNext",
    "module": "ESNext",
    "lib": ["ES2015", "DOM"],
    "allowJs": true,
    "checkJs": true,
    "isolatedModules": true,
    "strict": true,
    "moduleResolution": "node",
    "esModuleInterop": true,
  }
}
$ node --version
v14.15.5
$ yarn test
yarn run v1.22.5
$ node --experimental-vm-modules node_modules/.bin/jest --no-cache
(node:23494) ExperimentalWarning: VM Modules is an experimental feature. This feature could change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
 FAIL  ./lib.test.ts
  ● Test suite failed to run

    ReferenceError: exports is not defined

      1 | import { X } from "./lib";
    > 2 |
        | ^
      3 | test("X", () => {
      4 |   expect(X).toBe(42);
      5 | });

      at lib.test.ts:2:23

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        2.343 s
Ran all test suites.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

And without --no-cache, the following behaviors are observed:

  • Change lib.ts a bit (e.g. adding / removing empty lines) and rerun the test: the test starts to fail.
  • Change lib.test.ts a bit (e.g. adding / removing empty lines) and rerun the test: the test starts to pass.

Expected behavior

The test is expected to always pass.

Link to repo (highly encouraged)

https://github.com/qnighy/ts-jest-esm-failure-example

The example above was extracted from https://github.com/qnighy/ts-type-module-example.

Debug log:

https://gist.github.com/qnighy/9ba2c6b2023c4a8dad38c53faf4ed592

envinfo

System:
    OS: Ubuntu 20.04 on WSL2
    Node version: v14.15.5

Npm packages:
    jest: 27.0.0-next.3
    ts-jest: 27.0.0-next.6
    typescript: 4.1.5
    babel(optional): 7.13.1

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6

github_iconTop GitHub Comments

4reactions
qnighycommented, Feb 23, 2021

Aaah, this one! https://kulshekhar.github.io/ts-jest/docs/next/guides/esm-support

Thanks! It turned out to be my mistake.

1reaction
ahnpnlcommented, Feb 23, 2021

I see you missed one important thing, enable support esm config option for ts-jest. If you go to online doc and choose next version, you will find the guide 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Typescript ReferenceError: exports is not defined
This worked for me (removed the above error). I'm using ESM with typescript (with jest), so I don't want exports. – crimbo. Jan...
Read more >
ReferenceError: exports is not defined in TypeScript | bobbyhadz
To solve the Uncaught ReferenceError: exports is not defined, add a script tag that defines an `exports` variable, e.g. `` above your JS...
Read more >
How to fix ReferenceError: exports is not defined TypeScript
ReferenceError: exports is not defined. Solution. If you are getting the error for code that runs in the browser, try defining a global...
Read more >
ReferenceError: exports is not defined in ES module scope
Node version: 18 I'm trying to run my project with: node dist/index.js My tsconfig.json "lib": ["ES5"] "module": "CommonJS" "allowJs": true…
Read more >
referenceerror: exports is not defined in es module scope this ...
Webpack, exports & ES modules: ReferenceError: module is not defined in ES ... be able to import various helpers from the base package...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found