<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Vedhashree Sampath</title>
    <description>The latest articles on DEV Community by Vedhashree Sampath (@svedhashree).</description>
    <link>https://dev.to/svedhashree</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1752769%2F6ce2b32b-724f-45b5-a43c-e488e416f9b0.png</url>
      <title>DEV Community: Vedhashree Sampath</title>
      <link>https://dev.to/svedhashree</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/svedhashree"/>
    <language>en</language>
    <item>
      <title>🚀 Simplifying imports by TypeScript Path Aliases with `ts-path-alias-fixer`</title>
      <dc:creator>Vedhashree Sampath</dc:creator>
      <pubDate>Sat, 20 Sep 2025 15:06:11 +0000</pubDate>
      <link>https://dev.to/svedhashree/simplifying-imports-by-typescript-path-aliases-with-ts-path-alias-fixer-cm</link>
      <guid>https://dev.to/svedhashree/simplifying-imports-by-typescript-path-aliases-with-ts-path-alias-fixer-cm</guid>
      <description>&lt;p&gt;If you’ve worked on a TypeScript project of any size, you’ve probably found yourself tangled in a web of relative import hell:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;UserService&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;../../../services/user&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It’s not just ugly — it’s hard to manage, especially when the project grows. Luckily, TypeScript Path Aliases come to the rescue. And even better, the &lt;a href="https://www.npmjs.com/package/ts-path-alias-fixer" rel="noopener noreferrer"&gt;ts-path-alias-fixer&lt;/a&gt; npm package can help you automatically migrate your entire codebase from relative imports to clean, manageable alias paths.&lt;/p&gt;

&lt;p&gt;In this post, we’ll break down:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What path aliases are&lt;/li&gt;
&lt;li&gt;How to set them up in TypeScript&lt;/li&gt;
&lt;li&gt;How to use &lt;a href="https://www.npmjs.com/package/ts-path-alias-fixer" rel="noopener noreferrer"&gt;ts-path-alias-fixer&lt;/a&gt; to refactor existing code&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  📌 What Are TypeScript Path Aliases?
&lt;/h2&gt;

&lt;p&gt;Path aliases let you define custom names for directories in your project. So instead of this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;ProductService&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;../../../../core/services/ProductService&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can do this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;ProductService&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@core/services/ProductService&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Much cleaner, right?&lt;/p&gt;

&lt;h2&gt;
  
  
  🔧 Setting Up Path Aliases in TypeScript
&lt;/h2&gt;

&lt;p&gt;To define path aliases, you need to tweak your &lt;code&gt;tsconfig.json&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "compilerOptions": {
    "baseUrl": "./src",
    "paths": {
      "@components/*": ["components/*"],
      "@utils/*": ["utils/*"],
      "@core/*": ["core/*"]
    }
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Breakdown:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;baseUrl&lt;/code&gt;: The root directory for all your imports (usually &lt;code&gt;src&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;paths&lt;/code&gt;: An object where keys are aliases and values are actual paths relative to &lt;code&gt;baseUrl&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Make sure your tooling (Webpack, Jest, etc.) also understands these aliases. That’s outside this post’s scope, but worth noting.&lt;/p&gt;

&lt;h2&gt;
  
  
  🤖 Meet ts-path-alias-fixer: Your Import Refactoring Bot
&lt;/h2&gt;

&lt;p&gt;Refactoring all those &lt;code&gt;../../../&lt;/code&gt; imports manually? No thanks.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.npmjs.com/package/ts-path-alias-fixer" rel="noopener noreferrer"&gt;ts-path-alias-fixer&lt;/a&gt; is a CLI tool that scans your project and replaces relative imports with your configured path aliases — in seconds.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;✅Key Benefits:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;No manual work&lt;/li&gt;
&lt;li&gt;Preview changes using--dry-run option without modifying files&lt;/li&gt;
&lt;li&gt;Fast and safe&lt;/li&gt;
&lt;li&gt;Works recursively across folders&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  ⚙️ Installing and Using &lt;code&gt;ts-path-alias-fixer&lt;/code&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Step 1: Install the package
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;Global Install&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install -g ts-path-alias-fixer
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Local Install (recommended)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Install as a development dependency in your project:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;with npm:&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install --save-dev ts-path-alias-fixer
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;or with Yarn:&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;yarn add --dev ts-path-alias-fixer
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 2: Define Path Aliases in &lt;code&gt;tsconfig.json&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Make sure your &lt;code&gt;tsconfig.json&lt;/code&gt; includes &lt;code&gt;baseUrl&lt;/code&gt; and &lt;code&gt;paths&lt;/code&gt;, like we saw earlier.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "@/*": ["src/*"],
      "@components/*": ["src/lib/apiClient/components/*"],
      "@services/*": ["src/lib/apiClient/services/*"],
      "@utils/*": ["src/utils/*"]
    }
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;or&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "compilerOptions": {
    "baseUrl": "./src",
    "paths": {
      "@services/*": ["services/*"],
      "@models/*": ["models/*"]
    }
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 3: Run the Fixer
&lt;/h3&gt;

&lt;p&gt;From your project root, run with or without optional cli flags as described below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx fix-ts-imports
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It will scan all &lt;code&gt;.ts&lt;/code&gt; and &lt;code&gt;.tsx&lt;/code&gt; files, detect relative imports, and replace them with the corresponding path aliases based on your &lt;code&gt;tsconfig.json&lt;/code&gt; settings.&lt;/p&gt;

&lt;h2&gt;
  
  
  🛠️ Optional CLI Flags
&lt;/h2&gt;

&lt;p&gt;Some useful options you can use:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;--dir&lt;/code&gt;: Specify your project root directory e.g &lt;code&gt;./&lt;/code&gt;&lt;br&gt;
&lt;code&gt;--alias&lt;/code&gt; : Specify the alias to replace relative paths with e.g &lt;code&gt;@&lt;/code&gt;&lt;br&gt;
&lt;code&gt;--base&lt;/code&gt;: Base folder path inside the project to replace e.g &lt;code&gt;src&lt;/code&gt;&lt;br&gt;
&lt;code&gt;--dry-run&lt;/code&gt;: Preview changes without modifying files&lt;/p&gt;

&lt;p&gt;Example:&lt;/p&gt;

&lt;p&gt;If you just want to change all the files with relative imports as &lt;code&gt;../..&lt;/code&gt; to be replaced with custom alias with the base directory like &lt;code&gt;@/&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;to convert the following import line&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { ProductService } from '../../../../core/services/ProductService';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;to :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { ProductService } from '@/core/services/ProductService';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can do this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx fix-ts-imports
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;or&lt;/p&gt;

&lt;p&gt;with optional args as&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx fix-ts-imports --dir ./ --alias @ --base src
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note: Here &lt;code&gt;src&lt;/code&gt; is the dir path given as &lt;code&gt;--base&lt;/code&gt; path for alias to be used that is defined as paths in &lt;code&gt;tsconfig.json&lt;/code&gt; file as given in the example for &lt;code&gt;@/*&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
...
"baseUrl": ".",
    "paths": {
      "@/*": ["src/*"],
      }
...
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;or&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
...,
"baseUrl": "./src",
    "paths": {
      "@/*": ["/*"],
...,
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  🧪 Before &amp;amp; After Example
&lt;/h2&gt;

&lt;p&gt;Example &lt;code&gt;tsconfig.json&lt;/code&gt; file in your project&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "@/*": ["src/*"],
      "@components/*": ["src/lib/apiClient/components/*"],
      "@services/*": ["src/lib/apiClient/services/*"],
      "@utils/*": ["src/utils/*"]
    },
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Want to modify just all relative path imports like &lt;code&gt;../..&lt;/code&gt; in your project directory to a common base custom alias path like &lt;code&gt;@/&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Before:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { ApiService } from '../../../services/ApiService';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After running &lt;code&gt;ts-path-alias-fixer&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;Run the below from your command line&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx fix-ts-imports --dir ./ --alias @ --base src
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and your files will get converted to&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { ApiService } from '@/services/ApiService';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Want to modify all project files with all relative path imports related to services like &lt;code&gt;../../src/lib/apiClient/services/service-1&lt;/code&gt; in your project directory to a custom directory alias path like &lt;code&gt;@services/&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Before:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { ApiService } from '../../../../services/ApiService';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After running ts-path-alias-fixer:&lt;br&gt;
Run the below from your command line&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx fix-ts-imports --dir ./ --alias @services --base src/lib/apiClient/services
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and your files will get converted to&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { ApiService } from '@services/ApiService';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Want to modify again now, with all project files with all relative path imports related to components like &lt;code&gt;../../src/lib/apiClient/components/component-1&lt;/code&gt; in your project directory to a custom directory alias path like &lt;code&gt;@components/&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Before:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Button} from '../../../../components/buttonComponent';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After running ts-path-alias-fixer:&lt;br&gt;
Run the below from your command line&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx fix-ts-imports --dir ./ --alias @components --base src/lib/apiClient/components
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and your files will get converted to&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Button} from '@components/buttonComponent';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Use this tool like as many times as you like and in minutes enjoy a much cleaner and readable code.&lt;/p&gt;

&lt;h2&gt;
  
  
  Run with dry-run option:
&lt;/h2&gt;

&lt;p&gt;In your project, before modifying your existing files by using this tool, if you want to check and understand what files will get affected&lt;/p&gt;

&lt;h4&gt;
  
  
  Preview changes without modifying files:
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx fix-ts-imports --dry-run
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Run with custom options:&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx fix-ts-imports --dir ./src --alias @ --base src --dry-run
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Example Output&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Once you run the cli command as above with &lt;code&gt;dry-run&lt;/code&gt;, You should see console logs indicating how many files would be modified, along with their file paths, like below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;🔍 Dry run: 3 file(s) would be modified.
 - src\index.ts
 - src\utils\helper.ts
 - src\api\v1\index.ts
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After you are happy with the &lt;code&gt;--dry-run&lt;/code&gt; option output, run the cli command without &lt;code&gt;--dry-run&lt;/code&gt; option to modify the files.&lt;/p&gt;

&lt;h2&gt;
  
  
  🛡️ Tips &amp;amp; Best Practices
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Always commit your code before running the tool — just in case&lt;/li&gt;
&lt;li&gt;Use the &lt;code&gt;--dry-run&lt;/code&gt; option to preview changes&lt;/li&gt;
&lt;li&gt;Add path aliases early in a project to avoid future refactors&lt;/li&gt;
&lt;li&gt;Combine with ESLint rules to enforce alias usage in teams&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  🚀 Wrapping Up
&lt;/h2&gt;

&lt;p&gt;Using TypeScript Path Aliases makes your codebase cleaner, more maintainable, and scalable. With the power of &lt;code&gt;ts-path-alias-fixer&lt;/code&gt;, you can automate the migration and focus on writing code instead of counting &lt;code&gt;../../&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;So go ahead — install it, alias your paths, and thank yourself later. 🧠&lt;/p&gt;

&lt;h2&gt;
  
  
  💬 Got Questions or Tips?
&lt;/h2&gt;

&lt;p&gt;Drop them in the comments! And if this helped you, consider sharing it with your team or giving the repo a ⭐️ on &lt;a href="https://github.com/vedhashrees/ts-path-alias-fixer" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Original Content link :  "&lt;a href="https://medium.com/@shreevedhas/mastering-typescript-path-aliases-with-ts-path-alias-fixer-be3184778e7b" rel="noopener noreferrer"&gt;https://medium.com/@shreevedhas/mastering-typescript-path-aliases-with-ts-path-alias-fixer-be3184778e7b&lt;/a&gt;"&lt;/p&gt;

&lt;p&gt;Happy coding! 👨‍💻👩‍💻&lt;/p&gt;

</description>
      <category>typescript</category>
      <category>relativepathimports</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
