Skip to content

Combine ERR_REQUIRE_ESM with warning #30599

Closed

Description

@guybedford

Now that modules are unflagged, the warning for ERR_REQUIRE_ESM when loading a .js file that would be interpreted as a module is being thrown along with the warning message in https://github.com/nodejs/node/blob/master/lib/internal/modules/cjs/loader.js#L1193.

This hinders approaches that want to use this error to explicitly check for support.

Ideally the warning can just be combined with the error message.

Activity

  1. added
    esmIssues and PRs related to the ECMAScript Modules implementation.
    good first issueIssues that are suitable for first-time contributors.
    help wantedIssues that need assistance from volunteers or PRs that need help to proceed.
    on Nov 22, 2019
  2. juanarbol commented on Nov 26, 2019

    @juanarbol
    Member

    I'm gonna take this one.

  3. guybedford commented on Nov 27, 2019

    @guybedford
    ContributorAuthor

    @juanarbol that would be really great - just let me know if I can help.

  4. juanarbol commented on Nov 27, 2019

    @juanarbol
    Member

    @guybedford sure! I'll ask you for some help if needed (I think i'll need it)

  5. juanarbol commented on Nov 27, 2019

    @juanarbol
    Member

    So, we still needing to throws the exception, but now, the warning should contain error (ERR_REQUIRE_ESM) message.

  6. guybedford commented on Nov 27, 2019

    @guybedford
    ContributorAuthor

    @juamedgod yes the error should contain in the message the descriptive contents of what is currently in the warning.

  7. juanarbol commented on Nov 28, 2019

    @juanarbol
    Member

    Jm, I'm misunderstanding something.

    So, we need to emit the current warning as is, but with the message of ERR_REQUIRE_ESM, and then redefine the message of the error to the same emmited in the warning. so new ERR_REQUIRE_ESM().message = 'same warning message emitted'

    In a most short way; the warning message emitted, should be the current message concatenated with ERR_REQUIRE_ESM message, and the message of the trowned ERR_REQUIRE_ESM should the same of the emitted warning.

    Am I right?

  8. guybedford commented on Nov 28, 2019

    @guybedford
    ContributorAuthor

    So at the moment we provide both a warning and an error message.

    The error message should continue to be thrown, but the useful information from the warning can be folded into the error message itself.

    This way it will still be shown to users, but libraries can wrap the error and swallow it if they need, whereas the warning cannot be silenced without disabling all warnings in Node.js.

  9. juanarbol commented on Nov 28, 2019

    @juanarbol
    Member

    This is what I've done:

    try {
      const a = require('./mod/file2.js')
      console.log(a)
    } catch (err) {
      console.log('\nError message: ')
      console.log(err.message)
    }
    (node:91392) Warning: require() of ES modules is not supported.
    require() of /Users/juanjose/Documents/GitHub/Node/node/mod/file2.js from /Users/juanjose/Documents/GitHub/Node/node/file.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
    Instead rename file2.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /Users/juanjose/Documents/GitHub/Node/node/mod/package.json.
    Must use import to load ES Module: /Users/juanjose/Documents/GitHub/Node/node/mod/file2.js
    
    Error message:
    Must use import to load ES Module: /Users/juanjose/Documents/GitHub/Node/node/mod/file2.js
    require() of ES modules is not supported.
    require() of /Users/juanjose/Documents/GitHub/Node/node/mod/file2.js from /Users/juanjose/Documents/GitHub/Node/node/file.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
    Instead rename file2.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /Users/juanjose/Documents/GitHub/Node/node/mod/package.json.

    Is this the expected behaviour?

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

    esmIssues and PRs related to the ECMAScript Modules implementation.good first issueIssues that are suitable for first-time contributors.help wantedIssues that need assistance from volunteers or PRs that need help to proceed.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions