Mocha or Node cannot find ts-node/register
Error: Cannot find module 'ts-node/register'Short answer
Node resolves ts-node/register from your project's node_modules, so the package must be installed there, next to typescript. Install both as dev dependencies, run from the project folder, and use the loader instead if the project is native ESM.
Fix it fast
1Find out which cause you have
Check whether ts-node and typescript are both listed as installed in this project folder.
npm ls ts-node typescript2Match what you see, then jump to its fix
What does the error look like?
Error: Cannot find module 'ts-node/register'
Cause 1 of 2
ts-node is not installed in the project
1Check
The ts-node install guide lists a local install and a global install as two separate options. Only a local one is found by a require inside your project.
ls node_modules/ts-node/register2Fix
Install ts-node and typescript as dev dependencies. Add tslib and @types/node if your configuration needs them.
npm install -D typescript ts-node @types/node3Verify
Run the command that failed.
npx mocha --require ts-node/register --extensions ts 'tests/**/*.ts'Confirmed by typestrong.org
Cause 2 of 2
A native ESM project needs the loader, not the require hook
1Check
The ts-node Mocha recipe uses require for CommonJS and a loader option for native ESM.
grep -n '"type"' package.json && cat .mocharc.json2Fix
In .mocharc.json replace the require entry with the loader.
{
"loader": "ts-node/esm",
"extensions": ["ts"],
"spec": ["tests/**/*.spec.ts"]
}3Verify
Run Mocha again.
npx mochaConfirmed by typestrong.org
How do I stop it coming back?
- Keep test tooling in devDependencies of the same package that runs the tests.
- Run scripts through npm scripts or npx so the project's node_modules is used.
- Commit the lockfile so CI installs the same versions.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- WorkforceA field app, an admin console and a self-hosted Node.js server for push and reconciliation.
Related fixes
Back end
TypeScript cannot find '@angular/platform-browser' in an Angular project
Cannot find module '@angular/platform-browser' or its corresponding type declarations.
TS2307 means TypeScript could not turn the import into a file.
3 causes, each with a fixBack end
Angular build fails with TS2707 or "has no exported member" after installing a library
Module '"@angular/core"' has no exported member
These errors mean a library was compiled by a newer Angular than your app uses.
3 causes, each with a fixBack end
cdk synth says --app is required
--app is required either in command-line, in cdk.json or in ~/.cdk.json
The CDK CLI needs a command that runs your app and finds it in three places: the --app option, cdk.json in the project folder, or cdk.json in your home folder.
3 causes, each with a fixBack end
TypeScript cannot find module '@nestjs/core' or its type declarations
error TS2307: Cannot find module '@nestjs/common' or its corresponding type declarations.
TS2307 means TypeScript searched node_modules and found neither the package nor its types.
3 causes, each with a fixBack end
TypeError: Class constructor cannot be invoked without 'new' in compiled TypeScript
TypeError: Class constructors cannot be invoked without 'new'
Node.js throws this when a native class is called like a plain function.
2 causes, each with a fixBack end
create-next-app keeps creating a TypeScript project
Would you like to use TypeScript? No / Yes
create-next-app now makes a TypeScript project by default.
2 causes, each with a fix