This article is about fixing error when tsconfig-paths doesn't work with node (works with ts-node) dividab tsconfig-paths
  • 12-Feb-2023
Lightrun Team
Author Lightrun Team
Share
This article is about fixing error when tsconfig-paths doesn't work with node (works with ts-node) dividab tsconfig-paths

tsconfig-paths doesn’t work with node (works with ts-node) in dividab tsconfig-paths

Lightrun Team
Lightrun Team
12-Feb-2023

Explanation of the problem

The following error message was encountered when executing the command node -r tsconfig-paths/register dist/index.js in the project guild-review:

module.js: 550
    throw err;
    ^

Error: Cannot find module '@modules/webhooks'
    at Function.Module._resolveFilename (module.js:548:15)
    at Function.Module._resolveFilename (/home/niko/WebstormProjects/guild-review/node_modules/tsconfig-paths/lib/register.js:73:40)
    at Function.Module._load (module.js:475:25)
    at Module.require (module.js:597:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (/home/niko/WebstormProjects/guild-review/server/dist/index.js:5:20)
    at Module._compile (module.js:653:30)
    at Object.Module._extensions..js (module.js:664:10)
    at Module.load (module.js:566:32)
    at tryModuleLoad (module.js:506:12)

The project guild-review is built using yarn and the server sub-workspace is built with the command yarn workspace server build. The final execution of node -r tsconfig-paths/register dist/index.js in the server directory is expected to run without error. However, an error is encountered indicating that the required module @modules/webhooks cannot be found.

This error message shows that the module @modules/webhooks cannot be found by the node runtime system. This error might occur because of several reasons, such as missing module dependencies, incorrect file paths, or missing module exports. The detailed error message includes the call stack information, which might provide some insights into the cause of the error.

The project guild-review is available at the following repository: https://github.com/darkbasic/guild-review.

Troubleshooting with the Lightrun Developer Observability Platform

Getting a sense of what’s actually happening inside a live application is a frustrating experience, one that relies mostly on querying and observing whatever logs were written during development.
Lightrun is a Developer Observability Platform, allowing developers to add telemetry to live applications in real-time, on-demand, and right from the IDE.

  • Instantly add logs to, set metrics in, and take snapshots of live applications
  • Insights delivered straight to your IDE or CLI
  • Works where you do: dev, QA, staging, CI/CD, and production

Start for free today

Problem solution for tsconfig-paths doesn’t work with node (works with ts-node) in dividab tsconfig-paths

The issue you’re encountering is likely due to the way the tsconfig-paths package is used. This package is typically used with the TypeScript compiler (tsc) to provide support for custom path mapping in your tsconfig.json file. However, when you run your code with node, it doesn’t use the TypeScript compiler and therefore doesn’t understand your custom path mappings.

To resolve this issue, you can use the ts-node package instead, which allows you to run TypeScript code directly, including custom path mappings defined in your tsconfig.json file. When you run your code with ts-node, it will use the TypeScript compiler and the tsconfig-paths package to resolve your custom paths.

Here’s how you can run your code with ts-node:

npx ts-node myFile.ts

Alternatively, you can also use the --compiler option with node to use ts-node as the compiler for your TypeScript code:

node --compiler ts-node myFile.ts

This should resolve the issue with the tsconfig-paths package not working with node.

Other popular problems with dividab tsconfig-paths

Problem: tsconfig-paths not working with node

The tsconfig-paths package provides support for custom path mapping in a TypeScript project’s tsconfig.json file. However, when running code with node, it does not use the TypeScript compiler and therefore cannot understand custom path mappings defined in tsconfig.json. This leads to the error “Cannot find module” when trying to import modules using the custom path mappings.

Solution:

To resolve this issue, you can use the ts-node package instead. ts-node allows you to run TypeScript code directly and will use the TypeScript compiler and tsconfig-paths package to resolve custom paths defined in tsconfig.json.

npx ts-node myFile.ts

Alternatively, you can also use the --compiler option with node to use ts-node as the compiler for your TypeScript code:

node --compiler ts-node myFile.ts

Problem: Incorrect path resolution with multiple tsconfig.json files

When a project has multiple tsconfig.json files in different directories, the TypeScript compiler may use the wrong tsconfig.json file when resolving custom paths defined in tsconfig-paths. This can result in incorrect path resolution and “Cannot find module” errors when importing modules.

Solution:

To resolve this issue, you can specify the location of the correct tsconfig.json file using the --project option with the TypeScript compiler. This will ensure that the correct tsconfig.json file is used for path resolution.

tsc --project path/to/tsconfig.json

Problem: “Cannot find module” errors with webpack

When using tsconfig-paths with a bundler such as webpack, you may encounter “Cannot find module” errors when trying to import modules using custom path mappings. This is because webpack does its own module resolution and does not use the TypeScript compiler or tsconfig-paths package.

Solution:

To resolve this issue, you can use the tsconfig-paths-webpack-plugin package, which integrates tsconfig-paths with webpack. The plugin will automatically resolve custom paths defined in tsconfig.json during the webpack build process.

const TsconfigPathsPlugin = require('tsconfig-paths-webpack-plugin');

module.exports = {
  // ...
  resolve: {
    plugins: [
      new TsconfigPathsPlugin({
        configFile: './path/to/tsconfig.json'
      })
    ]
  }
  // ...
};

A brief introduction to dividab tsconfig-paths

tsconfig-paths is a package that provides support for custom path mapping in a TypeScript project’s tsconfig.json file. With tsconfig-paths, developers can define custom paths for their project’s modules, making it easier to manage the structure and organization of large codebases. This helps to avoid having to use relative file paths in imports, which can become messy and hard to maintain.

The tsconfig-paths package works by integrating with the TypeScript compiler (tsc) and using the information defined in the tsconfig.json file to resolve custom paths when importing modules. The custom path mappings are defined in the compilerOptions.paths property of the tsconfig.json file. Once the custom paths have been defined, developers can use the mapped paths in their imports, making it easier to manage the project structure and avoid having to update import statements when file locations change. Additionally, the tsconfig-paths package can be used with other tools, such as webpack, to provide seamless support for custom paths in a project.

Most popular use cases for dividab tsconfig-paths

  1. Simplifying import statements in TypeScript projects

tsconfig-paths can be used to simplify import statements in a TypeScript project by allowing developers to define custom paths for their project’s modules. This helps to avoid having to use relative file paths in imports, which can become messy and hard to maintain, especially in large codebases. The custom path mappings are defined in the compilerOptions.paths property of the tsconfig.json file and can be used in import statements like this:

import { someModule } from '@app/someModule';
  1. Managing the structure and organization of large codebases

tsconfig-paths makes it easier to manage the structure and organization of large codebases by allowing developers to define custom paths for their project’s modules. With custom paths, developers can better organize their code into meaningful directories, and make it easier to change the file structure of the project without having to update every import statement.

  1. Integrating with other tools to provide support for custom paths

tsconfig-paths can be integrated with other tools, such as webpack, to provide support for custom paths in a project. The tsconfig-paths package works by using the information defined in the tsconfig.json file to resolve custom paths when importing modules. By integrating with tools such as webpack, tsconfig-paths helps to provide seamless support for custom paths in a project, making it easier for developers to manage the structure and organization of their code.

Share

It’s Really not that Complicated.

You can actually understand what’s going on inside your live applications.

Try Lightrun’s Playground

Lets Talk!

Looking for more information about Lightrun and debugging?
We’d love to hear from you!
Drop us a line and we’ll get back to you shortly.

By submitting this form, I agree to Lightrun’s Privacy Policy and Terms of Use.