How To Solve ‘TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension “.ts” for…’ Error
If you’re using Chai 5, Mocha, and ts-node together, probably you may have encountered the error specified in the title.
First, let’s reproduce the issue again:
- Create an empty project.
$ mkdir sample-project &&\
cd sample-project &&\
npm init -f
- Install the project dependencies.
$ npm install - save-dev mocha \
typescript \
ts-node \
chai \
@types/mocha \
@types/chai
- Create a simple typescript configuration file (tsconfig.json) as the one below:
{
"compilerOptions": {
"target": "ES2017",
"module": "commonjs",
"moduleResolution": "Node",
"sourceMap": true,
"outDir": "./dist",
"esModuleInterop": true,
"strict": true,
"skipLibCheck": true
}
}
- Create a simple test case like the one below without adding any assertion
import { expect } from 'chai';
describe('Example test case', function () {
it('should complete successfully', function () {
const var1 = 1;
const var2 = 2;
const total = var1 + var2;
const expectedValue = 3;
// expect(total).to.eql(expectedValue);
})
})
- Create a ‘test’ script in the scripts section in the package.json file to execute the created test file
// package.json
{
//...
"scripts": {
"test": "mocha -r ts-node/register '*.test.ts'"
},
//...
}
- First, let’s run the test to see if it does not fail without calling expect() function from the chai library.
npm run test
> sample-project@1.0.0 test
> mocha -r ts-node/register '*.test.ts'
Example test case
✔ should complete successfully
1 passing (3ms)
If you saw an output like the one above, let’s try to make it failed by uncommenting the line calls expect() function.
expect(total).to.eql(expectedValue);
Re-run the test
npm run test
> sample-project@1.0.0 test
> mocha -r ts-node/register '*.test.ts'
Exception during run: TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for /home/ibrahimgunduz/projects/personal/sample-project/demo.test.ts
at new NodeError (node:internal/errors:371:5)
at Object.getFileProtocolModuleFormat [as file:] (node:internal/modules/esm/get_format:87:11)
at defaultGetFormat (node:internal/modules/esm/get_format:102:38)
at defaultLoad (node:internal/modules/esm/load:21:14)
at ESMLoader.load (node:internal/modules/esm/loader:359:26)
at ESMLoader.moduleProvider (node:internal/modules/esm/loader:280:58)
at new ModuleJob (node:internal/modules/esm/module_job:66:26)
at ESMLoader.#createModuleJob (node:internal/modules/esm/loader:297:17)
at ESMLoader.getModuleJob (node:internal/modules/esm/loader:261:34)
at async Promise.all (index 0)
at async ESMLoader.import (node:internal/modules/esm/loader:337:24)
at async importModuleDynamicallyWrapper (node:internal/vm/module:437:15)
at async formattedImport (/home/ibrahimgunduz/projects/personal/sample-project/node_modules/mocha/lib/nodejs/esm-utils.js:9:14)
at async Object.exports.requireOrImport (/home/ibrahimgunduz/projects/personal/sample-project/node_modules/mocha/lib/nodejs/esm-utils.js:42:28)
at async Object.exports.loadFilesAsync (/home/ibrahimgunduz/projects/personal/sample-project/node_modules/mocha/lib/nodejs/esm-utils.js:100:20)
at async singleRun (/home/ibrahimgunduz/projects/personal/sample-project/node_modules/mocha/lib/cli/run-helpers.js:162:3)
at async Object.exports.handler (/home/ibrahimgunduz/projects/personal/sample-project/node_modules/mocha/lib/cli/run.js:375:5) {
code: 'ERR_UNKNOWN_FILE_EXTENSION'
The issue occurs because Chai is migrated to ESM starting from v5.
https://github.com/chaijs/chai-http/releases/tag/5.0.0
Solution 1:
Modify the following options from the tsconfig.json file
{
// "module": "commonjs",
// "moduleResolution": "Node",
// "target": "ES2017",
"module": "nodenext",
"moduleResolution": "nodenext",
"target": "esnext",
}
- Create or update your .mocharc.json file in the following shape
// .mocharc.json
{
//..
"loader": "ts-node/esm",
"extensions": ["ts"]
//..
}%
- Run the test again:
npm run test
> sample-project@1.0.0 test
> mocha -r ts-node/register '*.test.ts'
(node:899490) 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)
Example test case
✔ should complete successfully
1 passing (5ms)
Solution 2:
Downgrade the Chai package to the latest 4.x version.
You can find the complete source code with the first solution in the following Github repository.
https://github.com/ibrahimgunduz34/blog-code-examples/tree/master/unknown-file-extension-ts