-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
ES2015 and up don't handle ESM default exports correctly #5768
Comments
The error occurred because of TDZ which is expected. See this example, run it in node native ESM environments, you will get the error. import foo from "./index.js"; // self import
console.log(foo); // ReferenceError: Cannot access 'foo' before initialization
export default () => {
return "hello";
}; move the import foo from "./index.js"; // self import
export default () => {
return "hello";
};
console.log(foo); // [Function: default] |
I tested using chrome but Object.defineProperty(window, "__esModule", {
value: true
});
Object.defineProperty(window, "default", {
enumerable: true,
get: ()=>_default
});
const _default = ()=>{
return "hello";
}; worked. |
I'll try to create a repo where I am experiencing this. I ran into this at work so I'll have to try and recreate this on my home machine. My issue was actually the |
Okay here is a repo to reproduce the issue. Files of interest:
Is this just expected with CommonJS? IMPORTANT: Thanks for looking! EDIT: Well heck, is there any benefit to choosing the target when compiling for Jest? Like what benefit do you get from using es2022 over es5? |
@lgibso34 try this https://gist.github.com/magic-akari/5a8a1bee1c78aa94b383572bed2ce44e This line is where the error is located. You can get rid of the error by using the lazy trick. const someObj = {
Temp: ()=> Temp,
}; |
Thank you for spending the time on this. I appreciate your feedback. The lazy trick is a good solution. Could you explain why this is happening? Like it works when you set the target to ES5 and it worked with babel previously. I'd like to leave here with complete understanding since to me it "seems" like an issue that it works with ES5 but not with ES2015 and up. I know now that it may not be an issue with Thanks again. |
Originally posted by @evanw in evanw/esbuild#1444 (comment)
Originally posted by @evanw in evanw/esbuild#1395 (comment) We can follow these steps to understand how ESM works. For the following input. ( Removed the React content because they are not relevant. ) // const.js
import Temp from "./temp";
const someObj = {
Temp, // because this is importing Temp and Temp is importing somethingElse it hits this error
};
export const somethingElse = "not using Temp but commonJS does not tree shake"; // temp.js
import { somethingElse } from "./const.js";
const Temp = () => {
return somethingElse;
};
export default Temp;
export { Temp }; // index.js
import temp from "./temp.js" And put them togeter by those steps. we get the following codes. const someObj = {
Temp, // because this is importing Temp and Temp is importing somethingElse it hits this error
};
const somethingElse = "not using Temp but commonJS does not tree shake";
const Temp = () => {
return somethingElse;
}; It is easy to see that this is not valid. Note: Changing How to get rid of this error?
The following code works as expected because of the function hoist. const someObj = {
Temp, // because this is importing Temp and Temp is importing somethingElse it hits this error
};
const somethingElse = "not using Temp but commonJS does not tree shake";
function Temp() {
return somethingElse;
}; Function hoist works in ESM as well. export default function Temp() {
return somethingElse;
};
export { Temp }; Note: |
When you write If you do not need / do not care the ESM behaviour, try the following. import foo = require("./foo"); // CTS syntax, cjs require in TypeScript
function x(){
}
// `module.exports = ` in CTS
export = {
bar: 42,
x,
} |
This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you. |
Describe the bug
I ran into this when using @swc/jest. The error is
ReferenceError: Cannot '_default' before intialization
const
seems to have start being used in 1.2.212 causing this issue. Previous versions were usingvar
which gets hoisted.Input code
No response
Config
No response
Playground link
https://play.swc.rs/?version=1.2.212&code=H4sIAAAAAAAAA0utKMgvKlFISU1LLM0pUdDQVLC1U6jm5VIAgqLUktKiPAWljNScnHwlXq5aAErKsCUtAAAA&config=H4sIAAAAAAAAA0WOSwrDMAxE76J1Fm2hG9%2BhhzCuEhz8Q%2BNATfDdK4eE7Ib5PGmnFY7MTsUKWIZCS9X%2ByFBtheHEl0oTVahVZeOu2srCVRuM1%2BP51jTkDCYz2wCeKPrk5zZYLsciDNyRTUu4ml1ZMX%2B3YezHOWXqJOa0gvoNOscen7N9PPIHEdWl27wAAAA%3D
Expected behavior
The code should compile correctly in a usable manner and not error.
Actual behavior
No response
Version
1.2.212
Additional context
No response
The text was updated successfully, but these errors were encountered: