Skip to content

ESM build is not compatible with Node ESM loader #84

@e1himself

Description

@e1himself

Describe the bug

The @restart/ui package provides ESM and CommonJS builds. However, the ESM build is not really compliant with Nodejs ESM module resolution.

To Reproduce

  1. Start a new Nodejs package with type: "module"
  2. Add @restart/ui as a dependency
  3. Add index.js file containing the following:
    import { useRootClose } from '@restart/ui';
    console.log(useRootClose);
  4. Run the file with node index.js

Reproducible Example

https://github.com/e1himself/restart-ui-esm-incompatibility-demo

The test run:
https://github.com/e1himself/restart-ui-esm-incompatibility-demo/actions/runs/4915733576/jobs/8778577614

Expected behavior

The ESM module import successfully resolves.

Actual behavior

import { useRootClose } from '@restart/ui';
         ^^^^^^^^^^^^
SyntaxError: Named export 'useRootClose' not found. The requested module '@restart/ui' 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 '@restart/ui';
const { useRootClose } = pkg;

    at ModuleJob._instantiate (node:internal/modules/esm/module_job:123:21)
    at async ModuleJob.run (node:internal/modules/esm/module_job:189:5)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:530:24)
    at async loadESM (node:internal/process/esm_loader:91:5)
    at async handleMainPromise (node:internal/modules/run_main:65:12)

Environment (please complete the following information)

  • Operating System: Fedora Linux
  • Node v16.20.0
  • NPM 8.19.4

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions