Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Mocha or Node cannot find ts-node/register

ErrorError: 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.

  • Applies to ts-node 10, TypeScript 5, Mocha 10, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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 typescript
  2. 2Match what you see, then jump to its fix

    1. 1npm ls prints (empty) for ts-node.ts-node is not installed in the projectFix
    2. 2package.json has "type": "module" and the Mocha config uses require.A native ESM project needs the loader, not the require hookFix

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/register

2Fix

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/node

3Verify

Run the command that failed.

npx mocha --require ts-node/register --extensions ts 'tests/**/*.ts'

Confirmed by typestrong.org

Did this fix it?

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.json

2Fix

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 mocha

Confirmed by typestrong.org

Did this fix it?

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:

Related fixes

Back 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 fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.