{"action":"created","issue":{"url":"https://api.github.com/repos/nodejs/modules/issues/81","repository_url":"https://api.github.com/repos/nodejs/modules","labels_url":"https://api.github.com/repos/nodejs/modules/issues/81/labels{/name}","comments_url":"https://api.github.com/repos/nodejs/modules/issues/81/comments","events_url":"https://api.github.com/repos/nodejs/modules/issues/81/events","html_url":"https://github.com/nodejs/modules/issues/81","id":322971924,"number":81,"title":"Named exports when importing CJS","user":{"login":"giltayar","id":403268,"avatar_url":"https://avatars3.githubusercontent.com/u/403268?v=4","gravatar_id":"","url":"https://api.github.com/users/giltayar","html_url":"https://github.com/giltayar","followers_url":"https://api.github.com/users/giltayar/followers","following_url":"https://api.github.com/users/giltayar/following{/other_user}","gists_url":"https://api.github.com/users/giltayar/gists{/gist_id}","starred_url":"https://api.github.com/users/giltayar/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/giltayar/subscriptions","organizations_url":"https://api.github.com/users/giltayar/orgs","repos_url":"https://api.github.com/users/giltayar/repos","events_url":"https://api.github.com/users/giltayar/events{/privacy}","received_events_url":"https://api.github.com/users/giltayar/received_events","type":"User","site_admin":false},"labels":[{"id":831667756,"url":"https://api.github.com/repos/nodejs/modules/labels/interoperability","name":"interoperability","color":"7eeac3","default":false}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":79,"created_at":"2018-05-14T20:37:06Z","updated_at":"2018-05-16T12:32:44Z","closed_at":null,"author_association":"NONE","body":"Currently in NodeJS, if I import from a CJS module,\r\n\r\n```js\r\nimport ns from `./cjs-module.js`\r\n```\r\n\r\nIt will allow this, but won't allow named exports, i.e. the following is _not_ allowed:\r\n\r\n```js\r\nimport {namedSomething} from `./cjs-module.js`.\r\n```\r\n\r\nThis is called **transparent interop**. \r\nThe reason that NodeJS doesn't allow named exports is that determining what those exports are MUST happen in the _parsing_ phase of the ESM Spec (according to some, although there is contention about that too),  and executing code is not allowed at or prior to that phase. But a CJS module's exports can only be determined by evaluating the code in it (static analysis of JS is not an option as it is not determined to always give the correct results).\r\n\r\nThis would maybe have been cool if NodeJS was the first ESM implementation, but people are used to the babel way of doing modules, and in the babel world, named exports from CJS modules _are_ allowed. This is because babel does not conform to the ESM spec to the letter (it can't, because it just transpiles ESM to CJS).\r\n\r\nAnd, good or bad, developers _expect_ to use named exports when importing CJS.\r\n\r\nI see these options:\r\n\r\n1. Continue the existing way (no named exports for CJS)\r\n1. Don't conform to the spec when importing CJS\r\n1. Do late-linking/shaping of named modules based on late evaluation of CJS module\r\n1. Disallow transparent interop, and enable `import.meta.require` (or equivalent) to enable importing CJS from ES modules\r\n1. Enable metadata in the CJS module that can statically describe the exports for ESM, e.g. something like `//export default; export foo, bar;` at the head of the CJS file, thus enabling named exports when importing the file.\r\n\r\nI am sure there are others options, so if you have another option besides those four, please add it here.\r\n\r\nIt would be great if for each options you specify pros and cons, or at least if you don't like the option, specify a clear and simple use case that would be problematic if the option was chosen."},"comment":{"url":"https://api.github.com/repos/nodejs/modules/issues/comments/389501830","html_url":"https://github.com/nodejs/modules/issues/81#issuecomment-389501830","issue_url":"https://api.github.com/repos/nodejs/modules/issues/81","id":389501830,"user":{"login":"benjamingr","id":1315533,"avatar_url":"https://avatars2.githubusercontent.com/u/1315533?v=4","gravatar_id":"","url":"https://api.github.com/users/benjamingr","html_url":"https://github.com/benjamingr","followers_url":"https://api.github.com/users/benjamingr/followers","following_url":"https://api.github.com/users/benjamingr/following{/other_user}","gists_url":"https://api.github.com/users/benjamingr/gists{/gist_id}","starred_url":"https://api.github.com/users/benjamingr/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/benjamingr/subscriptions","organizations_url":"https://api.github.com/users/benjamingr/orgs","repos_url":"https://api.github.com/users/benjamingr/repos","events_url":"https://api.github.com/users/benjamingr/events{/privacy}","received_events_url":"https://api.github.com/users/benjamingr/received_events","type":"User","site_admin":false},"created_at":"2018-05-16T12:32:44Z","updated_at":"2018-05-16T12:32:44Z","author_association":"MEMBER","body":"Can we reach consensus on the following?\r\n\r\n - We agree to pursue spec compliant solutions in the nodejs/modules team and to address any non-spec-compliant changes we'd like by raising them up with TC39. We agree that it's important to do this in order to have real browser interop while supporting CJS.\r\n - We agree that we should provide a way for users to use CJS modules in Node.js from inside ESM and that this way should be about as easy for the average user as using ESM with current userland solutions. We acknowledge this means named exports should work.\r\n - We agree that we should discuss a compromise with regards to late bindings regardless of this issue - we should bring it up with TC39 (at the same breath as top level await which also causes late bindings if I understand correctly) and see if these concerns can be addressed at a language level.\r\n\r\nAlso - would anyone (or hopefully, two people suggesting alternatives) would be willing to bring this up for discussion in the next meeting? "}}
{"actor":{"display_login":"benjamingr"}}