Solve - Cannot find module 'ts-node/register' Error in TS

avatar

Borislav Hadzhiev

Sat Mar 26 20222 min read

banner

Photo by Clay Banks

Solve - Cannot find module 'ts-node/register' Error in TS #

To solve the error "Cannot find module 'ts-node/register'", install ts-node and typescript as development dependencies by running npm install --save-dev ts-node typescript.

Open your terminal in your project's root directory (where your package.json file is located) and run the following command:

shell
npm install --save-dev ts-node typescript

This will add the ts-node package to the development dependencies of your project.

If the error is not resolved, try to delete your node_modules and package-lock.json (not package.json) files, re-run npm install and restart your IDE.

shell
# 👇️ delete node_modules and package-lock.json rm -rf node_modules rm -f package-lock.json # 👇️ clean npm cache npm cache clean --force npm install
Make sure to restart your IDE and dev server if the error still persists. VSCode often glitches and a reboot solves things sometimes.

If the error is not resolved, try to install the packages globally, by opening your terminal in your project's root directory and running the following commands:

shell
# ✅ Install ts-node globally npm install -g ts-node typescript # ✅ Create a symbolic link from the global package # to node_modules/ of current folder npm link ts-node npm link typescript

Once you run the two commands, the error should be resolved.

If the global installation of ts-node fails, you might have to run the command prefixed with sudo.
shell
# 👇️ If you got permissions error, run with sudo sudo npm install -g ts-node typescript npm link ts-node npm link typescript

The npm link command creates a symbolic link from the globally installed package to the node_modules/ directory of the current folder.

If you're still getting the "Cannot find module 'ts-node/register'" error, open your package.json file and make sure it contains the ts-node package in the devDependencies object.

package.json
{ "devDependencies": { "ts-node": "^10.7.0", "typescript": "^4.6.3" }, }

You can try to manually add the line and re-run npm install.

shell
npm install

Or install the latest version of the packages by running:

shell
npm install --save-dev ts-node@latest typescript@latest
Use the search field on my Home Page to filter through my more than 1,000 articles.