Skip to content

Instantly share code, notes, and snippets.

@tyhopp
Created December 5, 2022 05:01
Show Gist options
  • Save tyhopp/4e13f681d385237959a150b01bd53871 to your computer and use it in GitHub Desktop.
Save tyhopp/4e13f681d385237959a150b01bd53871 to your computer and use it in GitHub Desktop.
await-import-conditional-cjs-exports
const myModule = await import(`./other.cjs`);
// Running node index.mjs writes this out to stdout, top-level `other` property is undefined:
// [Module: null prototype] {
// default: { hello: [Function: hello] },
// hello: [Function: hello],
// other: undefined
// }
console.log(myModule);
function hello() {
console.log(`world`);
}
exports.hello = hello;
const shouldInclude = false;
if (shouldInclude) {
exports.other = `other`;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment