René's URL Explorer Experiment


Title: Tracking issue: Named imports from CJS module incorrectly allowed in nodenext · Issue #54018 · microsoft/TypeScript · GitHub

Open Graph Title: Tracking issue: Named imports from CJS module incorrectly allowed in nodenext · Issue #54018 · microsoft/TypeScript

X Title: Tracking issue: Named imports from CJS module incorrectly allowed in nodenext · Issue #54018 · microsoft/TypeScript

Description: This issue has come up a few times, most recently at DefinitelyTyped/DefinitelyTyped#65147 (comment), and I don’t think we’ve had a canonical place to explain, discuss, or track it. The symptom Sometimes, when you’re writing an ES module...

Open Graph Description: This issue has come up a few times, most recently at DefinitelyTyped/DefinitelyTyped#65147 (comment), and I don’t think we’ve had a canonical place to explain, discuss, or track it. The symptom Som...

X Description: This issue has come up a few times, most recently at DefinitelyTyped/DefinitelyTyped#65147 (comment), and I don’t think we’ve had a canonical place to explain, discuss, or track it. The symptom Som...

Opengraph URL: https://github.com/microsoft/TypeScript/issues/54018

X: @github

direct link

Domain: github.com

IPv4 address: 140.82.114.4


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Tracking issue: Named imports from CJS module incorrectly allowed in nodenext","articleBody":"This issue has come up a few times, most recently at https://github.com/DefinitelyTyped/DefinitelyTyped/pull/65147#issuecomment-1515134407, and I don’t think we’ve had a canonical place to explain, discuss, or track it.\r\n\r\n## The symptom\r\n\r\nSometimes, when you’re writing an ES module in `--module nodenext`, you will want to import a CJS dependency, and TypeScript lets you used a named import:\r\n\r\n```ts\r\nimport { createProgram } from \"typescript\";\r\ncreateProgram(/* ... */);\r\n```\r\n\r\nbut Node complains:\r\n\r\n```\r\nfile:///project/out/main.mjs:1\r\nimport { createProgram } from \"typescript\";\r\n         ^^^^^^^^^^^^^\r\nSyntaxError: Named export 'createProgram' not found. The requested module 'typescript' is a CommonJS module, which may not support all module.exports as named exports.\r\nCommonJS modules can always be imported via the default export, for example using:\r\n\r\nimport pkg from 'typescript';\r\nconst { createProgram } = pkg;\r\n```\r\n\r\nAlternatively but equivalently, you might try to use a namespace import:\r\n\r\n```ts\r\nimport * as ts from \"typescript\";\r\nts.createProgram(/* ... */);\r\n```\r\n\r\nand Node gives a much less helpful error:\r\n\r\n```\r\nfile:///project/out/main.mjs:2\r\nts.createProgram();\r\n   ^\r\n\r\nTypeError: ts.createProgram is not a function\r\n```\r\n\r\nIn either case, you find that you instead need to write a default import:\r\n\r\n```ts\r\nimport ts from \"typescript\";\r\nts.createProgram(/* ... */);\r\n```\r\n\r\nBoth TypeScript and Node are happy with this, but why did TypeScript let you write the forms that crash at runtime?\r\n\r\n## The problem\r\n\r\nNode uses [cjs-module-lexer](https://github.com/nodejs/cjs-module-lexer) to syntactically analyze CommonJS modules without executing them in order to turn `module.exports` properties into named exports that can be imported as named (or namespace) imports by ES modules. However, syntactic analysis has its limitations, and not all `module.exports` properties get detected on all modules. (In practice, it’s common for this to be all-or-nothing, as with the `typescript` package in its current state: it has _no_ named exports available.)\r\n\r\nType definitions have no way of declaring which exports/properties of a CommonJS module will be detectable by Node’s named export analysis. In other words, `typescript.d.ts` is not misrepresenting the shape of the module; it would be impossible to “fix” it for Node ESM consumers without breaking it for CJS consumers or bundlers, which typically have more relaxed interop rules. TypeScript currently assumes that _all_ declared exports/properties of a CJS module will be detectable by Node and available as named imports. This is unsound, leading to the error in the example above.\r\n\r\nIt should be noted that at least for the _named_ import case, the crash occurs during Node’s module linking phase (as soon as the module graph is loaded—at startup, unless the affected part of the graph is isolated in a dynamic import), with a good runtime error message. While annoying to run into, it’s usually immediately diagnosable and fixable. The namespace import variation is a bit more insidious, since the import can be linked, but subsequent property accesses, which can occur later in execution, may fail.\r\n\r\n## Solutions and non-solutions\r\n\r\nI’m putting this issue up for tracking/documentation purposes, not to advocate for a fix. But it’s worth mentioning a few ways of addressing the problem since people will ask or suggest them:\r\n\r\n- We could make a flag that makes potentially unsafe named/namespace imports always error. It would be very cumbersome and catch a lot of false positives, but some people may prefer the safety over the convenience.\r\n- We could make it possible to annotate modules or individual exports/properties in type declarations that cannot be correctly analyzed by Node. This sounds pretty fraught and fragile to me, given how often people postprocess both their types and JS by third-party build tools.\r\n- We _cannot_ reasonably determine which exports/properties are detectable by Node by looking at the JS, e.g. by running cjs-module-lexer ourselves, because we never even resolve or read JS sources when types are found for them. Even without incurring the cost of running the (fast) lexer, the performance penalty for doing twice as much module resolution and file system hits would be unacceptable.","author":{"url":"https://github.com/andrewbranch","@type":"Person","name":"andrewbranch"},"datePublished":"2023-04-25T22:02:56Z","url":"https://github.com/microsoft/TypeScript/issues/54018"}

react-profiling0
react-import-mapreact,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-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:1f693702-b208-c669-6736-16ee5a863762
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-id9F1A:3179F7:40954F:52D60B:6B04F74E
html-safe-noncec043224214c12aff050cc260459ac632481225aea37e0f423a0a1d0427298761
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5RjFBOjMxNzlGNzo0MDk1NEY6NTJENjBCOjZCMDRGNzRFIiwidmlzaXRvcl9pZCI6IjEwMzYyMzQ4NTEzNTcwOTU3NTgiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ==
visitor-hmac6e9be1fe252fdc80d8316457304c0efa54c8c8b660d67454707210eb0d03d9d9
hovercard-subject-tagissue:1683938587
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/microsoft/TypeScript/54018/issue_layout
twitter:imagehttps://opengraph.githubassets.com/ed5622ad454ad6966349af48991536a9199556ce33f7ff2b4d1d7273dee6a182/microsoft/TypeScript/issues/54018
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/ed5622ad454ad6966349af48991536a9199556ce33f7ff2b4d1d7273dee6a182/microsoft/TypeScript/issues/54018
og:image:altThis issue has come up a few times, most recently at DefinitelyTyped/DefinitelyTyped#65147 (comment), and I don’t think we’ve had a canonical place to explain, discuss, or track it. The symptom Som...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernameandrewbranch
hostnamegithub.com
expected-hostnamegithub.com
None2c3971d6ab4be607cf9a26ac26ef0a7bfce380ec2ba14642e64028ad07a492b9
turbo-cache-controlno-preview
go-importgithub.com/microsoft/TypeScript git https://github.com/microsoft/TypeScript.git
octolytics-dimension-user_id6154722
octolytics-dimension-user_loginmicrosoft
octolytics-dimension-repository_id20929025
octolytics-dimension-repository_nwomicrosoft/TypeScript
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id20929025
octolytics-dimension-repository_network_root_nwomicrosoft/TypeScript
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
releaseb3f1d45dee14b3f87c64e9017a9c08d011561786
ui-targetfull
theme-color#1e2327
color-schemelight dark
react-app-nameissues

Links:

Skip to contenthttps://github.com/microsoft/TypeScript/issues/54018#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fmicrosoft%2FTypeScript%2Fissues%2F54018
GitHubRepository hosting, code review, and collaborationhttps://github.com/platform
CopilotAI coding agent for tasks, projects, and automated workhttps://github.com/features/copilot
ActionsCI/CD and automationhttps://github.com/features/actions
Code QualityQuality analysis, fixes, and enforcementhttps://github.com/features/code-quality
CodespacesCloud development environmentshttps://github.com/features/codespaces
Code SecurityVulnerability detection and remediationhttps://github.com/security/advanced-security/code-security
Secret ProtectionSecret scanning and leak preventionhttps://github.com/security/advanced-security/secret-protection
All Featureshttps://github.com/features
Documentationhttps://docs.github.com/en
Traininghttps://learn.github.com/skills
Certificationshttps://learn.github.com/credentials
Build with Copilothttps://github.com/features/ai
Customize Copilothttps://docs.github.com/en/copilot/how-tos/copilot-on-github/customize-copilot/customize-copilot-overview
Integrationshttps://github.com/marketplace
Trending reposhttps://github.com/trending
GitHub Sponsorshttps://github.com/open-source/sponsors
Developer articleshttps://github.com/resources/articles
ChangelogYour source for the latest features, improvements, and fixes across GitHub products, all in one place.https://github.blog/changelog/
Enterpriseshttps://github.com/enterprise
Teamhttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
See all industrieshttps://github.com/solutions/industry
Why GitHubhttps://github.com/why-github
Customer Storieshttps://github.com/customer-stories
Business insightshttps://github.com/solutions/executive-insights
Trust Centerhttps://github.com/trust-center
GitHub Advanced SecurityEnterprise-grade code securityhttps://github.com/security/advanced-security
Copilot for businessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/enterprise/premium-support
Documentationhttps://docs.github.com/en
Resource centerhttps://github.com/resources
Bloghttps://github.blog/
Events & webinarshttps://github.com/resources/events
Community Forumhttps://github.com/orgs/community/discussions
Customer Storieshttps://github.com/customer-stories
Integrationshttps://github.com/marketplace
Customer supporthttps://help.github.com/
Traininghttps://learn.github.com/skills
GitHub Statushttps://www.githubstatus.com/
Abouthttps://github.com/about
Partnershttps://github.com/partners
Changeloghttps://github.blog/changelog/
GitHub UniverseThe event for developers, by developers — talks, workshops, and what’s next for GitHub.https://githubuniverse.com/
Pricinghttps://github.com/pricing
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fmicrosoft%2FTypeScript%2Fissues%2F54018
Sign uphttps://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=microsoft%2FTypeScript
Reloadhttps://github.com/microsoft/TypeScript/issues/54018
Reloadhttps://github.com/microsoft/TypeScript/issues/54018
Reloadhttps://github.com/microsoft/TypeScript/issues/54018
microsofthttps://github.com/microsoft
TypeScripthttps://github.com/microsoft/TypeScript
Codehttps://github.com/microsoft/TypeScript
Issues5k (5k)https://github.com/microsoft/TypeScript/issues
Pull requests319 (319)https://github.com/microsoft/TypeScript/pulls
Actionshttps://github.com/microsoft/TypeScript/actions
Projectshttps://github.com/microsoft/TypeScript/projects
Wikihttps://github.com/microsoft/TypeScript/wiki
Security and qualityhttps://github.com/microsoft/TypeScript/security
Insightshttps://github.com/microsoft/TypeScript/insights
https://github.com/microsoft/TypeScript
New issuehttps://github.com/login?return_to=https://github.com/microsoft/TypeScript/issues/54018
New issuehttps://github.com/login?return_to=https://github.com/microsoft/TypeScript/issues/54018
Tracking issue: Named imports from CJS module incorrectly allowed in nodenexthttps://github.com/microsoft/TypeScript/issues/54018#top
Discussionhttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Discussion%22
https://github.com/andrewbranch
Andrew Branch (andrewbranch)https://github.com/andrewbranch
on Apr 25, 2023https://github.com/microsoft/TypeScript/issues/54018#issue-1683938587
DefinitelyTyped/DefinitelyTyped#65147 (comment)https://github.com/DefinitelyTyped/DefinitelyTyped/pull/65147#issuecomment-1515134407
cjs-module-lexerhttps://github.com/nodejs/cjs-module-lexer
Andrew Branch (andrewbranch)https://github.com/andrewbranch
Discussionhttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Discussion%22
on Apr 25, 2023https://github.com/microsoft/TypeScript/issues/54018#event-9097025688
https://github.com/fatcerberus
Bruce Pascoe (fatcerberus)https://github.com/fatcerberus
on Apr 26, 2023https://github.com/microsoft/TypeScript/issues/54018#issuecomment-1522789803
https://github.com/segevfiner
Segev Finer (segevfiner)https://github.com/segevfiner
on Nov 27, 2023https://github.com/microsoft/TypeScript/issues/54018#issuecomment-1828719140
https://github.com/andrewbranch
Andrew Branch (andrewbranch)https://github.com/andrewbranch
on Nov 27, 2023https://github.com/microsoft/TypeScript/issues/54018#issuecomment-1828766691
https://github.com/segevfiner
Segev Finer (segevfiner)https://github.com/segevfiner
on Nov 27, 2023https://github.com/microsoft/TypeScript/issues/54018#issuecomment-1828787908
https://github.com/jakebailey
Jake Bailey (jakebailey)https://github.com/jakebailey
on Mar 18, 2024https://github.com/microsoft/TypeScript/issues/54018#issuecomment-2005288340
#57133https://github.com/microsoft/TypeScript/pull/57133
Lev Chelyadinov (illright)https://github.com/illright
on May 30, 2024https://github.com/microsoft/TypeScript/issues/54018#event-12988376372
Fix referencing of TypeScripthttps://github.com/feature-sliced/filesystem/commit/a331d695dbb1cd52492ab29ef86b72cc8e150e74
a331d69https://github.com/feature-sliced/filesystem/commit/a331d695dbb1cd52492ab29ef86b72cc8e150e74
https://github.com/shlroland
Roland (shlroland)https://github.com/shlroland
on Dec 19, 2024https://github.com/microsoft/TypeScript/issues/54018#issuecomment-2554633943
nodejs/node#56304https://github.com/nodejs/node/issues/56304
Antoine du Hamel (@aduh95)https://github.com/aduh95
Sign up for freehttps://github.com/signup?return_to=https://github.com/microsoft/TypeScript/issues/54018
Sign in to commenthttps://github.com/login?return_to=https://github.com/microsoft/TypeScript/issues/54018
Discussionhttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Discussion%22
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Response headers:
Servergithub.com
Content-Typetext/html; charset=utf-8
Cache-Controlmax-age=0, private, must-revalidate

Viewport: width=device-width


URLs of crawlers that visited me.