![require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages](https://cdn.sanity.io/images/cgdhsj6q/production/be8ab80c8efa5907bc341c6fefe9aa20d239d890-1600x1097.png?w=400&fit=max&auto=format)
Security News
require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
Rename TypeScript created ESM .js files to .mjs
This tool takes output from tsc
and copies/renames the files.
file.js
=> file.mjs
file.js.map
=> file.mjs.map
file.d.ts
=> file.d.mts
file.d.ts.map
=> file.d.mts.map
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';
This is an example on how to create a package that exports both CommonJS and ESM from TypeScript source.
tsconfig.esm.json
{
"$schema": "https://json.schemastore.org/tsconfig",
"extends": "@tsconfig/node16/tsconfig.json",
"compilerOptions": {
"declaration": true,
"module": "ES2020",
"moduleResolution": "node16",
"outDir": "dist/esm",
"sourceMap": true
},
"include": ["src"]
}
tsconfig.cjs.json
{
"$schema": "https://json.schemastore.org/tsconfig",
"extends": "@tsconfig/node16/tsconfig.json",
"compilerOptions": {
"declaration": true,
"module": "CommonJS",
"moduleResolution": "node16",
"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"
}
}
}
tsc -p tsconfig.cjs.json
tsc -p tsconfig.esm.json
ts2mjs dist/esm
Usage: ts2mjs [options] <files...>
Rename ESM .js files to .mjs
Arguments:
files The files to rename.
Options:
-o, --output <dir> The output directory.
--cwd <dir> The current working directory.
--root <dir> The root directory.
--dry-run Dry Run do not update files.
--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
FAQs
Rename TypeScript Created ESM .js files to .mjs
The npm package ts2mjs receives a total of 700 weekly downloads. As such, ts2mjs popularity was classified as not popular.
We found that ts2mjs demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
Security News
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.