29

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

3
  • 1
    I'm working on this same issue. Looks like the issue here: github.com/node-fetch/node-fetch/issues/1279 Mar 15, 2022 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. Mar 16, 2022 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. Jan 20 at 9:20

8 Answers 8

22

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"
  ]
}
1
  • 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. Jun 21 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

3
8
+50

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.

2
  • 6
    Thank you for the answer. I am sorry, but "esModuleInterop": true did not help (I had this option initially). Also tried "include": [ "Source/**/*" ]. Mar 19, 2022 at 1:57
  • Can you post your config and your import statements?
    – Nice-Guy
    Mar 20, 2022 at 2:54
3

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

2

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 [email protected] installed:

✗ npm run test      

> @[email protected] 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

> @[email protected] 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).

1

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"}
1

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

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"
    ]
}

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.