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.

[Bug]: can't use ESM with external node modules that export commonjs and moduel files

See original GitHub issue

Version

29.0.0

Steps to reproduce

  1. clone my repo - https://github.com/rbnayax/ts-jest-example
  2. run yarn
  3. run yarn test

Expected behavior

test should pass

Actual behavior

 FAIL  lib/fn/fn.spec.ts
  ● Test suite failed to run

    lib/fn/fn.ts:1:21 - error TS7016: Could not find a declaration file for module 'colorette'. '/home/regev/WebstormProjects/ts-jest-example/node_modules/colorette/index.cjs' implicitly has an 'any' type.
      Try `npm i --save-dev @types/colorette` if it exists or add a new declaration (.d.ts) file containing `declare module 'colorette';`

    1 import {green} from "colorette";
                          ~~~~~~~~~~~

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        1.761 s
Ran all test suites.
(node:415750) 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)

Debug log

link

Additional context

when removing the reference to 3rd party node module, everything works as expected

Environment

System:
    OS: Linux 5.14 Ubuntu 20.04.5 LTS (Focal Fossa)
    CPU: (8) x64 11th Gen Intel(R) Core(TM) i7-11370H @ 3.30GHz
  Binaries:
    Node: 16.11.1 - ~/.nvm/versions/node/v16.11.1/bin/node
    Yarn: 3.2.1 - ~/.nvm/versions/node/v16.11.1/bin/yarn
    npm: 8.0.0 - ~/.nvm/versions/node/v16.11.1/bin/npm
  npmPackages:
    jest: ^29.0.2 => 29.0.2

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:14

github_iconTop GitHub Comments

2reactions
goumscommented, Sep 15, 2022

Have you tried removing preset from your config? I think setting both preset and transform at once will cause two transformations.

removing the preset works for me when using ts-jest@29, thanks @okitan 👍

so maybe need to update the docs here: https://kulshekhar.github.io/ts-jest/docs/guides/esm-support#use-esm-presets as the example config is working fine on v28 but not in v29

2reactions
okitancommented, Sep 13, 2022

@rbnayax

Have you tried removing preset from your config? I think setting both preset and transform at once will cause two transformations.

Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeScript module "node16" does not work with CommonJS ...
Axios is declaring a default export in a CJS file. Typescript knows that ESM NodeJS will actually expose the module.exports as the "default ......
Read more >
Node Modules at War: Why CommonJS and ES ... - Code Red
CJS scripts use require() and module.exports ; ESM scripts use import and export . ESM and CJS are completely different animals.
Read more >
How to Create a Hybrid NPM Module for ESM and CommonJS.
Author your code in ES6, ES-Next or Typescript using import and export. From this base, you can import either ES modules or CommonJS...
Read more >
Error [ERR_MODULE_NOT_FOUND]: Cannot find module
What am I doing wrong? You're mixing node's CJS module system (require/module. exports) with ES6 modules (import/export). Don't do that unless ...
Read more >
CommonJS vs. ES Modules: Modules and Imports in NodeJS
In NodeJS each .js file is handled as a separate CommonJS module. ... The same way, we can import and use modules from...
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