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
Domain: github.com
IPv4 address: 140.82.114.4
{"@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-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:ca41287d-e004-b4bf-8eea-f7c77d4f8bec |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E18E:1173F1:1F9B6F9:29403FF:6B0535B6 |
| html-safe-nonce | 239141d9236cab630760216225e9b7426d5f1141e8c39860c1e9af9fe9964411 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFMThFOjExNzNGMToxRjlCNkY5OjI5NDAzRkY6NkIwNTM1QjYiLCJ2aXNpdG9yX2lkIjoiNTEzMzM2ODc5ODI4NzgzNjU5OCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | eedd11618e7c52bf3ba11d9a9c27398b255406d8559930ab929dea96ecfe8514 |
| hovercard-subject-tag | issue:2253465122 |
| 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/nodejs/node/52599/issue_layout |
| twitter:image | https://opengraph.githubassets.com/d4277c856388bad7401d7177e90c8120451f6ac5ece544c1c9bb10faf66ac315/nodejs/node/issues/52599 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/d4277c856388bad7401d7177e90c8120451f6ac5ece544c1c9bb10faf66ac315/nodejs/node/issues/52599 |
| og:image:alt | 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... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | jakebailey |
| hostname | github.com |
| expected-hostname | github.com |
| None | 854d880890b5e847b3fce9d1b31f032107baeddeebad2aa379c48aa4f1451f6b |
| turbo-cache-control | no-preview |
| go-import | github.com/nodejs/node git https://github.com/nodejs/node.git |
| octolytics-dimension-user_id | 9950313 |
| octolytics-dimension-user_login | nodejs |
| octolytics-dimension-repository_id | 27193779 |
| octolytics-dimension-repository_nwo | nodejs/node |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 27193779 |
| octolytics-dimension-repository_network_root_nwo | nodejs/node |
| 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 | 220e980097d94ce616d5d11fbd5316d208e3fc93 |
| ui-target | canary-2 |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | max-age=0, private, must-revalidate |
Viewport: width=device-width