Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: Use .mjs extension for library ESM bundle #7714

Closed
wants to merge 1 commit into from
Closed

docs: Use .mjs extension for library ESM bundle #7714

wants to merge 1 commit into from

Conversation

cdauth
Copy link

@cdauth cdauth commented Apr 13, 2022

As explained in #7609, when importing an ES module in Node.js, either its package needs to have "type": "module" set in its package.json, or its file extension needs to be .mjs. Otherwise the file will be interpreted as CommonJS and the import will fail.

The Library section of the Vite docs documents how to publish a library and also recommends to add an exports section to package.json. As far as I am aware, this section tells Node.js which file to import, although I believe some frontend bundlers are also adding support for it these days. The current documentation encourages developers to publish their ESM bundles with a .es.js extension, which will make the package impossible to be imported from an ESM Node.js project. Since not many Node.js projects use ESM yet, many developers are not aware of this problem.

This pull request changes the documentation to recommend the use of the .mjs extension for the generated bundle. This way the library bundle can be successfully used both in Node.js and in frontend bundlers.

What is the purpose of this pull request?

  • Bug fix
  • New Feature
  • Documentation update
  • Other

As explained in #7609, when importing an ES module in Node.js, either its package needs to have `"type": "module"` set in its `package.json`, or its file extension needs to be `.mjs`. Otherwise the file will be interpreted as CommonJS and the import will fail.

By changing the extension of the ESM bundle to `.mjs`, the library bundle can be successfully used both in Node.js and in frontend bundlers.
@cdauth cdauth changed the title Use .mjs extension for library ESM bundle docs: Use .mjs extension for library ESM bundle Apr 13, 2022
@Niputi Niputi added feat: library mode documentation Improvements or additions to documentation labels Apr 13, 2022
@patak-dev
Copy link
Member

See #6827, we are going to merge it as part of Vite v3 because it has a potential breaking change. I think we should update the docs together with that change in Vite (that is already part of the PR)
Thanks for looking into this!

@patak-dev patak-dev closed this Apr 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation feat: library mode
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants