-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
does not provide an export named "xxx" #744
Comments
The problem is that when CommonJS modules are converted to ESM, they only export a single default export. It's not just esbuild that behaves this way; node itself does as well. This is because the exports of CommonJS modules can depend on run-time behavior. You can do two things to fix this:
|
Closing because I believe the solutions described above to be sufficient. Either that or this is a duplicate of #442. |
@evanw Thanks, this can solve my problem. 🍻 Two output files share the same path but have different contents: _dist/@chunk/index.js This should be caused by different modules but using the same basename. entryPoints: [
"....node_module/react/index.js",
"....node_module/react-dom/index.js"
], They have the same I understand that you are preparing the onEmit method to solve this kind of problem, but how can I solve this problem before implementing the onEmit method ? |
That wasn't a problem with the code I posted because the entry points were the package names directly instead of the result of calling The underlying reason for this collision is that the path for the entry point ends in |
@evanw Thanks, I understand, this is my negligence. |
@evanw Thank you for the plugin, but it has several problems. 1,Output path missing or wrongWhen I build require('esbuild').build({
format: 'esm',
entryPoints: [
"@material-ui/core"
],
....
plugins: [cjs_to_esm_plugin],
}) When the plugin is not used, the output path is After using the plugin, the output path becomes The path P1 is a wrong path because it changed the import indicator: import { Button } from "@material-ui/core" // correct
// becomes:
import { Button } from "@material-ui/core/esm" // wrong The path P2 is also a wrong path, it is missing The correct output path should be 2,does not provide an export named 'default'After using this plugin, Good: import { useState, useContext } from "./@chunk/react.js" Bad: import React from "./@chunk/react.js"
^^^^^
SyntaxError: The requested module './@chunk/react.js' does not provide an export named 'default' The plugin is a good solution, but it still needs to be improved. |
@evanw please give me some help 🙏 |
The plugin was just a starting point to work from. You should be able to take it from here. You can add the |
@evanw Yeah, as you said, I added return {
contents: `
import * as m from ${path};
import { ${keys} } from ${path};
export { ${keys} };
export default m;
`,
resolveDir
} Now it works. But it will still cause the output path to be wrong or missing. The P1 error path appears, I guess it is because esbuild does not generate the output path according to entryPoint, but generates the path according to the module file path, which causes this problem. The P2 error path appeared because of the export code added using the above plugin. I don't know the specific reason behind it. I don't know how to solve this problem, please give me some help. 🙏 |
I know that now there is no solution, I do not know how vite solve this |
1 similar comment
I know that now there is no solution, I do not know how vite solve this |
I transform react library to esm through the following operation:
But I get an error while importing:
How can i solve this problem ?
node: v15.5.0 (Native support for esm)
esbuild": "^0.8.31"
The text was updated successfully, but these errors were encountered: