GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — module: better error for named exports from cjs by MylesBorins · Pull Request #33256 · nodejs/node

Conversation

@MylesBorins

@MylesBorins MylesBorins commented May 5, 2020

Copy link
Copy Markdown
Contributor

We do not support importing named exports from a CJS module.
This change decorates the error message for missing named exports in
the case that the module being imported is expected to be CJS by the
ESM loader.

Some high level questions

  • Are folks ok with how I've decorated the error? It is a bit fragile, as it relies on the format of the error in V8.
  • Are folks ok with the copy of the error message? I took a quick stab at it but it could maybe be better

Needs a test, but wanted to confirm folks would be open to doing this before hacking on a test.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

brainkim and bricss reacted with eyes emoji
All reactions
  • 👀 2 reactions
@nodejs-github-bot nodejs-github-bot added the esm Issues and PRs related to the ECMAScript Modules implementation. label May 5, 2020
@guybedford

guybedford commented May 5, 2020

Copy link
Copy Markdown
Contributor

Definitely a +1 to this, it is much needed.

Ideally the error message could contain the exact context and solution, eg something like:

No named export named "asdf" for CommonJS module /path/to/module.js
CommonJS modules only have a "default" export, try importing this instead

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

mathiasbynens
mathiasbynens reviewed May 5, 2020
View reviewed changes
Comment thread lib/internal/modules/esm/module_job.js Resolved

Uh oh!

There was an error while loading. Please reload this page.

@MylesBorins

MylesBorins commented May 5, 2020

Copy link
Copy Markdown
Contributor Author

@guybedford getting the specific named export that was attempted to be imported would be a bit more work digging into the internals (is a hidden part of the error message). Happy to include the solution regarding the default but was unsure how verbose to be

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@MylesBorins MylesBorins mentioned this pull request May 5, 2020
Rediscussion for out-of-order CJS exec? nodejs/modules#509
Closed
ljharb
ljharb reviewed May 5, 2020
View reviewed changes

@ljharb ljharb left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cc @nodejs/modules-active-members

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread lib/internal/modules/esm/module_job.js Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread lib/internal/modules/esm/module_job.js Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread lib/internal/modules/esm/module_job.js Resolved

Uh oh!

There was an error while loading. Please reload this page.

addaleax
addaleax reviewed May 5, 2020
View reviewed changes
Comment thread lib/internal/modules/esm/module_job.js Resolved

Uh oh!

There was an error while loading. Please reload this page.

@GeoffreyBooth

GeoffreyBooth commented May 5, 2020

Copy link
Copy Markdown
Member

Ideally the error message could contain the exact context and solution, eg something like:

The requested module 'pkg' is expected to be of type CommonJS which does not
support named exports. CommonJS modules can be imported by importing the
default, for example: import pkg from 'pkg'; const { name } = pkg;
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

devsnek
devsnek reviewed May 5, 2020
View reviewed changes
Comment thread lib/internal/modules/esm/module_job.js Resolved

Uh oh!

There was an error while loading. Please reload this page.

@addaleax

addaleax commented May 5, 2020

Copy link
Copy Markdown
Member

@GeoffreyBooth … jinx ;)

GeoffreyBooth and MylesBorins reacted with laugh emoji
All reactions
  • 😄 2 reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@MylesBorins

MylesBorins commented May 5, 2020

Copy link
Copy Markdown
Contributor Author

Ok... I've accepted the proposed changes and made another stab at the text. Currently it uses a generic module name and named imports... We should be able to make the error message include the explicit named imports that were used but I'll need to dig in a bit how the message is being decorated with the arrow to do this.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

addaleax
addaleax reviewed May 6, 2020
View reviewed changes
Comment thread lib/internal/util.js Resolved

Uh oh!

There was an error while loading. Please reload this page.

@MylesBorins

MylesBorins commented May 6, 2020

Copy link
Copy Markdown
Contributor Author

ok... it is gross but it works 🎉.

Added options argument to the decorate error function and utilized the existing code that grabs the failing import statement. Errors will now point out the error and offer alternative working code.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

ljharb
ljharb reviewed May 6, 2020
View reviewed changes
Comment thread lib/internal/modules/esm/module_job.js Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread lib/internal/util.js Resolved

Uh oh!

There was an error while loading. Please reload this page.

@benjamingr

benjamingr commented May 6, 2020

Copy link
Copy Markdown
Member

I'm not up to date enough in that space right now to approve IMO but changes LGTM.

I just want to make sure that semverness is taken into account and that if modules are stable we make sure to choose the appropriate semverness for an error message change.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@MylesBorins

MylesBorins commented May 6, 2020

Copy link
Copy Markdown
Contributor Author

@benjamingr ESM is still marked experimental in the docs and changes are not subject to Semver

benjamingr reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@mafintosh

mafintosh commented May 6, 2020

Copy link
Copy Markdown
Member

@MylesBorins thanks for this! much appreciated

MylesBorins reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

ljharb
ljharb reviewed May 6, 2020
View reviewed changes
Comment thread lib/internal/modules/esm/module_job.js Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread lib/internal/modules/esm/module_job.js Resolved

Uh oh!

There was an error while loading. Please reload this page.

@MylesBorins

MylesBorins commented May 6, 2020 via email

Copy link
Copy Markdown
Contributor Author
This is catching a V8 error, not an node.js error. I can double check but I don't believe these errors have error.code
…
On Wed, May 6, 2020 at 5:18 PM Jordan Harband ***@***.***> wrote: ***@***.**** commented on this pull request. ------------------------------ In lib/internal/modules/esm/module_job.js <#33256 (comment)>: > @@ -92,7 +94,22 @@ class ModuleJob { this.module.instantiate(); } } catch (e) { - decorateErrorStack(e); + if (e.message.includes('does not provide an export named')) { + const rootURL = this.module.url; + const childSpecifier = StringPrototypeMatch( + e.message, + // Match the first string in either single or double quotes fair enough; it still seems safer to me to use error codes to pivot, and to store this information as a property on the error rather than parsing it out of the error message — You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub <#33256 (comment)>, or unsubscribe <https://github.com/notifications/unsubscribe-auth/AADZYVZE2I67XE62JGFUFMLRQHHYTANCNFSM4MZ4EM7Q> .
ljharb reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

GeoffreyBooth
GeoffreyBooth reviewed May 6, 2020
View reviewed changes
Comment thread lib/internal/util.js Resolved

Uh oh!

There was an error while loading. Please reload this page.

GeoffreyBooth
GeoffreyBooth reviewed May 7, 2020
View reviewed changes
Comment thread lib/internal/util.js Resolved

Uh oh!

There was an error while loading. Please reload this page.

@MylesBorins
MylesBorins force-pushed the better-cjs-named-export-error branch from 69199b7 to 7a40821 Compare May 7, 2020 06:45
@MylesBorins

MylesBorins commented May 7, 2020

Copy link
Copy Markdown
Contributor Author

PTAL I've added tests and refactored pretty significantly including documentation in the code that gets a bit funky.

I do not have tests for specifiers that have escaped quotes as my various manual tests seem to imply it is impossible to import esmodules in Node.js with escaped quotes in the specifier. If my assumption here is wrong someone please let me know.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@targos

targos commented May 7, 2020

Copy link
Copy Markdown
Member

I do not have tests for specifiers that have escaped quotes as my various manual tests seem to imply it is impossible to import esmodules in Node.js with escaped quotes in the specifier. If my assumption here is wrong someone please let me know.

Not sure what you mean. This works:

import { value } from './a\'b.js';
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

ljharb
ljharb reviewed May 7, 2020
View reviewed changes
Comment thread lib/internal/modules/esm/module_job.js Resolved

Uh oh!

There was an error while loading. Please reload this page.

ljharb
ljharb reviewed May 7, 2020
View reviewed changes

@ljharb ljharb left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread lib/internal/util.js Resolved

Uh oh!

There was an error while loading. Please reload this page.

@MylesBorins

MylesBorins commented May 7, 2020

Copy link
Copy Markdown
Contributor Author

OK... so it looks like escaped strings can work as specifiers... and it also looks like V8 normalizes this before printing a message... so independent of if single or double quotes have been used, if quote inside the string are single / double or escaped, the message will always include only single quote 😅

SyntaxError: The requested module './oh'no.cjs' does not provide an export named 'value'

I've gone ahead and removed the use of a regular expression and am instead manually separating the string based on knowing the shape it will be (this is hardcoded into V8). If this changes internally in V8 all the tests will blow up.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

MylesBorins
MylesBorins commented May 7, 2020
View reviewed changes
Comment thread lib/internal/modules/esm/module_job.js Resolved

Uh oh!

There was an error while loading. Please reload this page.

MylesBorins
MylesBorins commented May 7, 2020
View reviewed changes
Comment thread lib/internal/modules/esm/module_job.js Resolved

Uh oh!

There was an error while loading. Please reload this page.

addaleax
addaleax approved these changes May 7, 2020
View reviewed changes

@addaleax addaleax left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@nodejs-github-bot

nodejs-github-bot commented May 7, 2020

Copy link
Copy Markdown
Collaborator

CI: https://ci.nodejs.org/job/node-test-pull-request/31215/

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

GeoffreyBooth
GeoffreyBooth reviewed May 8, 2020
View reviewed changes
Comment thread test/es-module/test-esm-cjs-named-error.mjs Resolved

Uh oh!

There was an error while loading. Please reload this page.

@MylesBorins
MylesBorins force-pushed the better-cjs-named-export-error branch from 603a879 to 06cdf08 Compare May 8, 2020 05:06
@MylesBorins

MylesBorins commented May 8, 2020

Copy link
Copy Markdown
Contributor Author

OK... so this was a wild ride but I've fixed all the things. I didn't write a test for the indirection sub package.json edge case I was thinking could be an issue... but all child modules, including bare imports, are 100% being resolved relative to the file that the import statement was contained in 🎉.

This should cover all cases of escape characters, although might give inaccurate results depending on the string we get from V8.

ljharb reacted with rocket emoji
All reactions
  • 🚀 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@nodejs-github-bot

nodejs-github-bot commented May 8, 2020

Copy link
Copy Markdown
Collaborator

CI: https://ci.nodejs.org/job/node-test-pull-request/31236/

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

ljharb
ljharb reviewed May 8, 2020
View reviewed changes

@ljharb ljharb left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread test/fixtures/es-modules/package-cjs-named-error/json-hack/fail.js Outdated

@ljharb ljharb May 8, 2020

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

everybody to the limit

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

MylesBorins, DerekNonGeneric, and apottere reacted with rocket emoji
All reactions
  • 🚀 3 reactions

@DerekNonGeneric DerekNonGeneric May 8, 2020

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe foo would be more appropriate here?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@ljharb ljharb May 8, 2020 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i don't see how anything could possibly be more appropriate than this particular reference

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

DerekNonGeneric and GaryGSC reacted with laugh emoji MylesBorins reacted with eyes emoji
All reactions
  • 😄 2 reactions
  • 👀 1 reaction
Comment thread lib/internal/modules/esm/module_job.js Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread lib/internal/modules/esm/module_job.js Resolved

Uh oh!

There was an error while loading. Please reload this page.

targos
targos reviewed May 8, 2020
View reviewed changes
Comment thread lib/internal/modules/esm/module_job.js Resolved

Uh oh!

There was an error while loading. Please reload this page.

targos
targos approved these changes May 8, 2020
View reviewed changes
@MylesBorins
module: better error for named exports from cjs
75f08f9
We do not support importing named exports from a CJS module.
This change decorates the error message for missing named exports in
the case where the module being imported is expected to be CJS by the
ESM loader.

Signed-off-by: Myles Borins <[email protected]>
@MylesBorins
MylesBorins force-pushed the better-cjs-named-export-error branch from d0df6ab to 75f08f9 Compare May 8, 2020 15:42
@nodejs-github-bot

nodejs-github-bot commented May 8, 2020

Copy link
Copy Markdown
Collaborator

CI: https://ci.nodejs.org/job/node-test-pull-request/31242/

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@nodejs-github-bot

nodejs-github-bot commented May 8, 2020

Copy link
Copy Markdown
Collaborator

CI: https://ci.nodejs.org/job/node-test-pull-request/31243/

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@MylesBorins

MylesBorins commented May 8, 2020

Copy link
Copy Markdown
Contributor Author

CI is green. Going to land this EOD unless there are any blockers

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

MylesBorins added a commit that referenced this pull request May 8, 2020
@MylesBorins
module: better error for named exports from cjs
2c3c9f5
We do not support importing named exports from a CJS module.
This change decorates the error message for missing named exports in
the case where the module being imported is expected to be CJS by the
ESM loader.

Signed-off-by: Myles Borins <[email protected]>

PR-URL: #33256
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Michaël Zasso <[email protected]>
@MylesBorins

MylesBorins commented May 8, 2020

Copy link
Copy Markdown
Contributor Author

Landed in 2c3c9f5

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@MylesBorins MylesBorins closed this May 8, 2020
codebytere pushed a commit that referenced this pull request May 11, 2020
@MylesBorins @codebytere
module: better error for named exports from cjs
ad86807
We do not support importing named exports from a CJS module.
This change decorates the error message for missing named exports in
the case where the module being imported is expected to be CJS by the
ESM loader.

Signed-off-by: Myles Borins <[email protected]>

PR-URL: #33256
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Michaël Zasso <[email protected]>
@codebytere codebytere mentioned this pull request May 18, 2020
v14.3.0 proposal #33452
Merged
codebytere pushed a commit that referenced this pull request Jun 7, 2020
@MylesBorins @codebytere
module: better error for named exports from cjs
77caf92
We do not support importing named exports from a CJS module.
This change decorates the error message for missing named exports in
the case where the module being imported is expected to be CJS by the
ESM loader.

Signed-off-by: Myles Borins <[email protected]>

PR-URL: #33256
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Michaël Zasso <[email protected]>
@codebytere codebytere mentioned this pull request Jun 9, 2020
v12.18.1 proposal #33811
Merged
@ctavan ctavan mentioned this pull request Sep 18, 2020
module: improve error decoration for cjs named exports for multi-line import statements #35259
Closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@ljharb ljharb ljharb left review comments
@pipobscure pipobscure pipobscure left review comments
@GeoffreyBooth GeoffreyBooth GeoffreyBooth left review comments
@hybrist hybrist hybrist left review comments
@addaleax addaleax addaleax approved these changes
+4 more reviewers
@mathiasbynens mathiasbynens mathiasbynens left review comments
@devsnek devsnek devsnek left review comments
@DerekNonGeneric DerekNonGeneric DerekNonGeneric left review comments
@targos targos targos approved these changes
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

esm Issues and PRs related to the ECMAScript Modules implementation.

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

14 participants

@MylesBorins @guybedford @GeoffreyBooth @addaleax @benjamingr @mafintosh @targos @nodejs-github-bot @ljharb @mathiasbynens @pipobscure @hybrist @devsnek @DerekNonGeneric
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.