Skip to content
Latchkey

Mocha "Cannot find module 'ts-node/register'" in CI

Mocha was told to load TypeScript via --require ts-node/register, but ts-node is not installed in the CI environment. The require fails before any test runs.

What this error means

Mocha aborts at startup with "Error: Cannot find module 'ts-node/register'." It usually means ts-node (or typescript) is missing from devDependencies, or CI installed without dev packages.

mocha
Error: Cannot find module 'ts-node/register'
Require stack:
- node_modules/mocha/lib/cli/cli.js
    at Module._resolveFilename (node:internal/modules/cjs/loader)

Common causes

ts-node not installed

ts-node (and often typescript) is missing from node_modules - never added, or pruned by npm ci --omit=dev in CI.

Registered without being a dependency

A .mocharc or CLI flag requires ts-node/register, but the package was only ever installed globally on a developer machine, not in the project.

How to fix it

Install ts-node and typescript

Terminal
npm install -D ts-node typescript
# ensure CI installs devDependencies (no --omit=dev)

Register the loader in .mocharc

.mocharc.json
// .mocharc.json
{
  "require": ["ts-node/register"],
  "extension": ["ts"],
  "spec": "test/**/*.spec.ts"
}

How to prevent it

  • Keep ts-node and typescript in devDependencies and the lockfile.
  • Run npm ci (with dev deps) in CI.
  • Register the loader through .mocharc so it is consistent everywhere.

Frequently asked questions

What causes ""Cannot find module 'ts-node/register'""?
ts-node (and often typescript) is missing from node_modules - never added, or pruned by npm ci --omit=dev in CI.
How do I fix "Cannot find module 'ts-node/register'"?
Install ts-node and typescript

Related guides

References

Latchkey auto-heals failures like this one - detected, fixed, and retried without you. Start free → 30-day trial · No credit card