Skip to content

ESM: Named export 'rm' not found #44

@idleberg

Description

@idleberg

Consider the following script:

// index.ts
import gitly from 'gitly';

gitly("https://github.com/iwatakeshi/gitly", process.cwd(), {});

Building it with various bundlers to ESM works fine:

  • npx esbuild --format=esm index.ts
  • npx tsup --format esm index.ts
  • npx tsdown --format esm index.ts

However, any of the resulting scripts will produce a runtime error:

import { rm as rm2 } from "shelljs";
         ^^
SyntaxError: Named export 'rm' not found. The requested module 'shelljs' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:

import pkg from 'shelljs';
const { rm: rm2 } = pkg;

    at #_instantiate (node:internal/modules/esm/module_job:254:21)
    at async ModuleJob.run (node:internal/modules/esm/module_job:369:5)
    at async onImport.tracePromise.__proto__ (node:internal/modules/esm/loader:691:26)
    at async asyncRunEntryPointWithESMLoader (node:internal/modules/run_main:101:5)

Node.js v24.11.0

When I change the build-target to CommonJS, everything works as desired. Following the suggestion from the error message will likely fix the issue.

Update This error is NodeJS-specific. Since you checked in a lockfile for Bun, I tested it there aswell and don't get an error.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions