Registry / devops / mts-migrator

mts-migrator

JSON →
library2.1.11jsnpmunverified

mts-migrator is a javascript-to-typescript migration engine that converts CommonJS modules to ESM, infers types from AST analysis, extracts JSDoc annotations, and refines types with the TypeScript compiler using ts-morph. Current stable version is 2.1.11, released under AGPL-3.0. It can be used via `pnpm dlx` or `npx` without installation. Key differentiators: it handles module system conversion (require→import, module.exports→export), deduplicates imports, strips .js/.jsx from relative imports, infers types from literals, constructors, known APIs, arrays, binary ops, and JSDoc, and optionally runs eslint --fix. It backs up original files, supports dry runs, error recovery, migration reports, and skips config files. Does not handle Flow types or fix logic bugs.

npm install mts-migrator
INSTALL
IMPORT
SIG · MTS-MIGRATOR
M
mts-migrator
devopsjavascriptv2.1.11
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

migrateCode
import { migrateCode } from 'mts-migrator'
const migrateCode = require('mts-migrator')
Package is ESM-only. Named export, not default.
migrateCodeSimple
import { migrateCode } from 'mts-migrator'
migrateCode is the main function for quick code conversion.
codeToAST
import { codeToAST } from 'mts-migrator'
import { codeToAst } from 'mts-migrator'
Export name is camelCase: codeToAST, not codeToAst.
convertModuleSystem
import { convertModuleSystem } from 'mts-migrator'
Converts CommonJS modules to ESM in an AST.
injectTypes
import { injectTypes } from 'mts-migrator'
Injects type annotations into an AST.
astToCode
import { astToCode } from 'mts-migrator'
Converts an AST back to code string.

Shows basic usage of the migrateCode function to convert CommonJS to ESM with type inference.

import { migrateCode } from 'mts-migrator'; const jsCode = ` const fs = require('fs'); const { join } = require('path'); module.exports = function readConfig(filePath) { const data = fs.readFileSync(join(__dirname, filePath), 'utf8'); return JSON.parse(data); }; `; const result = migrateCode(jsCode); console.log(result.code); // Output: // import fs from 'fs'; // import { join } from 'path'; // // export function readConfig(filePath: string): any { // const data = fs.readFileSync(join(__dirname, filePath), 'utf8'); // return JSON.parse(data); // }
Debug
Known issues
gotchaThe migrateCode function returns an object with code and errors; errors are non-fatal but should be inspected.
fix
Check result.errors for migration warnings or issues.
affects: >=2.0
gotchaType refinement using ts-morph requires TypeScript to be installed; otherwise, any annotations may remain.
fix
Install typescript as a devDependency: pnpm add -D typescript
affects: >=2.0
gotchaThe CLI command is 'mts' not 'mts-migrator'; running without arguments prompts to migrate all folders in current directory.
fix
Use 'pnpm mts' or 'npx mts-migrator' but note that the binary name is 'mts'.
affects: >=2.0
gotchaOriginal files are backed up before migration; backup directory is created alongside the original files.
fix
To restore, copy files from .mts-backups or similar (check documentation for exact backup folder name).
affects: >=2.0
deprecatedNo breaking changes identified in current version; API is stable.
affects: >=2.1
Errors
Common errors & fixes
Error [ERR_REQUIRE_ESM]: require() of ES Module not supported
Using CommonJS require() to import an ESM-only package.
fix
Use import syntax or switch to ESM in your project (add 'type': 'module' to package.json).
TypeError: migrateCode is not a function
Package is ESM-only; using require() returns the module object, not the named export.
fix
Use dynamic import: const { migrateCode } = await import('mts-migrator'); or use ESM import syntax.
Cannot find module 'mts-migrator'
Package is not installed or not in node_modules.
fix
Install it: pnpm add mts-migrator or npm install mts-migrator
Type 'string' is not assignable to type 'number'
Type inference may produce incorrect types due to dynamic code; manual annotation needed.
fix
Add explicit type annotations where inference fails.
Upgrade
Version history
2.1.11latest on npm
Audit
Dependencies
typescriptoptionalts-morph uses the TypeScript compiler for type refinement; TypeScript must be installed in the project for full functionality.
@ts-morph/bootstraprequiredUsed for programmatic type refinement with the TypeScript compiler API.
@babel/parserrequiredParses JavaScript code into AST for analysis and transformation.
@babel/traverserequiredTraverses and transforms the AST during migration.
eslintoptionalRuns eslint --fix on migrated files if available in the project.
Agent activity
4 hits · last 30 days
node
4
Resources
mts-migrator — npm install mts-migrator · libregistry