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

feat(config): friendly ESM file require error #13283

Conversation

sapphi-red
Copy link
Member

Description

When importing an CJS package from the config, Vite throws the following errors.

X [ERROR] Failed to resolve entry for package "@sveltejs/vite-plugin-svelte". The package may have incorrect main/module/exports specified in its package.json: No known conditions for "." specifier in "@sveltejs/vite-plugin-svelte" package [plugin externalize-deps]
failed to load config from D:\documents\GitHub\vite\playground\alias\vite.config.js
error when starting dev server:
Error [ERR_REQUIRE_ESM]: require() of ES Module D:\documents\GitHub\vite\node_modules\.pnpm\slash@5.1.0\node_modules\slash\index.js from D:\documents\GitHub\vite\playground\alias\vite.config.js not supported.
Instead change the require of index.js in D:\documents\GitHub\vite\playground\alias\vite.config.js to a dynamic import() which is available in all CommonJS modules.

These errors seems to be confusing for the users: https://github.com/sapphi-red/vitepress-plugins/issues?q=is%3Aissue+ERR_REQUIRE_ESM+is%3Aclosed, #13255, https://github.com/Subwaytime/vite-aliases/issues?q=is%3Aissue+is%3Aclosed+%22Failed+to+resolve+entry+for+package%22, https://github.com/sveltejs/vite-plugin-svelte/issues?q=is%3Aissue+%22Failed+to+resolve+entry+for+package%22

This PR changes the error message to tell what happened and what needs to be done.

X [ERROR] "slash" resolved to an ESM file. ESM file cannot be loaded by `require`. See http://vitejs.dev/guide/troubleshooting.html#this-package-is-esm-only for more details. [plugin externalize-deps]
X [ERROR] Failed to resolve "@sveltejs/vite-plugin-svelte". This package is ESM only but it was tried to load by `require`. See http://vitejs.dev/guide/troubleshooting.html#this-package-is-esm-only for more details. [plugin externalize-deps]

I hope this would unblock plugins to migrate to ESM only.

Additional context


What is the purpose of this pull request?

  • Bug fix
  • New Feature
  • Documentation update
  • Other

Before submitting the PR, please make sure you do the following

  • Read the Contributing Guidelines.
  • Read the Pull Request Guidelines and follow the PR Title Convention.
  • Check that there isn't already a PR that solves the problem the same way to avoid creating a duplicate.
  • Provide a description in this PR that addresses what the PR is solving, or reference the issue that it solves (e.g. fixes #123).
  • Ideally, include relevant tests that fail without this PR but pass with it.

@sapphi-red sapphi-red added the p2-nice-to-have Not breaking anything but nice to have (priority) label May 21, 2023
@stackblitz
Copy link

stackblitz bot commented May 21, 2023

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

@patak-dev
Copy link
Member

Looks great! Just some naming nitpicks. Cheers for an ESM only future 🙏🏼

@patak-dev patak-dev added this to the 4.4 milestone May 23, 2023
Copy link
Member

@bluwy bluwy left a comment

Choose a reason for hiding this comment

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

Looks like a nice addition. FWIW I've also raised this in node without much conclusion: https://github.com/orgs/nodejs/discussions/46379. I've been leaning towards using "default" condition in ESM-only packages to workaround this though.

@patak-dev patak-dev merged commit b9a6ba0 into vitejs:main Jun 6, 2023
13 checks passed
@sapphi-red sapphi-red deleted the feat/friendly-esm-plugin-import-error-from-config branch June 7, 2023 09:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p2-nice-to-have Not breaking anything but nice to have (priority)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants