A Babel plugin that transforms specified require
path file extensions.
This is useful for building dual ESM/CJS packages. File names with extensions are mandatory in ESM import
specifiers in .mjs
files. After transpiling this to CJS in .js
files, the hardcoded .mjs
extensions in the require
paths must be transformed to .js
. Alternatively, sometimes it’s safe to remove the extensions as Node.js in CJS mode can automatically resolve .js
file extensions.
To install from npm run:
npm install babel-plugin-transform-require-extensions --save-dev
Configure Babel to use the plugin:
{ "plugins": ["transform-require-extensions"] }
This plugin must run after any ESM to CJS transpilation occurs.
By default .mjs
extensions are transformed to .js
. The extensions
option can be used to specify extension replacements. For example, to remove .mjs
extensions:
{
"plugins": [
[
"transform-require-extensions",
{
"extensions": {
".mjs": ""
}
}
]
]
}
- Node.js v10+