TypeScript Path Alias Resolver
tsconfig-paths is a utility that provides runtime support for TypeScript's path mapping feature, allowing Node.js to resolve modules based on the `paths` configuration in `tsconfig.json`. This addresses the common issue where TypeScript compiles code successfully using path aliases (e.g., `@lib/utils`), but Node.js fails at runtime because it doesn't understand these mappings. The package, currently at version 4.2.0, functions by hooking into Node.js's module resolution system, typically via a `--require` flag (e.g., `node -r tsconfig-paths/register`) or through a programmatic API. While TypeScript handles `paths` during compilation, tsconfig-paths ensures these aliases work in development environments with `ts-node` or directly with compiled JavaScript, making it a critical tool for maintaining clean import paths and structured projects without additional build steps or manual path adjustments for runtime execution. Its release cadence is generally tied to bug fixes and compatibility updates, not a strict schedule.
Common errors
-
Error: Cannot find module '@alias/my-module' from 'path/to/my-file.js'
cause Node.js failed to resolve a module imported with a path alias, indicating tsconfig-paths either isn't loaded or is misconfigured.fixEnsure `tsconfig-paths/register` is correctly loaded via `node -r tsconfig-paths/register` or `ts-node -r tsconfig-paths/register`. Double-check `tsconfig.json` for correct `baseUrl` and `paths` entries, and confirm the alias matches a valid file path. -
TypeError: Cannot read properties of undefined (reading 'compilerOptions')
cause The `tsconfig.json` file could not be found or parsed when using programmatic API, or is missing the `compilerOptions` field.fixVerify that `tsconfig.json` exists in the expected location and is valid JSON. If using the programmatic API, ensure the path to `tsconfig.json` is correct and accessible. -
Error: tsconfig-paths: Could not load tsconfig.json. Config file: undefined
cause The `tsconfig.json` file could not be automatically located by `tsconfig-paths`.fixIf `tsconfig.json` is not in the current working directory, either move it, or use the programmatic API (`register({ baseUrl, paths })`) to explicitly provide the configuration. When using `ts-node`, you can set `process.env.TS_NODE_PROJECT` to point to your `tsconfig.json`.
Warnings
- breaking When using Mocha versions 4.0.0 or higher, the `--compiler` option was deprecated. Instead, you must use `--require` to load `ts-node/register` and `tsconfig-paths/register`, and explicitly specify TypeScript file extensions in your glob pattern.
- gotcha Incorrect `baseUrl` or `paths` configuration in `tsconfig.json` will lead to module resolution failures. `tsconfig-paths` relies directly on these settings, so any mismatch with actual file locations or incorrect wildcard usage will cause 'module not found' errors.
- gotcha The `tsconfig-paths/register` module must be loaded *before* any other modules that rely on path aliases. If your application code is loaded first, Node.js will attempt resolution before tsconfig-paths has a chance to hook into the module system.
Install
-
npm install tsconfig-paths -
yarn add tsconfig-paths -
pnpm add tsconfig-paths
Imports
- register for side-effects
import 'tsconfig-paths/register'
require('tsconfig-paths/register') - register (API)
const register = require('tsconfig-paths').registerimport { register } from 'tsconfig-paths' - createMatchPath
const createMatchPath = require('tsconfig-paths').createMatchPathimport { createMatchPath } from 'tsconfig-paths'
Quickstart
const tsConfig = require('./tsconfig.json');
const { register } = require('tsconfig-paths');
// Example tsconfig.json content:
// {
// "compilerOptions": {
// "baseUrl": ".",
// "paths": {
// "@utils/*": ["src/utils/*"],
// "@config": ["src/config/index.ts"]
// }
// }
// }
// 1. Using the CLI with Node.js:
// To run a JavaScript file compiled from TypeScript with path aliases:
// node -r tsconfig-paths/register main.js
// 2. Using the CLI with ts-node:
// To run a TypeScript file directly with path aliases:
// ts-node -r tsconfig-paths/register main.ts
// 3. Programmatic registration for more control:
// This is useful if your tsconfig.json is not in the CWD or you need custom settings.
const baseUrl = process.env.TS_NODE_BASEURL ?? tsConfig.compilerOptions.baseUrl ?? './';
const cleanup = register({
baseUrl,
paths: tsConfig.compilerOptions.paths
});
console.log('tsconfig-paths registered successfully.');
// Your application logic that uses path aliases would go here.
// For example, if you had '@utils/logger' mapped to 'src/utils/logger.ts'
// import { log } from '@utils/logger';
// log('Application started');
// When path registration is no longer needed (e.g., in a long-running process that might reconfigure)
// cleanup();