Rename TypeScript created ESM .js files to .mjs
This tool takes output from tsc and copies/renames the files.
file.js=>file.mjsfile.js.map=>file.mjs.mapfile.d.ts=>file.d.mtsfile.d.ts.map=>file.d.mts.mapfile.ts=>file.mts(by default, can be skipped with--skip-ts)
Use the --cjs flag to generate CommonJS extensions instead:
file.js=>file.cjsfile.js.map=>file.cjs.mapfile.d.ts=>file.d.ctsfile.d.ts.map=>file.d.cts.mapfile.ts=>file.cts(by default, can be skipped with--skip-ts)
Automatically updates relative imports and exports in your files:
dist/code.js -> dist/code.mjs
import * as path from 'path';
import { lib } from 'package/lib/index.js'
-import { findFiles } from './findFiles.js';
+import { findFiles } from './findFiles.mjs';dist/index.d.ts -> dist/index.d.mts
-export { PrimeNumber, Tuple, GUID, Address, Person, Annotation, } from './types.js';
+export { PrimeNumber, Tuple, GUID, Address, Person, Annotation, } from './types.mjs';
-export { lookUpPerson } from './lookup.js';
+export { lookUpPerson } from './lookup.mjs';Use --remove-source to automatically delete original files after successful conversion:
ts2mjs dist/esm --remove-source
# Original .js files are deleted after being converted to .mjsUse --skip-ts to ignore TypeScript files instead of converting them:
ts2mjs dist/esm --skip-ts
# .ts and .d.ts files are left unchanged, only .js files are processed--cjs- Generate.cjsextensions instead of.mjs--remove-source- Remove original files after successful conversion--skip-ts- Skip all TypeScript files (.tsand.d.ts) instead of converting them--dry-run- Preview changes without making them--verbose- Show detailed output-o, --output <dir>- Specify output directory (default: in-place)--root <dir>- Set the root directory for processing
This is an example on how to create a package that exports both CommonJS and ESM from TypeScript source.
tsconfig.esm.json
tsconfig.cjs.json
{
"$schema": "https://json.schemastore.org/tsconfig",
"extends": "@tsconfig/node20/tsconfig.json",
"compilerOptions": {
"declaration": true,
"module": "NodeNext",
"moduleResolution": "NodeNext",
"outDir": "dist/cjs",
"sourceMap": true
},
"include": ["src"]
}package.json
{
"type": "commonjs",
"main": "dist/csj/index.js",
"module": "dist/esm/index.mjs",
"types": "dist/cjs/index.d.ts",
"exports": {
".": {
"import": "./dist/esm/index.mjs",
"require": "./dist/cjs/index.js"
}
}
}# Build both CommonJS and ESM
tsc -p tsconfig.cjs.json
tsc -p tsconfig.esm.json
# Convert ESM output to .mjs
ts2mjs dist/esm
# Convert CJS TypeScript files to .cts (if needed)
ts2mjs dist/cjs --cjs
# Advanced usage examples
ts2mjs dist/esm --remove-source # Delete original files after conversion
ts2mjs dist/esm --skip-ts # Only process .js files, skip .ts/.d.ts
ts2mjs dist/esm --dry-run --verbose # Preview changes with detailed outputUsage: ts2mjs [options] <files...>
Rename ESM .js files to .mjs (or .cjs if --cjs options is used)
Arguments:
files Globs matching files to rename.
Options:
--cjs Use .cjs extension instead of .mjs extension.
-o, --output <dir> The output directory.
--cwd <dir> The current working directory.
--root <dir> The root directory.
-x,--exclude <glob> Exclude a glob from the files. (default: [])
--dry-run Dry Run do not update files.
--remove-source Remove the original files after successful conversion.
--skip-ts Skip all TypeScript files (.ts and .d.ts) instead of
converting them.
--no-must-find-files No error if files are not found.
--no-enforce-root Do not fail if relative `.js` files outside of the root
are imported.
--color Force color.
--no-color Do not use color.
-v, --verbose Verbose mode
-V, --version output the version number
-h, --help display help for command
Brought to you by
Street Side Software
{ "$schema": "https://json.schemastore.org/tsconfig", "extends": "@tsconfig/node20/tsconfig.json", "compilerOptions": { "declaration": true, "module": "ES2022", "moduleResolution": "node", "outDir": "dist/esm", "sourceMap": true }, "include": ["src"] }