Last modified: Jul 02, 2026

Fix Cannot find module 'ts-node'

Understanding the Error

When you run a TypeScript file with ts-node and see "Cannot find module 'ts-node'", it means Node.js cannot locate the package. This error often occurs in new projects or after an incomplete installation. The module is missing from your local node_modules folder.

This error stops your script from running. It happens because ts-node is not installed or is installed globally instead of locally. Many beginners face this issue when they forget to add the package to their project.

Common Causes

The main reason is a missing local installation. You might have installed ts-node globally but your project expects it locally. Another cause is a corrupted node_modules folder or an outdated package version.

Sometimes, your package.json file lacks the dependency. If you cloned a project, the node_modules folder is not included. You must run npm install to fetch all dependencies.

Step-by-Step Fixes

1. Install ts-node Locally

The most reliable fix is to install ts-node as a dev dependency in your project. Open your terminal and run this command inside your project folder:

npm install --save-dev ts-node

This adds ts-node to your devDependencies in package.json. After installation, try running your script again:

npx ts-node your-file.ts

The npx command ensures Node.js uses the local package. If the error disappears, the fix worked.

2. Check Your package.json

Open your package.json file. Look for ts-node under devDependencies or dependencies. If it is missing, the installation did not complete. Run npm install again to sync the file.

If you are using a specific version, ensure it matches the TypeScript version in your project. For example, ts-node version 10 works well with TypeScript 4.x. Check your versions using:

npx tsc --version
npx ts-node --version

If versions conflict, update both packages.

3. Clear npm Cache and Reinstall

A corrupted cache can cause module errors. Clear the npm cache and delete the node_modules folder, then reinstall all dependencies:

npm cache clean --force
rm -rf node_modules package-lock.json
npm install

This forces a fresh installation. After that, install ts-node again if needed. This step often resolves stubborn errors.

4. Use npx for Global Installation

If you prefer a global installation, use npx to run ts-node without installing it locally. This works for one-time scripts. Run:

npx ts-node your-file.ts

The npx tool downloads ts-node temporarily. However, for regular use, a local installation is better. For more details on similar issues, check our guide on Fix Node.js Error: Cannot find module.

Example: Running a TypeScript File

Let's test a simple TypeScript file to ensure everything works. Create a file named hello.ts with this code:

// hello.ts - A simple TypeScript program
function greet(name: string): string {
  return `Hello, ${name}!`;
}
console.log(greet("World"));

Now run it with npx ts-node:

npx ts-node hello.ts

Expected output:

Hello, World!

If you see this output, the error is fixed. If you still see "Cannot find module 'ts-node'", double-check the installation steps above.

Alternative: Use tsx Instead

If you continue to face issues, consider using tsx as an alternative to ts-node. It is faster and easier to set up. Install it with:

npm install --save-dev tsx

Then run your file:

npx tsx hello.ts

The tsx tool works similarly but avoids some common ts-node errors. This is a good fallback if the main fix does not work.

Conclusion

The "Cannot find module 'ts-node'" error is easy to fix. Install the package locally with npm install --save-dev ts-node and use npx ts-node to run your files. Always check your package.json for missing dependencies. If problems persist, clear your cache and reinstall. For additional help with module errors, see our article on Fix Node.js Error: Cannot find module. With these steps, you can run TypeScript in Node.js smoothly.