Skip to content
This repository was archived by the owner on Sep 2, 2023. It is now read-only.
This repository was archived by the owner on Sep 2, 2023. It is now read-only.

require('some-es-package') does not fail as expected #325

Description

@tniessen

First of all, thanks to all of you for your efforts! I am currently playing around with Node.js 12.0.0 and the new ESM implementation. One thing I have come across is that require('some-es-package') does not fail as expected if some-es-package has a package.json file containing these properties:

"type": "module",
"main": "index.js"

The call to require() fails with a syntax error whereas it becomes ERR_REQUIRE_ESM if I rename index.js to index.mjs. Is this behavior intended?

Activity

  1. GeoffreyBooth commented on May 1, 2019

    @GeoffreyBooth
    Member

    See nodejs/node#27417 and #320.

    The short answer is that the behavior of require with respect to ESM files and packages is still a work in progress.

  2. weswigham commented on May 3, 2019

    @weswigham
    Contributor

    The call to require() fails with a syntax error whereas it becomes ERR_REQUIRE_ESM if I rename index.js to index.mjs. Is this behavior intended?

    Slightly more full answer: Yes and no. The behavior will likely change. It should ideally be ERR_REQUIRE_ESM in both cases, or not an error in either case. Moving from the (likely) parse error to the much nicer and more explainative ERR_REQUIRE_ESM error (or to not an error) hasn't really been seen as a semver breaking change, so fixing that up one way or another (which entails closing in on our final cjs interop strategy) hasn't been looked at much (sicne we're either just changing the text of a (likely) error to a better one or removing the errors entirely). In both cases, our logic for package-scoped configuration (for recognition of the type field) needs to get baked into the cjs resolver (as right now it is not) - our current implementation pretty much doesn't touch the cjs resolver, which is also why we just had to emergency patch in throwing on .mjs require's without the flag set.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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