npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

replace-imports-cli

v1.0.5

Published

A CLI tool to replace long relative import paths with tsconfig aliases in TypeScript projects.

Readme

Replace Imports CLI

A lightweight CLI tool to refactor and optimize ES6 imports by replacing long relative paths with aliases defined in tsconfig.json file. It supports configurable file extensions, processes only the specified files, and allows for flexibility with simple-to-use prompts.


Features

  • Replace Long Relative Imports: Automatically replace long relative import paths (e.g., ../../../services/auth.service) with aliases (e.g., @services/auth.service).
  • Custom File Extensions: Specify the file extensions you want to process (default: .js, .ts).
  • Flexible Import Handling:
    • Skip imports already using aliases.
    • Replace imports only outside alias folders.
  • Interactive CLI: Simple prompts to configure paths, extensions, and processing behavior.
  • Framework-Agnostic: Works with any TypeScript project that uses tsconfig.json.

Installation

Prerequisites

Clone and Setup

git clone [email protected]:jzolnowski/replace-imports-cli.git
cd replace-imports-cli
npm install

Install and Use the Package

After publishing, users can install your CLI tool globally or locally:

  • Global Installation:

    npm install -g replace-imports-cli

    Run the tool:

    replace-imports
  • Local Installation:

    npm install --save-dev replace-imports-cli

    Use it in a project:

    npx replace-imports

Example

Developer Workflow:

  1. Initialize a project with aliases in tsconfig.json e.g.
    {
      "compilerOptions": {
        "baseUrl": "./src",
          "paths": {
            "@services/*": ["app/services/*"],
            "@components/*": ["app/components/*"]
          }
      }
    }
  2. Install replace-imports-cli package and run it - check the details in Install and Use the Package section.

CLI Output:

Welcome to Replace Imports CLI! 🚀

? Enter the path to your tsconfig.json: ./tsconfig.json
? Enter the path to your project source directory (e.g., ./src): ./src
? Enter file extensions to process (comma-separated, e.g., .ts,.js): .ts,.js

Processing files in ./src using aliases from ./tsconfig.json with extensions: .ts, .js...

Updated imports in src/app/services/auth.service.ts
Updated imports in src/app/components/header.component.js

All imports updated successfully! 🎉

Before Running the Script

In src/app/modules/user.module.ts:

import { AuthService } from '../../../services/auth.service';
import { UserComponent } from '../../components/user.component';

After Running the Script

import { AuthService } from '@services/auth.service';
import { UserComponent } from '@components/user.component';

How It Works

  1. Reads tsconfig.json: Parses the paths section to extract aliases.
  2. Scans the Source Directory: Recursively searches for files matching the specified extensions.
  3. Replaces Imports:
  • Checks each import statement to determine if it matches the alias mappings.
  • Replaces relative paths with the appropriate alias if applicable.
  1. Writes Changes: Updates the files with optimized imports.

Configuration

Default Values

The following values are used by default if not explicitly specified during the CLI prompts:

| Setting | Default Value | |------------------------|---------------------| | tsconfig.json Path | ./tsconfig.json | | Source Directory | ./src | | File Extensions | .ts, .js |


Folder Structure

replace-imports-cli/
├── src/
│   ├── constants.js         # Defines reusable constants, like default extensions and error messages.
│   ├── file-utils.js        # Contains utility functions to scan directories and retrieve files based on extensions.
│   ├── replace-imports.js   # Handles the logic for replacing relative imports with tsconfig aliases.
│   ├── tsconfig-utils.js    # Parses tsconfig.json and extracts alias paths for use in replacements.
│   ├── cli.js               # Contains the interactive CLI logic and user prompts.
│   └── index.js             # Orchestrates the replacement process by tying together other modules.
├── bin/
│   └── replace-imports.js   # Entry point for the CLI; calls `runCLI` from `src/cli.js`.
├── package.json             # Defines the npm package configuration, dependencies, and scripts.
├── README.md                # Documentation for the project, including usage and examples.
├── .editorconfig            # Ensures consistent coding styles across editors.
├── .gitignore               # Specifies files and directories to ignore in Git commits.
└── LICENSE                  # Open-source license for the project.

Contributing

  1. Fork the repository.
  2. Create a new branch for your feature or bug fix.
  3. Submit a pull request with a detailed explanation of your changes.

License

This project is licensed under the MIT License. See the LICENSE file for more details.


Troubleshooting

Common Errors

Error: Missing "paths" property in compilerOptions of tsconfig.json

  • Ensure your tsconfig.json includes a valid paths mapping under compilerOptions.

Error: Source directory not found.

  • Verify the directory path you provided exists and contains the source files.

Error: Could not parse tsconfig.json. Ensure the file is valid.

  • Check for syntax errors or invalid JSON in your tsconfig.json.

Future Features

  • Dry Run Mode: Preview changes before applying them.
  • Custom Output Directory: Specify a directory to save updated files.
  • Nx support: Add support for Nx workspace folder structures that use multiple tsconfig files.

Feel free to suggest features or report issues by opening an issue on GitHub! 🎉