Registry / devops / typescript-transform-paths

typescript-transform-paths

JSON →
library3.5.6jsnpmunverified

typescript-transform-paths is a TypeScript compiler plugin that transforms module resolution paths defined in `tsconfig.json`'s `paths` or `rootDirs` options into correct relative paths within the compiled JavaScript and declaration files. It addresses a common pain point where TypeScript's path aliases are not natively resolved by Node.js or bundlers without additional configuration. The current stable version is 3.5.6, with a significant v4.0.0-beta release that transitions to ESM-only. The package sees regular maintenance releases and active development, with a beta channel for upcoming major changes. Its key differentiators include comprehensive support for both `.js` and `.d.ts` file transformations, integration with various build tools like `ts-patch`, `ts-node`, `node -r`, and NX, and the ability to define exclusion patterns for granular control.

npm install typescript-transform-paths
INSTALL
IMPORT
SIG · TYPESCRIPT-TRANSFO
T
typescript-transform-paths
devopsjavascriptv3.5.6
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

transformer
{ "transform": "typescript-transform-paths" }
import * as transform from 'typescript-transform-paths'
This package is a TypeScript compiler plugin, configured directly in `tsconfig.json`'s `plugins` array. It is not imported as a runtime module for direct use in application code, but rather invoked by the TypeScript compiler pipeline or build tools like `ts-patch`.
afterDeclarations transformer
{ "transform": "typescript-transform-paths", "afterDeclarations": true }
{ "transform": "typescript-transform-paths/afterDeclarations" }
To transform paths in `.d.ts` files, a separate plugin entry with `"afterDeclarations": true` is required in `tsconfig.json`. This is critical for ensuring type declarations correctly reflect the transformed module paths, especially when publishing libraries.
register
node -r typescript-transform-paths/register your-app.js
require('typescript-transform-paths/register')
For runtime resolution with `node` or `ts-node`, use the provided register hook. In `ts-node` configuration, add `"typescript-transform-paths/register"` to the `require` array. This is a side-effect import that modifies module resolution globally.
nx-transformer
{ "name": "typescript-transform-paths/nx-transformer" }
When integrating with NX, specify the transformer name directly in the project's `project.json` build targets. This transformer is specifically designed for the NX build pipeline and allows for `afterDeclarations` options.

Demonstrates how typescript-transform-paths conceptually integrates with the TypeScript compilation process, showing a basic setup for transforming both `.js` and `.d.ts` files based on `tsconfig.json` paths.

import * as fs from 'fs'; import * as path from 'path'; import * as ts from 'typescript'; // Minimal ts-patch setup for demonstration const tsconfigPath = path.resolve('./tsconfig.json'); const tsconfigContent = fs.readFileSync(tsconfigPath, 'utf8'); const tsconfig = ts.parseConfigFileTextToJson(tsconfigPath, tsconfigContent).config; // Simulate the ts-patch application of the transformer // In a real setup, ts-patch handles this with its CLI tool. const program = ts.createProgram({ rootNames: [path.resolve('./src/index.ts')], options: { ...tsconfig.compilerOptions, noEmit: false, // Ensure output for demonstration outDir: './dist-quickstart' // Output to a specific directory }, }); const emitResult = program.emit(undefined, undefined, undefined, undefined, [ { before: (ctx) => (sourceFile) => { // This represents the 'typescript-transform-paths' plugin // In real scenarios, this is loaded dynamically by ts-patch const ttPaths = require('typescript-transform-paths'); const transformer = ttPaths.default(program, tsconfig.compilerOptions, {}, ts, path, fs)(ctx); return transformer(sourceFile); }, afterDeclarations: (ctx) => (sourceFile) => { const ttPaths = require('typescript-transform-paths'); const transformer = ttPaths.default(program, tsconfig.compilerOptions, { afterDeclarations: true }, ts, path, fs)(ctx); return transformer(sourceFile); } } ]); if (emitResult.emitSkipped) { console.log('Compilation failed or skipped.'); } else { console.log('Compilation successful. Check ./dist-quickstart for output.'); } /* To run this: 1. Create tsconfig.json: { "compilerOptions": { "baseUrl": ".", "paths": { "@utils/*": ["./src/utils/*"] }, "outDir": "./dist", "declaration": true, "target": "es2018", "module": "commonjs", "strict": true, "esModuleInterop": true, "forceConsistentCasingInFileNames": true, "plugins": [ { "transform": "typescript-transform-paths" }, { "transform": "typescript-transform-paths", "afterDeclarations": true } ] }, "include": ["src/**/*.ts"] } 2. Create src/utils/sum.ts: export function sum(a: number, b: number): number { return a + b; } 3. Create src/index.ts: import { sum } from "@utils/sum"; console.log(sum(1, 2)); 4. Install deps: npm i -D typescript typescript-transform-paths 5. Run with `npx ts-node your-script.ts` (this script simulates the plugin, not directly using ts-patch CLI) (For actual usage, you'd run `npx ts-patch tsc` after `ts-patch install`) */
Debug
Known issues
breakingVersion 4.0.0-beta introduces an ESM-only distribution. Projects targeting CommonJS modules will need to stick to version 3.x or migrate their build system to ESM.
fix
For CommonJS projects, remain on `typescript-transform-paths@3.x`. For new projects or migration, ensure your `tsconfig.json` sets `"module": "esnext"` or similar, and update your build tools to handle ESM.
affects: >=4.0.0-beta
gotchaTo ensure path transformations apply to both compiled JavaScript (`.js`) files and their corresponding TypeScript declaration (`.d.ts`) files, you must include two separate plugin entries in your `tsconfig.json`.
fix
Add both `{"transform": "typescript-transform-paths"}` and `{"transform": "typescript-transform-paths", "afterDeclarations": true}` to your `compilerOptions.plugins` array.
affects: >=1.0.0
gotchaWhen using `typescript-transform-paths` with `ts-node`, it's important to configure `ts-node` to `require` the transformation module. Simply having the plugin in `tsconfig.json` does not automatically enable it for `ts-node`'s runtime compilation.
fix
In your `tsconfig.json`, within the `ts-node` configuration block, add `"require": ["typescript-transform-paths/register"]`.
affects: >=1.0.0
gotchaThe plugin needs to be applied by a TypeScript transformer runner. Installing the package alone is not enough; you must integrate it with a tool like `ts-patch` for `tsc` compilation or use its `register` script for runtime.
fix
For `tsc` compilation, install and use `ts-patch` (`ts-patch install` then `ts-patch tsc`). For runtime, use `node -r typescript-transform-paths/register` or configure `ts-node` accordingly.
affects: >=1.0.0
gotchaOlder TypeScript versions (e.g., prior to 3.6.5, though the peer dep is now >=3.6.5) or specific patch versions might lead to compatibility issues, as observed with `minimatch` dependency changes and TypeScript 5.6 compatibility fixes.
fix
Always ensure your `typescript` peer dependency meets the specified requirements and consider updating `typescript-transform-paths` to the latest stable version to benefit from compatibility fixes.
affects: <3.5.2
Errors
Common errors & fixes
TypeError: (0 , typescript_transform_paths_1.default) is not a function
Attempting to `import` or `require` the plugin directly in application code, or an incorrect setup with `ts-patch` or `ts-node`.
fix
This package is a compiler plugin, not a runtime library for direct import. Ensure it's correctly configured in `tsconfig.json` `plugins` for compilation (with `ts-patch`) or used via its `register` script for runtime (`node -r` or `ts-node` config).
Error: Expected version to be parsed
This error occurs in specific scenarios where the installed TypeScript version string contains a '0' in an unexpected position, causing issues with internal version parsing logic.
fix
This was fixed in v3.5.5. Update `typescript-transform-paths` to version 3.5.5 or newer to resolve this parsing issue.
Module not found: Error: Can't resolve '@utils/sum' in '...' (or similar path alias error after build)
The TypeScript `paths` configuration was not transformed during compilation, or the runtime environment is not resolving the aliased paths.
fix
Verify that `typescript-transform-paths` is correctly configured in your `tsconfig.json` plugins for both `.js` and `.d.ts` files. If compiling with `tsc`, ensure `ts-patch install` and `ts-patch tsc` are used. If at runtime, make sure `typescript-transform-paths/register` is active (e.g., `node -r` or `ts-node` config).
Upgrade
Version history
3.5.6latest on npm
Audit
Dependencies
typescriptrequiredPeer dependency, required for compilation and type checking functionalities. Version >=3.6.5 is explicitly stated.
Agent activity
5 hits · last 30 days
node
4
Resources
typescript-transform-paths — npm install typescript-transform-paths · libregistry