An open API service indexing awesome lists of open source software.

https://github.com/mrrefactoring/rollup-plugin-esnext-to-nodenext

A Rollup plugin that automatically transforms ESM imports into Node.js-compatible nodenext format by adding explicit file extensions (.js/.mjs). Perfect for libraries needing to work with TypeScript's "moduleResolution": "nodenext".
https://github.com/mrrefactoring/rollup-plugin-esnext-to-nodenext

bundler commonjs esm esmodules import-extension javascript js-extension module-resolution module-system node-modules nodejs nodenext rollup rollup-plugin typescript typescript-plugin

Last synced: 2 months ago
JSON representation

A Rollup plugin that automatically transforms ESM imports into Node.js-compatible nodenext format by adding explicit file extensions (.js/.mjs). Perfect for libraries needing to work with TypeScript's "moduleResolution": "nodenext".

Awesome Lists containing this project

README

          


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

```bash
npm install rollup-plugin-esnext-to-nodenext --save-dev
```

```bash
yarn add rollup-plugin-esnext-to-nodenext -D
```

```bash
pnpm add rollup-plugin-esnext-to-nodenext -D
```

## Usage

```javascript
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:

```json
{
"compilerOptions": {
"module": "esnext",
"moduleResolution": "bundler",
// ← Development mode (no .js extensions needed)
"outDir": "dist"
}
}
```

But your output needs to work with Node.js ESM (`nodenext`):

```json
// 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:
```diff
- 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