rollup-plugin-esnext-to-nodenext
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

Rollup Plugin ESNext to NodeNext

NPM version NPM downloads per month build status license

A Rollup plugin that transforms ESM imports to Node.js-compatible nodenext format by adding explicit file extensions.

Features

  • Automatically adds .js extensions 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-dev
yarn add rollup-plugin-esnext-to-nodenext -D
pnpm add rollup-plugin-esnext-to-nodenext -D

Usage

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:

  1. Detects your Rollup output directory automatically
  2. Processes all emitted files after bundling
  3. Transforms import statements like from "./file" to from "./file.js"
  4. 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:

  1. Taking Rollup's bundled output (no extensions)
  2. Transforming imports to be Node.js ESM-compliant:
    - import { foo } from './utils';
    + import { foo } from './utils.js';
  3. Ensuring type declarations work in strict nodenext environments

When You Need This

  1. Publishing libraries with dual ESM/TypeScript support
  2. Building CLI tools that need strict Node.js ESM compliance
  3. Generating type declarations that must work in nodenext projects
  4. 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

Package Sidebar

Install

npm i rollup-plugin-esnext-to-nodenext

Weekly Downloads

20

Version

1.0.0

License

MIT

Unpacked Size

16.9 kB

Total Files

12

Last publish

Collaborators

  • mrrefactoring