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.
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
npm install -D ts-node typescript
# ensure CI installs devDependencies (no --omit=dev)Register the loader in .mocharc
// .mocharc.json
{
"require": ["ts-node/register"],
"extension": ["ts"],
"spec": "test/**/*.spec.ts"
}How to prevent it
- Keep
ts-nodeandtypescriptin devDependencies and the lockfile. - Run
npm ci(with dev deps) in CI. - Register the loader through
.mocharcso it is consistent everywhere.