rollup-plugin-esnext-to-nodenext
v1.0.1
Published
A Rollup plugin that transforms ESM imports to Node.js-compatible NodeNext format by adding explicit file extensions
Maintainers
Readme
A Rollup plugin that transforms ESM imports to Node.js-compatible NodeNext format by adding explicit file extensions.
Features
- Automatically adds
.jsextensions to relative imports - Supports TypeScript and JavaScript files
- Works with Rollup's output directory structure
- Verbose logging option for debugging
Installation
npm install rollup-plugin-esnext-to-nodenext --save-devyarn add rollup-plugin-esnext-to-nodenext -Dpnpm add rollup-plugin-esnext-to-nodenext -DUsage
import esnextToNodeNext from 'rollup-plugin-esnext-to-nodenext';
import { defineConfig } from 'rollup';
export default defineConfig({
input: 'src/index.ts',
output: {
file: 'dist/index.mjs',
format: 'esm',
sourcemap: true,
},
plugins: [
esnextToNodeNext({
verbose: true, // enable logging (optional)
}),
],
});Options
| Option | Type | Default | Description |
|-------------|-----------|-----------------|--------------------------------------|
| verbose | boolean | false | Enable detailed logging |
| outputDir | string | auto-detected | Explicit output directory (optional) |
How It Works
The plugin:
- Detects your Rollup output directory automatically
- Processes all emitted files after bundling
- Transforms import statements like
from "./file"tofrom "./file.js" - Preserves all other bundling functionality
Why Use This?
This plugin solves a specific compatibility problem when your project uses **TypeScript with bundler module resolution
** during development, but needs to output Node.js-compatible ESM with nodenext resolution (particularly important
for type declarations).
Typical Use Case Example
Your tsconfig.json uses bundler-friendly settings:
{
"compilerOptions": {
"module": "esnext",
"moduleResolution": "bundler",
// ← Development mode (no .js extensions needed)
"outDir": "dist"
}
}But your output needs to work with Node.js ESM (nodenext):
// package.json
{
"type": "module",
"exports": {
".": {
"types": "./dist/index.d.ts",
// ← Must be nodenext-compatible
"import": "./dist/index.js"
}
}
}The plugin bridges this gap by:
- Taking Rollup's bundled output (no extensions)
- Transforming imports to be Node.js ESM-compliant:
- import { foo } from './utils'; + import { foo } from './utils.js'; - Ensuring type declarations work in strict
nodenextenvironments
When You Need This
- Publishing libraries with dual ESM/TypeScript support
- Building CLI tools that need strict Node.js ESM compliance
- Generating type declarations that must work in
nodenextprojects - Migrating codebases from bundler-friendly to Node-native ESM
Without this transformation, you'll see Node.js errors like:
Error [ERR_MODULE_NOT_FOUND]: Cannot find module './utils' imported from...
Did you mean to import ./utils.js?License
MIT © Vladislav Tupikin
