Title: Runtime error with *esModuleInterop* when CJS module has an export named "default" · Issue #38540 · microsoft/TypeScript · GitHub
Open Graph Title: Runtime error with *esModuleInterop* when CJS module has an export named "default" · Issue #38540 · microsoft/TypeScript
X Title: Runtime error with *esModuleInterop* when CJS module has an export named "default" · Issue #38540 · microsoft/TypeScript
Description: TypeScript Version: 3.9.2 & 4.0.0-dev.20200512 Search Terms: __setModuleDefault, __importStar, redefine, ts-lib Code Check out the full example at https://github.com/marcelltoth/typescript-bug File dangerous-module.js (commonjs module) c...
Open Graph Description: TypeScript Version: 3.9.2 & 4.0.0-dev.20200512 Search Terms: __setModuleDefault, __importStar, redefine, ts-lib Code Check out the full example at https://github.com/marcelltoth/typescript-bug File...
X Description: TypeScript Version: 3.9.2 & 4.0.0-dev.20200512 Search Terms: __setModuleDefault, __importStar, redefine, ts-lib Code Check out the full example at https://github.com/marcelltoth/typescript-bug ...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/38540
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Runtime error with *esModuleInterop* when CJS module has an export named \"default\"","articleBody":"\u003c!-- 🚨 STOP 🚨 𝗦𝗧𝗢𝗣 🚨 𝑺𝑻𝑶𝑷 🚨\r\n\r\nHalf of all issues filed here are duplicates, answered in the FAQ, or not appropriate for the bug tracker. Even if you think you've found a *bug*, please read the FAQ first, especially the Common \"Bugs\" That Aren't Bugs section!\r\n\r\nPlease help us by doing the following steps before logging an issue:\r\n * Search: https://github.com/Microsoft/TypeScript/search?type=Issues\r\n * Read the FAQ: https://github.com/Microsoft/TypeScript/wiki/FAQ\r\n\r\nPlease fill in the *entire* template below.\r\n-->\r\n\r\n\u003c!--\r\nPlease try to reproduce the issue with the latest published version. It may have already been fixed.\r\nFor npm: `typescript@next`\r\nThis is also the 'Nightly' version in the playground: http://www.typescriptlang.org/play/?ts=Nightly\r\n-->\r\n**TypeScript Version:** 3.9.2 & 4.0.0-dev.20200512 \r\n\r\n\u003c!-- Search terms you tried before logging this (so others can find this issue more easily) -->\r\n**Search Terms: __setModuleDefault, __importStar, redefine, ts-lib**\r\n\r\n**Code**\r\n\r\nCheck out the full example at https://github.com/marcelltoth/typescript-bug\r\n\r\nFile `dangerous-module.js` (commonjs module)\r\n```js\r\nconst a = {\r\n someConstant: 2\r\n};\r\nmodule.exports = a;\r\n// Allow use of default import syntax in TypeScript\r\nmodule.exports.default = a;\r\n```\r\n\r\nThis is a pattern seen in real world, when the authors of CJS modules are trying to be nice with us TypeScript users. This way the module is nicely consumable from TS without *esModuleInterop*. The pattern is used by - for example - [axios](https://github.com/axios/axios/blob/master/lib/axios.js#L53)\r\n\r\nI have *esModuleInterop* and therefore *allowSyntheticDefaultImports* turned on. Then in another file:\r\n\r\nFile `index.ts`\r\n```ts\r\nimport m, {someConstant} from './dangerous-module.js';\r\n\r\nconsole.log(m);\r\nconsole.log(someConstant);\r\n```\r\n\r\nI build it via `tsc`, then run `node dist/index.js` on the output.\r\n\r\n**Expected behavior:**\r\n\r\nThe code logs to the console.\r\n\r\n**Actual behavior:**\r\n\r\nIt throws on the import line like so:\r\n> /home/marcelltoth/source/typescript-bug/dist/index.js:10\r\n> Object.defineProperty(o, \"default\", { enumerable: true, value: v });\r\n> ^\r\n>\r\n>TypeError: Cannot redefine property: default\r\n> at Function.defineProperty (\u003canonymous>)\r\n> at /home/marcelltoth/source/typescript-bug/dist/index.js:10:12\r\n> at __importStar (/home/marcelltoth/source/typescript-bug/dist/index.js:18:5)\r\n> at Object.\u003canonymous> (/home/marcelltoth/source/typescript-bug/dist/index.js:22:29)\r\n> at Module._compile (internal/modules/cjs/loader.js:1158:30)\r\n> at Object.Module._extensions..js (internal/modules/cjs/loader.js:1178:10)\r\n> at Module.load (internal/modules/cjs/loader.js:1002:32)\r\n> at Function.Module._load (internal/modules/cjs/loader.js:901:14)\r\n> at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)\r\n> at internal/main/run_main_module.js:18:47\r\n\r\n**Playground Link:** https://github.com/marcelltoth/typescript-bug\r\n\r\n**Related Issues:** https://github.com/microsoft/TypeScript/issues/37113\r\n","author":{"url":"https://github.com/marcelltoth","@type":"Person","name":"marcelltoth"},"datePublished":"2020-05-13T14:04:01Z","url":"https://github.com/microsoft/TypeScript/issues/38540"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:82f4a840-29a8-e349-fdf2-8e66779750f6 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E47C:2463BD:D313AB:1108DD6:6B00F338 |
| html-safe-nonce | 5fd7451d03283278d283282a5a793b1f76a168e407cb13f50be45ad59ee68f98 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFNDdDOjI0NjNCRDpEMzEzQUI6MTEwOERENjo2QjAwRjMzOCIsInZpc2l0b3JfaWQiOiIzNjkxODIxNTAxMzY0ODkyNDcyIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 89073975c84d7f8dea6a3f762047957e4a909962d696b9d2ff79e3fc755c52ba |
| hovercard-subject-tag | issue:617477376 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/microsoft/TypeScript/38540/issue_layout |
| twitter:image | https://opengraph.githubassets.com/bf0027e6314daf71b5a0ca4a8be7837eb9a54194aa73ad13124829c52a05047a/microsoft/TypeScript/issues/38540 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/bf0027e6314daf71b5a0ca4a8be7837eb9a54194aa73ad13124829c52a05047a/microsoft/TypeScript/issues/38540 |
| og:image:alt | TypeScript Version: 3.9.2 & 4.0.0-dev.20200512 Search Terms: __setModuleDefault, __importStar, redefine, ts-lib Code Check out the full example at https://github.com/marcelltoth/typescript-bug File... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | marcelltoth |
| hostname | github.com |
| expected-hostname | github.com |
| None | 4ef498cbabcbe3c50befc7a5b743e7072ee770b4daf0e15e54801180d1fa91fc |
| turbo-cache-control | no-preview |
| go-import | github.com/microsoft/TypeScript git https://github.com/microsoft/TypeScript.git |
| octolytics-dimension-user_id | 6154722 |
| octolytics-dimension-user_login | microsoft |
| octolytics-dimension-repository_id | 20929025 |
| octolytics-dimension-repository_nwo | microsoft/TypeScript |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 20929025 |
| octolytics-dimension-repository_network_root_nwo | microsoft/TypeScript |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 9983dd5da06943f833c971dd5b6bc988e2858be5 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width