J

correct-ts-specifiers

How to Use
Run this codemod on your codebase using one of the following commands

The easiest way to run this codemod without installing anything globally:

Documentation

Correct TypeScript Specifiers

This package transforms import specifiers from the old tsc (TypeScript's compiler) requirement of using .js file extensions in source-code to import files that are actually typescript; the corrected specifiers enable source-code to be runnable by standards-compliant software like Node.js.

This is a one-and-done process, and the updated source-code should be committed to your version control (eg git); thereafter, source-code import statements should be authored compliant with the ECMAScript (JavaScript) standard.

Tip
Those using to compile will need to enable ; using for only type-checking (ex via a lint/test step like ) needs (and some additional compile options—see the cited documentation);

This package does not just blindly find & replace file extensions within specifiers: It confirms that the replacement specifier actually exists; in ambiguous cases (such as two files with the same basename in the same location but different relevant file extensions like /tmp/foo.js and /tmp/foo.ts), it logs an error, skips that specifier, and continues processing.

Caution
This package does not confirm that imported modules contain the desired export(s). This actually ever result in a problem because ambiguous cases are skipped (so if there is a problem, it existed before the migration started). Merely running your source-code after the mirgration completes will confirm all is well (if there are problems, node will error, citing the problems).
Tip
Node.js requires the keyword be present on type imports. For own code, this package usually handles that. However, in some cases and for node modules, it does not. Robust tooling already exists that will automatically fix this, such as and . If your source code needs that, first run this codemod and then one of those fixers.

Running

Caution
This will change your source-code. Commit any unsaved changes before running this package.
sh

Monorepos

For best results, run this within each workspace of the monorepo.

text

Supported cases

  • no file extension → .cts, .mts, .js, .ts, .d.cts, .d.mts, or .d.ts
  • .cjs.cts, .mjs.mts, .js.ts
  • .js.d.cts, .d.mts, or .d.ts
  • Package.json subimports
  • tsconfig paths (via @nodejs-loaders/alias)
    • In order to subsequently run code via node, you will need to add this (or another) loader to your own project. Or, switch to subimports.
  • Commonjs-like directory specifiers

Before:

ts

After:

ts

Ready to contribute?

Build your own codemod and share it with the community.