Skip to content

Node ESM compatibility issue #58

@otakustay

Description

@otakustay

When using dedent in a node ESM environment, a import dedent from 'dedent' statement will result a {default: dedent} object.

> const dedent = await import('dedent')
undefined
> dedent`abc`
Uncaught TypeError: dedent is not a function
> dedent('abc')
Uncaught TypeError: dedent is not a function
> dedent
[Module: null prototype] {
  __esModule: undefined,
  default: <ref *1> [Function: dedent] { default: [Circular *1] }
}
> dedent.default`abc`
'abc'

This issue comes from several reasones:

  1. In package.json a module filed is specified to dist/dedent.mjs, but NodeJS doesn't support this field
  2. The main field in package.json references to dist/dedent.js which is a CommonJS module, NodeJS's ESM then transforms module.exports into a default export

To address this, we need a exports field in package.json:

{
  "exports": {
    ".": {
      "types": "./index.d.ts",
      "import": "./dist/dedent.mjs",
      "default": "./dist/dedent.js"
    }
  }
}

This will solve the import issue, but is potentially a breaking change to NodeJS ESM environment, would it worse a major version?

Metadata

Metadata

Labels

status: accepting prsPlease, send a pull request to resolve this!type: bugSomething isn't working :(

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions