Cannot find module ‘ts-node/register’ in TypeScript – How to fix it?

Cannot find module 'ts-node/register' in TypeScript

The error “Cannot find module ‘ts-node/register'” in TypeScript is a Typescript execution engine and REPL for Node.js. What is the cause of it and how to solve it? Let’s find out the answer by reading this article now.

The reason of the error “Cannot find module ‘ts-node/register'” in TypeScript

The error happened when you did not install ts-node successfully or got some errors when setting the ts-node package, which provided the ts-node/register module.

Example of how the error happens:

node -r ts-node/register

The error:

Error: Cannot find module 'ts-node/register'

The solution for this error

Install ts-node

To solve the error, you can install ‘ts-node’ and ‘typescript’ by that command:

npm install --save-dev ts-node typescript

Or with yarn:

That command line will help you install the ts-node package and save it in devDependencies.

Here is the result in the package.json file.

 "devDependencies": {
  "ts-node": "^10.9.1",
  "typescript": "^4.8.4"
 }

Here it means that I install the ts-node and Typescript package successfully. You can install a specific version of ts-node or Typescript by adding a version after the ‘@’ symbol added after the name of the package like this:

npm install --save-dev [email protected] [email protected]

Or if you want to install the latest version of ts-node and Typescript by running:

npm install --save-dev ts-node@latest typescript@latest

If the error does not solve, you can try to install ts-node and Typescript in the global scope that can apply to all projects in your device by this command:

npm install –g ts-node typescript

Or with yarn:

yarn add –g ts-node typescript

Re-create node_modules and package-lock.json file

If the error does not solve, you can try to delete the ‘node_modules’ and ‘package-lock.json’ files by this command:

rm –rf node_modules
rm –f package-lock.json

These two lines will help you delete the node_modules and package-lock.json files.

npm cache clean --force

This line will help you clear the cache in the npm.

npm install

This command will create node_modules and package-lock.json files and install all dependencies for your project.

After all installation and configuration, you should restart your IDE to apply the change.

Summary

In this article, I showed you how to solve the error “Cannot find module ‘ts-node/register'” in TypeScript. The core reason is the unsuccess of downloading the ts-node package, so you can solve it by installing it by the command above.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *