@pkerschbaum/codemod-rewrite-module-specifiers-to-full-paths
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

@pkerschbaum/codemod-rewrite-module-specifiers-to-full-paths

Codemod to rewrite module specifiers of TypeScript files to their full paths.

Problem

Module specifiers for relative, absolute, and "subpath" (=path alias) imports in Node.js ESM need to be "exact".
This is in contrast to Node.js CJS modules, which applied a "searching" algorithm when loading modules (see also https://nodejs.org/api/modules.html#file-modules).

For example, while the following require function calls were working in Node.js CJS modules, they are not working anymore in ESM modules:

/* assuming ./some-directory/index.js is present */
require('./some-directory');
require('./some-directory/index');

Therefore, migrating CJS code to ESM code involves rewriting every module specifier to its full path.
Doing this by hand is tedious and error-prone.

Solution

This package is a codemod which consumes a TypeScript configuration file and rewrites all relative, absolute, and path alias imports to their full paths.

How to use

npx @pkerschbaum/codemod-rewrite-module-specifiers-to-full-paths --help

# Usage: codemod [options]

# Options:
#   --project <path-to-tsconfig-json>  Path to the TypeScript configuration file (e.g. "./tsconfig.json").
#   --basepath <path>                  A root directory to resolve relative path entries in the TypeScript config file to (e.g. option "outDir"). If omitted, the directory of the
#                                      TypeScript configuration file passed with "--project" is used.
#   -h, --help                         display help for command

# for example
npx @pkerschbaum/codemod-rewrite-module-specifiers-to-full-paths --project=./path/to/tsconfig.json

Readme

Keywords

none

Package Sidebar

Install

npm i @pkerschbaum/codemod-rewrite-module-specifiers-to-full-paths

Weekly Downloads

1

Version

1.0.1

License

MIT

Unpacked Size

48.6 kB

Total Files

38

Last publish

Collaborators

  • pkerschbaum