26

Before ask this question, I checked similar topics and tried typical solutions.

I know what the frequent cause is "module": "ESXXXX" in TypeScript configuration. In my case, I have error

TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for D:\IntelliJ IDEA\XXXXXX\node_modules\tsconfig-paths\src\__tests__\config-loader.test.ts

in both "module": "ESnext" and "module": "CommonJS" cases.

One of typical solution is usage of ts-node/esm. First, this feature is experimental. Next, it just replace one error with another:

(node:24788) ExperimentalWarning: --experimental-loader is an experimental feature. This feature could change at any time
(Use `node --trace-warnings ...` to show where the warning was created)

× ERROR: CustomError: Cannot find module 'D:\IntelliJ IDEA\XXXXX\node_modules\tsconfig-paths\register' imported from D:\IntelliJ IDEA\XXXXX\node_modules\mocha\lib\nodejs\esm-utils.js

Versions

  • mocha: 9.2.1
  • ts-node: 10.7.0

Mocha config

extension:
  - ts

spec: "**/*.test.ts"

require:

  - ts-node/register
  - tsconfig-paths/register

loader: ts-node/esm # Tried with and without

Takeshi Tokugawa YD
  • 670
  • 5
  • 40
  • 124
  • 1
    I'm working on this same issue. Looks like the issue here: https://github.com/node-fetch/node-fetch/issues/1279 – Craig Fisher Mar 15 '22 at 23:31
  • @CraigFisher, Thank you for listening of the voice of us, Mocha users. Would you please to write the cause in answer? I'll give you the reputation points. – Takeshi Tokugawa YD Mar 16 '22 at 10:01
  • 1
    What a nightmare. I was getting this because had one new package that was ESM, but the test run wasn't telling me that was the cause at all. I was only getting this really generic error. So also check if you have any only ESM modules. If you can avoid them, I guess do so as it's a whole world of hurt. – Reece Daniels Jan 20 '23 at 09:20

8 Answers8

18

i'm using same configuration like yours but it only work when i downgrade to ts-node@9, and then i tried this option in my .mocharc.json and now it's working as i expected

{
  "extensions": ["ts"],
  "spec": ["**/*.spec.*"],
  "node-option": [
    "experimental-specifier-resolution=node",
    "loader=ts-node/esm"
  ]
}
martiendt
  • 1,909
  • 1
  • 17
  • 27
  • Downgrading ts-node to @9 helped me as well, even despite I have a different setup: `mocha -r ts-node/register -r tsconfig-paths/register tests/startup.ts tests/**/*.test.ts`. It's absolutely strange since I have a few repositories, and in one of them `ts-node@10` went fine. Might be a dependency clash. In this repo where I needed `@9`, I'm using typia.io for dynamic types validation. – ZitRo Jun 21 '23 at 11:58
12

I had the same error and it worked for me to set the NODE_OPTIONS env var to specify the loader:

NODE_OPTIONS="--loader ts-node/esm" mocha

Taken from here: https://typestrong.org/ts-node/docs/imports#native-ecmascript-modules

It might also help to upgrade to the latest version of ts-node 10.8.0

zirkelc
  • 1,451
  • 1
  • 23
  • 49
7

Do you have a tsconfig.json. That solution could help here:

{
  "compilerOptions": {
    "esModuleInterop": true,
  }
}

Here is an interesting thread about it with an alternative solution. In that case, the tsconfig has an include that looks like this:

"include": [
    "./**/*.ts"
]

One of these two options should work, but let me know. Not sure what your config looks like.

STh
  • 746
  • 9
  • 24
Nice-Guy
  • 1,457
  • 11
  • 20
3

I added "type": "module" in package.json and did npx tsx file.ts instead of using ts-node file.ts and it worked.

John Targaryen
  • 1,109
  • 1
  • 13
  • 29
1

This absolutely misleading error with mocha/ts-node may happen in case any of your dependencies are ES6 modules (!), but your TypeScript target is not. For instance, when having a package got@13.0.0 installed:

✗ npm run test      

> @mypackage@1.6.0 test
> mocha -r ts-node/register tests/**/*.test.ts


TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for /.../tests/index.test.ts
...

And now downgrading to npm i got@11, which still provides CommonJS exports:

npm run test

> @mypackage@1.6.0 test
> mocha -r ts-node/register tests/**/*.test.ts

// All right

So a "quick fix" for this annoying error might be to downgrade a certain package you have just upgraded (typically, a major version). This is related to got, node-fetch, and other packages that decide to publish ES6 only code.

Otherwise, you have to update your entire test suite (at least, I don't know any fix in mocha or ts-node for it; if not mocha, playwright worked well with ES6 deps).

ZitRo
  • 1,163
  • 15
  • 24
0

ts-node/register would do the trick.

Node developers: Add this to your package.json file

"scripts": {"test": "mocha --parallel -r ts-node/register /path/to/test.ts"}
GM_
  • 127
  • 7
0

If your package.json is set to "type": "module" try the following (taken from https://github.com/mochajs/mocha-examples/issues/47)

tsconfig.json

{
    "compilerOptions": {
        "module": "esnext",
        "moduleResolution": "node",
    }
}

.mocharc.json

{
    "node-option": [
        "experimental-specifier-resolution=node",
        "loader=ts-node/esm"
    ]
}
Brent
  • 4,611
  • 4
  • 38
  • 55
0

May be it is too late a bit, but may be help someone:

For me the cause was "too new" url-join depenendency.

So this can happen even if your package.json do not have type: "module" or such, but you have a dependency with type set to module as url-join latest, here error goes.

Mentioned here: https://github.com/piotrwitek/ts-mocha/issues/70#issuecomment-1027582584

Kote Isaev
  • 273
  • 4
  • 13