New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

codemod-add-import-extensions

Package Overview
Dependencies
Maintainers
0
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

codemod-add-import-extensions

Add file extensions to relative imports in TypeScript.

  • 0.1.2
  • latest
  • npm
  • Socket score

Version published
Maintainers
0
Created
Source

codemod-add-import-extensions

A codemod to automatically add file extensions to relative imports in your TypeScript project.

// Before
import x from "../x";

// After
import x from "../x.ts"; // Or whatever the actual file extension is!

Usage

Run with npx, passing the path to your project's tsconfig.json as an argument:

npx codemod-add-import-extensions --tsconfig tsconfig.json

By default, all TypeScript files will be updated in place. If you'd prefer to see what would change, use the --dry-run flag.

Limitations

  • Only ESM modules are supported, not CommonJS (require("../x")).
  • Only static imports are supported, not dynamic ones (import("../x")).
  • Import aliases are not supported (import x from "@my-alias/x").
  • If there are multiple files that could potentially match, the codemod emits a warning rather than trying to resolve the ambiguity.

Why use explicit file extensions?

  • Performance: Using explicit file extensions makes imports faster. See Speeding up the JavaScript ecosystem – module resolution.
  • Direct execution: Using explicit file extensions allows TypeScript to be run without a compilation step. To learn how, see the TypeScript 5.7 release notes – Path Rewriting for Relative Paths.

Preventing regressions

Once you've added file extensions to all of your relative imports, consider enabling the import/extensions rule from eslint-plugin-import to ensure that they're used consistently going forward.

Keywords

FAQs

Package last updated on 11 Jan 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc