René's URL Explorer Experiment


Title: Provide some mechanism to conditionally and synchronously import modules (or just builtins) from ESM · Issue #52599 · nodejs/node · GitHub

Open Graph Title: Provide some mechanism to conditionally and synchronously import modules (or just builtins) from ESM · Issue #52599 · nodejs/node

X Title: Provide some mechanism to conditionally and synchronously import modules (or just builtins) from ESM · Issue #52599 · nodejs/node

Description: What is the problem this feature will solve? Thanks to #51977, requiring ESM is looking to be a real possibility. As such, TypeScript is considering transitioning over to ESM in the near future (depending on when require(ESM) is unflagge...

Open Graph Description: What is the problem this feature will solve? Thanks to #51977, requiring ESM is looking to be a real possibility. As such, TypeScript is considering transitioning over to ESM in the near future (de...

X Description: What is the problem this feature will solve? Thanks to #51977, requiring ESM is looking to be a real possibility. As such, TypeScript is considering transitioning over to ESM in the near future (de...

Opengraph URL: https://github.com/nodejs/node/issues/52599

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":"Provide some mechanism to conditionally and synchronously import modules (or just builtins) from ESM","articleBody":"### What is the problem this feature will solve?\r\n\r\nThanks to #51977, requiring ESM is looking to be a real possibility. As such, TypeScript is considering transitioning over to ESM in the near future (depending on when `require(ESM)` is unflagged, hopefully in time for TS 6.0?), as that sort of change would no longer pose compatibility problems for the vast number of downstream CJS users. This has a number of benefits, mainly that we could finally share code between `tsc.js` and our public API without a startup perf regression, and that we wouldn't be duplicating code in our package (thankfully only two copies remain as of TS 5.5, down from _six_ copies in TS 4.9).\r\n\r\nTypeScript's current public API bundle is intentionally \"UMD-ish\", detecting whether or not `module.exports` exists and using it (declaring a global otherwise), then later conditionally requiring built-in modules like `fs` if we believe to be running within Node. This allows us to ship one single bundle that works in Node, browsers, and bundlers alike.\r\n\r\nHowever, the code that relies on conditional `require` is executed at the top-level as it's constructing the `ts.sys` object, the default \"system\" implementation for most of our APIs. Within CJS, this is fine, but within ESM, the only way to conditionally import something is by either:\r\n\r\n- Using top-level await (or doing it later asynchronously).\r\n- Importing `createRequire` from `node:module`.\r\n\r\nUsing top-level await breaks `require(ESM)`, the whole reason we think we can use ESM in the first place, and TS is infamously not async and couldn't import it later. Importing `node:module` is a moot choice, since if we could safely import `node:module`, we could have just imported `node:fs` and so on.\r\n\r\nSo, we need some mechanism to synchronously import modules, or at least the builtins.\r\n\r\nGiven `require(ESM)` is now possible, it sure seems like there could be a way to safely synchronously import modules in ESM that are already require-able from CJS after #51977.\r\n\r\n### What is the feature you are proposing to solve the problem?\r\n\r\nAfter discussing this in the TC39 Module Harmony meeting (with @guybedford @joyeecheung @JakobJingleheimer, others), there seemed to be a number of different paths forward:\r\n\r\n- Node or ECMAScript invent a new \"weak\" or \"optional\" import attribute (e.g. `import fs from \"node:fs\" with { optional: true }`) could be added; if the module fails to resolve, the imports are all undefined. This may require some sort of TC39 specing or proposal.\r\n  - In the meeting this, this seemed palatable, though potentially had a too-long time horizon to be helpful for TS or other projects trying to do the same thing as us.\r\n- Node can add `import.meta.require`. This was previously proposed at https://github.com/nodejs/modules/issues/130, but unfortunately drags CJS into the ESM world (potentially no more than `createRequire`, I suppose).\r\n  - In the meeting, this seemed \"okay\" in that it's something Node could offer \"now\", but less desirable than other options.\r\n- Node (+ whoever owns the import.meta spec) can add `import.meta.importSync` or `import.now`, which is effectively just `await import(...)` that only works on sync-loadable ESM.\r\n  - In the meeting, this seemed less palatable than other options without a more general use case or more supporting examples.\r\n- Node can add `import.meta.builtins` or similar (e.g. on `process`), which just provide access to Node's builtin modules. Largely, TS only needs `fs`, `path`, `os`, etc, so this would sidestep the \"sync import\" problem altogether. TS also conditionally imports `source-map-support`, so that would not work, though only in development. Thankfully, since one could get `node:module` this way, you can also shim `require` via `createRequire`, which is pretty neat.\r\n  - In the meeting, this seemed pretty palatable. There was mention of this being something useful for WinterCG or similar to WASM, but I'm not very qualified to fully understand that one. There was also discussion about whether or not it would be all-getters, since people do want to patch / mock builtins.\r\n\r\n### What alternatives have you considered?\r\n\r\nTS could also use `package.json` import maps to achieve \"conditional\" imports of Node-specific code, e.g. have an import like `#system` which in the Node condition imports from Node, but is shims otherwise. This seems to have a number of downsides in my view, specifically:\r\n\r\n- TS is bundled and our outputs are not associated with our inputs, so actually writing said code may be pretty challenging.\r\n- There are platforms other than Node that implement enough of the Node builtins to be compatible. Would they set the node condition? Would TS need to explicitly add mappings for each of these? What happens if our code is bundled? These gotchas make me feel like this would be too difficult to deal with.\r\n- Do import maps work when TS is loaded via a browser? Our intent is that we can go down to having only one copy of our code, but I don't think browsers understand what to do with `package.json` import maps. In the meeting, @JakobJingleheimer mentioned that one could remap imports like `node:fs` to shims, even to `data:...` blobs, but I'm definitely not experienced enough in browser ESM to know how to do that.","author":{"url":"https://github.com/jakebailey","@type":"Person","name":"jakebailey"},"datePublished":"2024-04-19T16:59:22Z","url":"https://github.com/nodejs/node/issues/52599"}

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:ca41287d-e004-b4bf-8eea-f7c77d4f8bec
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-idE18E:1173F1:1F9B6F9:29403FF:6B0535B6
html-safe-nonce239141d9236cab630760216225e9b7426d5f1141e8c39860c1e9af9fe9964411
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFMThFOjExNzNGMToxRjlCNkY5OjI5NDAzRkY6NkIwNTM1QjYiLCJ2aXNpdG9yX2lkIjoiNTEzMzM2ODc5ODI4NzgzNjU5OCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9
visitor-hmaceedd11618e7c52bf3ba11d9a9c27398b255406d8559930ab929dea96ecfe8514
hovercard-subject-tagissue:2253465122
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/nodejs/node/52599/issue_layout
twitter:imagehttps://opengraph.githubassets.com/d4277c856388bad7401d7177e90c8120451f6ac5ece544c1c9bb10faf66ac315/nodejs/node/issues/52599
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/d4277c856388bad7401d7177e90c8120451f6ac5ece544c1c9bb10faf66ac315/nodejs/node/issues/52599
og:image:altWhat is the problem this feature will solve? Thanks to #51977, requiring ESM is looking to be a real possibility. As such, TypeScript is considering transitioning over to ESM in the near future (de...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernamejakebailey
hostnamegithub.com
expected-hostnamegithub.com
None854d880890b5e847b3fce9d1b31f032107baeddeebad2aa379c48aa4f1451f6b
turbo-cache-controlno-preview
go-importgithub.com/nodejs/node git https://github.com/nodejs/node.git
octolytics-dimension-user_id9950313
octolytics-dimension-user_loginnodejs
octolytics-dimension-repository_id27193779
octolytics-dimension-repository_nwonodejs/node
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id27193779
octolytics-dimension-repository_network_root_nwonodejs/node
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
release220e980097d94ce616d5d11fbd5316d208e3fc93
ui-targetcanary-2
theme-color#1e2327
color-schemelight dark
react-app-nameissues

Links:

Skip to contenthttps://github.com/nodejs/node/issues/52599#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fnodejs%2Fnode%2Fissues%2F52599
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%2Fnodejs%2Fnode%2Fissues%2F52599
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=nodejs%2Fnode
Reloadhttps://github.com/nodejs/node/issues/52599
Reloadhttps://github.com/nodejs/node/issues/52599
Reloadhttps://github.com/nodejs/node/issues/52599
nodejshttps://github.com/nodejs
nodehttps://github.com/nodejs/node
Codehttps://github.com/nodejs/node
Issues630 (630)https://github.com/nodejs/node/issues
Pull requests655 (655)https://github.com/nodejs/node/pulls
Actionshttps://github.com/nodejs/node/actions
Projectshttps://github.com/nodejs/node/projects
Security and qualityhttps://github.com/nodejs/node/security
Insightshttps://github.com/nodejs/node/insights
https://github.com/nodejs/node
New issuehttps://github.com/login?return_to=https://github.com/nodejs/node/issues/52599
New issuehttps://github.com/login?return_to=https://github.com/nodejs/node/issues/52599
Provide some mechanism to conditionally and synchronously import modules (or just builtins) from ESMhttps://github.com/nodejs/node/issues/52599#top
feature requesthttps://github.com/nodejs/node/issues?q=state%3Aopen%20label%3A%22feature%20request%22
loadershttps://github.com/nodejs/node/issues?q=state%3Aopen%20label%3A%22loaders%22
https://github.com/jakebailey
jakebaileyhttps://github.com/jakebailey
on Apr 19, 2024https://github.com/nodejs/node/issues/52599#issue-2253465122
#51977https://github.com/nodejs/node/pull/51977
#51977https://github.com/nodejs/node/pull/51977
@guybedfordhttps://github.com/guybedford
@joyeecheunghttps://github.com/joyeecheung
@JakobJingleheimerhttps://github.com/JakobJingleheimer
Pull request opened for import.meta.require on core modules#130https://github.com/nodejs/modules/issues/130
@JakobJingleheimerhttps://github.com/JakobJingleheimer
Nexthttps://github.com/nodejs/node/issues/52599?timeline_page=1
jakebaileyhttps://github.com/jakebailey
feature requesthttps://github.com/nodejs/node/issues?q=state%3Aopen%20label%3A%22feature%20request%22
on Apr 19, 2024https://github.com/nodejs/node/issues/52599#event-12542377362
github-project-automationhttps://github.com/apps/github-project-automation
Node.js feature requestshttps://github.com/orgs/nodejs/projects/4
on Apr 19, 2024https://github.com/nodejs/node/issues/52599#event-14895938896
github-project-automationhttps://github.com/apps/github-project-automation
Node.js feature requestshttps://github.com/orgs/nodejs/projects/4
on Apr 19, 2024https://github.com/nodejs/node/issues/52599
https://github.com/nicolo-ribaudo
nicolo-ribaudohttps://github.com/nicolo-ribaudo
on Apr 19, 2024https://github.com/nodejs/node/issues/52599#issuecomment-2066960421
https://github.com/guybedford
guybedfordhttps://github.com/guybedford
on Apr 19, 2024https://github.com/nodejs/node/issues/52599#issuecomment-2066984368
https://github.com/littledan
littledanhttps://github.com/littledan
on Apr 19, 2024https://github.com/nodejs/node/issues/52599#issuecomment-2067041191
https://github.com/jakebailey
jakebaileyhttps://github.com/jakebailey
on Apr 19, 2024https://github.com/nodejs/node/issues/52599#issuecomment-2067047191
https://github.com/jakebailey
jakebaileyhttps://github.com/jakebailey
on Apr 19, 2024https://github.com/nodejs/node/issues/52599#issuecomment-2067049381
https://github.com/joyeecheung
joyeecheunghttps://github.com/joyeecheung
on Apr 19, 2024https://github.com/nodejs/node/issues/52599#issuecomment-2067399559
#52575https://github.com/nodejs/node/issues/52575
different per module based on the policyhttps://nodejs.org/api/permissions.html#dependency-redirection
https://github.com/joyeecheung
joyeecheunghttps://github.com/joyeecheung
on Apr 19, 2024https://github.com/nodejs/node/issues/52599#issuecomment-2067403365
https://github.com/joyeecheung
joyeecheunghttps://github.com/joyeecheung
on Apr 20, 2024https://github.com/nodejs/node/issues/52599#issuecomment-2067504014
avivkellerhttps://github.com/avivkeller
loadershttps://github.com/nodejs/node/issues?q=state%3Aopen%20label%3A%22loaders%22
on Apr 20, 2024https://github.com/nodejs/node/issues/52599#event-12548676622
https://github.com/jakebailey
jakebaileyhttps://github.com/jakebailey
on Apr 26, 2024https://github.com/nodejs/node/issues/52599#issuecomment-2078625631
https://github.com/jakebailey
jakebaileyhttps://github.com/jakebailey
on Apr 30, 2024https://github.com/nodejs/node/issues/52599#issuecomment-2084323892
https://private-user-images.githubusercontent.com/5341706/326659797-bb574e56-aa2a-4513-a969-946f51df15ad.png?jwt=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3OTU1MDM4NDMsIm5iZiI6MTc5NTUwMzU0MywicGF0aCI6Ii81MzQxNzA2LzMyNjY1OTc5Ny1iYjU3NGU1Ni1hYTJhLTQ1MTMtYTk2OS05NDZmNTFkZjE1YWQucG5nP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI2MTEyNCUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNjExMjRUMDY1OTAzWiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9M2U2M2Q5MmIzMjQyNDVlNTllODQ0ZDYxYTVlNjdlNzBiOGQ3ZDRjM2Q0NTA3NmZmNTc5ZWFiYjFhMGZkOWIzZCZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QmcmVzcG9uc2UtY29udGVudC10eXBlPWltYWdlJTJGcG5nIn0.-PUaQnH_8arIJX7V_o8uaTOgMbzJVTbqTaZC68CMS3s
https://github.com/jakebailey/TypeScript/tree/its-esmhttps://github.com/jakebailey/TypeScript/tree/its-esm
https://github.com/joyeecheung
joyeecheunghttps://github.com/joyeecheung
on Apr 30, 2024https://github.com/nodejs/node/issues/52599#issuecomment-2086158079
#52762https://github.com/nodejs/node/pull/52762
nodejs-github-bothttps://github.com/nodejs-github-bot
on May 28, 2024https://github.com/nodejs/node/issues/52599#event-12962824733
process: add process.getBuiltinModule(id)https://github.com/nodejs/node/commit/1de215e285bf8afebe0b9b1908009399e3b1f9e2
1de215ehttps://github.com/nodejs/node/commit/1de215e285bf8afebe0b9b1908009399e3b1f9e2
sthagenhttps://github.com/sthagen
on May 28, 2024https://github.com/nodejs/node/issues/52599#event-12962937561
doc: improve explanation about built-in moduleshttps://github.com/sthagen/nodejs-node/commit/e0b5deefc061009f0a0696d573ccfb447206b88a
e0b5deehttps://github.com/sthagen/nodejs-node/commit/e0b5deefc061009f0a0696d573ccfb447206b88a
targoshttps://github.com/targos
on Jun 1, 2024https://github.com/nodejs/node/issues/52599#event-13008628369
doc: improve explanation about built-in moduleshttps://github.com/nodejs/node/commit/1eb55f355090b0e45cf8e80fe8ff30bd997277c0
1eb55f3https://github.com/nodejs/node/commit/1eb55f355090b0e45cf8e80fe8ff30bd997277c0
process: add process.getBuiltinModule(id)https://github.com/nodejs/node/commit/374743cd4e864d903a3fde54c11573c2d1bec32d
374743chttps://github.com/nodejs/node/commit/374743cd4e864d903a3fde54c11573c2d1bec32d
https://github.com/AbhiPrasad
AbhiPrasadhttps://github.com/AbhiPrasad
on Jun 4, 2024https://github.com/nodejs/node/issues/52599#issuecomment-2148205468
open-telemetry/opentelemetry-js#4742https://github.com/open-telemetry/opentelemetry-js/issues/4742
https://github.com/joyeecheung
joyeecheunghttps://github.com/joyeecheung
on Jun 4, 2024https://github.com/nodejs/node/issues/52599#issuecomment-2148444828
eliphazbouyehttps://github.com/eliphazbouye
on Jun 20, 2024https://github.com/nodejs/node/issues/52599#event-13234958787
doc: improve explanation about built-in moduleshttps://github.com/eliphazbouye/node/commit/b8119f12e49df54fe7a61497fa296666f632e2d8
b8119f1https://github.com/eliphazbouye/node/commit/b8119f12e49df54fe7a61497fa296666f632e2d8
process: add process.getBuiltinModule(id)https://github.com/eliphazbouye/node/commit/78038dfc137737b6f9781dfa18fab26856af3d7f
78038dfhttps://github.com/eliphazbouye/node/commit/78038dfc137737b6f9781dfa18fab26856af3d7f
avivkellerhttps://github.com/avivkeller
Node.js feature requestshttps://github.com/orgs/nodejs/projects/4
on Sep 13, 2024https://github.com/nodejs/node/issues/52599
Sign up for freehttps://github.com/signup?return_to=https://github.com/nodejs/node/issues/52599
Sign in to commenthttps://github.com/login?return_to=https://github.com/nodejs/node/issues/52599
feature requesthttps://github.com/nodejs/node/issues?q=state%3Aopen%20label%3A%22feature%20request%22
loadershttps://github.com/nodejs/node/issues?q=state%3Aopen%20label%3A%22loaders%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.