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 — doc: fixes ESM/CJS wrapper example by fox1t · Pull Request #34853 · nodejs/node

Conversation

@fox1t

@fox1t fox1t commented Aug 20, 2020

Copy link
Copy Markdown
Contributor

This PR updates ESM wrapper example to prevent package maintainers to broke packages after copy-pasting.

Ref: #34714

Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • tests and/or benchmarks are included
  • documentation is changed or added
  • commit message follows commit guidelines

Sorry, something went wrong.

Uh oh!

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

bricss reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
@fox1t
doc: fixes ESM/CJS wrapper example
fde16f8
@nodejs-github-bot nodejs-github-bot added doc Issues and PRs related to Node.js documentation. esm Issues and PRs related to the ECMAScript Modules implementation. labels Aug 20, 2020
@nodejs-github-bot

nodejs-github-bot commented Aug 20, 2020

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/modules
All reactions

Sorry, something went wrong.

Uh oh!

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

jasnell
jasnell approved these changes Aug 20, 2020
View reviewed changes
mcollina
mcollina approved these changes Aug 20, 2020
View reviewed changes

@mcollina mcollina 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
@jasnell jasnell added the fast-track PRs proposed for a shorter-than-standard waiting period before landing. label Aug 20, 2020
@ljharb

ljharb commented Aug 20, 2020

Copy link
Copy Markdown
Member

the CJS extension was an intentional choice here; what would be broken without this change?

All reactions

Sorry, something went wrong.

Uh oh!

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

@jasnell

This comment has been minimized.

Sign in to view
@jasnell jasnell removed the fast-track PRs proposed for a shorter-than-standard waiting period before landing. label Aug 20, 2020
@jasnell

jasnell commented Aug 20, 2020

Copy link
Copy Markdown
Member

Removed fast-track label given @ljharb's comment

All reactions

Sorry, something went wrong.

Uh oh!

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

@fox1t

fox1t commented Aug 20, 2020

Copy link
Copy Markdown
Contributor Author

@ljharb here, a real world example: #34714 (comment)

All reactions

Sorry, something went wrong.

Uh oh!

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

@ljharb

ljharb commented Aug 20, 2020

Copy link
Copy Markdown
Member

That discusses only removing type module, not changing extensions.

All reactions

Sorry, something went wrong.

Uh oh!

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

@fox1t

fox1t commented Aug 20, 2020

Copy link
Copy Markdown
Contributor Author

@MylesBorins make a snippet and I copied it: #34714 (comment)
How ever if we decide to use .cjs I can update the PR.

All reactions

Sorry, something went wrong.

Uh oh!

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

@ljharb

ljharb commented Aug 20, 2020

Copy link
Copy Markdown
Member

I think it's useful to leave the .cjs there so people know it exists.

Separately, by using .cjs, type: "module" doesn't actually have any effect, so i'm still a bit confused by what's broken.

All reactions

Sorry, something went wrong.

Uh oh!

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

@fox1t

fox1t commented Aug 20, 2020

Copy link
Copy Markdown
Contributor Author

Because what the maintainers do is to copy-paste that snippet and since they already have all the codebase that has .js extension, just removes the c letter in both places. Since type is module (and they don't know that leaving it like that makes the js files be interpreted as ESM) they brake the package in Node versions that support type module property (so the newer ones). As I pointed out, the snippet is fine but ESM is still new and developers doesn't know how to deal with it.

All reactions

Sorry, something went wrong.

Uh oh!

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

GeoffreyBooth
GeoffreyBooth requested changes Aug 20, 2020
View reviewed changes

@GeoffreyBooth GeoffreyBooth 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.

This is not an improvement. The example was written intentionally, as we want to encourage people to include the "type" field, even if they also use explicit extensions (as in this example).

Sorry, something went wrong.

Uh oh!

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

All reactions
@fox1t

fox1t commented Aug 20, 2020

Copy link
Copy Markdown
Contributor Author

@GeoffreyBooth I tend to agree with you. However, I think that in this transition period it is important to be sure that the package maintainers are not going to change their pacakge.json file to:

{
  "type": "module",
  "main": "./index.js",
  "exports": {
    "import": "./wrapper.mjs",
    "require": "./index.js"
  }
}

Any ideas? We can add a warning note that points out that "type": "module", will make the legacy .js files act as ESM?

All reactions

Sorry, something went wrong.

Uh oh!

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

@GeoffreyBooth

GeoffreyBooth commented Aug 20, 2020

Copy link
Copy Markdown
Member

We can add a warning note that points out that "type": "module", will make the legacy .js files act as ESM?

The example doesn't use .js at all, so a warning seems unnecessary.

All reactions

Sorry, something went wrong.

Uh oh!

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

@fox1t

fox1t commented Aug 20, 2020 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Contributor Author

@GeoffreyBooth, the purpose of this snippet example is to make it easy to support ESM in CommonJS code. Its audience is developers that have CJS files all over the place (with .js extension). They are implicitly using "type": "commonjs" today, without even knowing it. I think that putting both "type": "module" and .cjs in that example could lead to really bad situations that I tried to explain as best as I could.

In addition to that, I hope that we all agree that the scope of a guide/documentation is to be easily understandable by just looking at the paragraph that you need. We cannot hope that all of the devs that are interested in supporting ESM in "legacy" packages are going to read all of the documentation about ESM in order to learn that they have to put "type": "commonjs", if they are NOT going to change all of their files to have .cjs extension.

All reactions

Sorry, something went wrong.

Uh oh!

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

@GeoffreyBooth

GeoffreyBooth commented Aug 20, 2020

Copy link
Copy Markdown
Member

It sounds like your concern is that people will copy this example, change the .cjs or .mjs to .js as appropriate for their codebase, and move on without realizing the consequences of using the .js extension. Is that about right?

So perhaps what would satisfy that concern would be a sentence below the snippet like:

Note that the above example uses explicit extensions .mjs and .cjs. If your files use the .js extension, note that "type": "module" will cause such files to be treated as ES modules, just as "type": "commonjs" would cause them to be treated as CommonJS. See Enabling.

fox1t and zackschuster reacted with heart emoji
All reactions
  • ❤️ 2 reactions

Sorry, something went wrong.

Uh oh!

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

@fox1t

fox1t commented Aug 21, 2020

Copy link
Copy Markdown
Contributor Author

@GeoffreyBooth I think that this might fix this specific problem. I am going to revert the snippet and add the disclaimer at the bottom of it. Is it fine?

All reactions

Sorry, something went wrong.

Uh oh!

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

@fox1t
Reverts wrapper snippet and adds a warning note
f38a85e
@fox1t
fox1t requested review from GeoffreyBooth, jasnell and mcollina August 21, 2020 07:47
@fox1t
Fixes md lint issues
1a2312a
ljharb
ljharb reviewed Aug 21, 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.

fox1t reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
@fox1t
Removes trailing space
06e581f
GeoffreyBooth
GeoffreyBooth previously requested changes Aug 21, 2020
View reviewed changes

@GeoffreyBooth GeoffreyBooth 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.

You need to change the link to Enabling to be relative to the page (see the other links) but otherwise this is fine.

Sorry, something went wrong.

Uh oh!

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

All reactions
addaleax pushed a commit that referenced this pull request Sep 22, 2020
@fox1t @addaleax
doc: fix ESM/CJS wrapper example
500b737
PR-URL: #34853
Refs: #34714
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
addaleax pushed a commit that referenced this pull request Sep 22, 2020
@fox1t @addaleax
doc: fix ESM/CJS wrapper example
da150f4
PR-URL: #34853
Refs: #34714
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
@codebytere codebytere mentioned this pull request Sep 28, 2020
v12.19.0 proposal #35401
Merged
This was referenced May 27, 2022
[Snyk] Fix for 1 vulnerabilities Dmarch28/node#59
Open
[Snyk] Fix for 1 vulnerabilities aliceUnhinged613/node#59
Open
@137717unity 137717unity mentioned this pull request May 27, 2022
[Snyk] Fix for 1 vulnerabilities 137717unity/node#58
Open
@baby636 baby636 mentioned this pull request May 27, 2022
[Snyk] Fix for 1 vulnerabilities baby636/node#56
Open
@snyk-bot snyk-bot mentioned this pull request May 27, 2022
[Snyk] Fix for 1 vulnerabilities kissofire/node#68
Open
@wedataintelligence wedataintelligence mentioned this pull request May 27, 2022
[Snyk] Fix for 1 vulnerabilities wedataintelligence/node#34
Open
@ron-a20 ron-a20 mentioned this pull request May 27, 2022
[Snyk] Fix for 1 vulnerabilities ron-a20/node#67
Open
@molllyn1 molllyn1 mentioned this pull request May 28, 2022
[Snyk] Fix for 1 vulnerabilities molllyn1/node#49
Open
@snyk-bot snyk-bot mentioned this pull request Sep 25, 2022
[Snyk] Fix for 22 vulnerabilities 137717unity/node#70
Open
@wedataintelligence wedataintelligence mentioned this pull request Sep 26, 2022
[Snyk] Fix for 22 vulnerabilities wedataintelligence/node#44
Open
@137717unity 137717unity mentioned this pull request Sep 26, 2022
[Snyk] Fix for 22 vulnerabilities 137717unity/node#73
Open
@snyk-bot snyk-bot mentioned this pull request Sep 27, 2022
[Snyk] Fix for 21 vulnerabilities Rednickle/node-1#56
Open
@wedataintelligence wedataintelligence mentioned this pull request Sep 29, 2022
[Snyk] Fix for 24 vulnerabilities wedataintelligence/node#50
Open
This was referenced Sep 29, 2022
[Snyk] Fix for 24 vulnerabilities 137717unity/node#75
Open
[Snyk] Fix for 23 vulnerabilities Rednickle/node-1#60
Open
This was referenced Oct 5, 2022
[Snyk] Fix for 24 vulnerabilities wedataintelligence/node#51
Open
[Snyk] Fix for 24 vulnerabilities wedataintelligence/node#52
Open
@snyk-bot snyk-bot mentioned this pull request Oct 6, 2022
[Snyk] Fix for 24 vulnerabilities wedataintelligence/node#53
Open
@137717unity 137717unity mentioned this pull request Oct 7, 2022
[Snyk] Fix for 24 vulnerabilities 137717unity/node#76
Open
@wedataintelligence wedataintelligence mentioned this pull request Oct 7, 2022
[Snyk] Fix for 24 vulnerabilities wedataintelligence/node#54
Open
This was referenced Oct 8, 2022
[Snyk] Fix for 24 vulnerabilities wedataintelligence/node#55
Open
[Snyk] Fix for 24 vulnerabilities 137717unity/node#77
Open
@137717unity 137717unity mentioned this pull request Oct 9, 2022
[Snyk] Fix for 24 vulnerabilities 137717unity/node#78
Open
@snyk-bot snyk-bot mentioned this pull request Oct 9, 2022
[Snyk] Fix for 23 vulnerabilities Rednickle/node-1#63
Open
@kissofire kissofire mentioned this pull request Jun 25, 2023
[Snyk] Fix for 28 vulnerabilities kissofire/node#102
Open
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
@Trott Trott Trott approved these changes
@GeoffreyBooth GeoffreyBooth GeoffreyBooth left review comments
@mcollina mcollina Awaiting requested review from mcollina
@jasnell jasnell Awaiting requested review from jasnell

Uh oh!

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

Assignees

No one assigned

Labels

doc Issues and PRs related to Node.js documentation. 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.

7 participants

@fox1t @nodejs-github-bot @ljharb @jasnell @GeoffreyBooth @Trott @mcollina
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 01402775 — 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.