This error occurs when ts-node cannot find or load the TypeScript module. The issue is typically caused by missing TypeScript installation, incorrect module resolution, or version conflicts between ts-node and TypeScript.
When you run a TypeScript file with ts-node, it needs to load the TypeScript compiler module to transpile your code. The error "Cannot resolve typescript module for ts-node" means ts-node cannot locate or load the TypeScript module from your project's dependencies. This can happen for several reasons: 1. TypeScript is not installed in your project (missing from package.json) 2. TypeScript is installed globally but ts-node expects a local installation 3. There's a version mismatch between ts-node and TypeScript 4. Module resolution is failing due to incorrect Node.js module resolution settings 5. The TypeScript module is corrupted or not properly installed ts-node uses Node.js's module resolution algorithm to find TypeScript. If it can't find TypeScript where it expects, this error appears.
First, verify TypeScript is installed in your project:
# Check package.json
cat package.json | grep typescript
# Or check node_modules
ls node_modules | grep typescript
# Or use npm
npm list typescriptIf TypeScript is not listed, you need to install it.
Install TypeScript in your project:
# Install the latest stable version
npm install --save-dev typescript
# Or install a specific version compatible with your ts-node
npm install --save-dev [email protected]
# If using yarn
yarn add --dev typescript
# If using pnpm
pnpm add --save-dev typescriptAfter installation, verify it's in your package.json and node_modules.
Ensure your ts-node and TypeScript versions are compatible:
# Check installed versions
npx ts-node --version
npx tsc --version
# Or check package.json
cat package.json | grep -A2 -B2 'ts-node|typescript'Common compatibility issues:
- ts-node v10+ requires TypeScript v4.7+
- Older ts-node versions may not work with TypeScript v5+
Update if needed:
# Update both to latest compatible versions
npm install --save-dev ts-node@latest typescript@latestIf TypeScript appears installed but still not found, clear cache and reinstall:
# Clear npm cache
npm cache clean --force
# Remove node_modules and package-lock.json
rm -rf node_modules package-lock.json
# Reinstall all dependencies
npm install
# Alternative: Use npm ci for clean install
npm ciThis fixes corrupted installations and ensures clean module resolution.
Ensure you're using the local ts-node installation:
# Use npx (recommended)
npx ts-node your-file.ts
# Or explicitly use local binary
./node_modules/.bin/ts-node your-file.ts
# Add to package.json scripts
{
"scripts": {
"start": "ts-node src/index.ts"
}
}Avoid global ts-node installations which may have different module resolution paths.
If issues persist, configure ts-node's module resolution:
// tsconfig.json
{
"compilerOptions": {
"module": "commonjs",
"moduleResolution": "node",
"esModuleInterop": true
}
}Or use ts-node with explicit TypeScript path:
# Specify TypeScript compiler
npx ts-node --compiler typescript your-file.ts
# Or use ts-node programmatically
require('ts-node').register({
compiler: 'typescript',
transpileOnly: true
});### Global vs Local Installations
ts-node prefers local TypeScript installations. If you have TypeScript installed globally, ts-node may still fail because:
1. Global modules have different resolution paths
2. Permission issues with global npm installations
3. Version conflicts between global and local
Uninstall global TypeScript if not needed:
npm uninstall -g typescript### Monorepo Considerations
In monorepos (Turborepo, Nx, Lerna), TypeScript might be installed at root level. Solutions:
1. Install TypeScript in each package (recommended):
# In each package directory
npm install --save-dev typescript2. Use workspace protocol (if supported):
{
"devDependencies": {
"typescript": "workspace:*"
}
}3. Configure module resolution in tsconfig.json:
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"typescript": ["../node_modules/typescript"]
}
}
}### Docker and CI/CD Environments
In containerized environments:
1. Ensure TypeScript is in dependencies (not just devDependencies if needed at runtime)
2. Copy package.json and run npm install before running ts-node
3. Set NODE_PATH if using custom module resolution:
ENV NODE_PATH=/app/node_modules### Alternative: Use tsx or esbuild-runner
If ts-node issues persist, consider alternatives:
- tsx: Faster TypeScript execution
- esbuild-runner: Very fast, minimal configuration
- swc: Rust-based, extremely fast
# Install and use tsx
npm install --save-dev tsx
npx tsx your-file.tsFunction expression requires a return type
Function expression requires a return type
Value of type 'string | undefined' is not iterable
How to fix "Value is not iterable" in TypeScript
Type 'undefined' is not assignable to type 'string'
How to fix "Type undefined is not assignable to type string" in TypeScript
Type narrowing from typeof check produces 'never'
How to fix "Type narrowing produces never" in TypeScript
Type parameter 'T' has conflicting constraints
How to fix "Type parameter has conflicting constraints" in TypeScript