Skip to content

Instantly share code, notes, and snippets.

@mfdj
Last active June 7, 2021 21:28
Show Gist options
  • Save mfdj/d7c443bf7c387ce92463adb77d16cf6f to your computer and use it in GitHub Desktop.
Save mfdj/d7c443bf7c387ce92463adb77d16cf6f to your computer and use it in GitHub Desktop.
ECMAScript modules (ESM): Yes, when you export an object the reference is shared across all usages
$ node index.mjs
🌞 module-a pre-export { name: 'module-a 🌞', obj: { a: 'πŸ‡¨πŸ‡¦', b: 'bee', c: 'see' } }
πŸ‘» module-b pre-export { name: 'module-b πŸ‘»', obj: { a: 'πŸ‡¨πŸ‡¦', b: '🐝', c: 'see' } }
{
  moduleA: { name: 'module-a 🌞', obj: { a: 'πŸ‡¨πŸ‡¦', b: '🐝', c: 'πŸ‘€' } },
  moduleB: { name: 'module-b πŸ‘»', obj: { a: 'πŸ‡¨πŸ‡¦', b: '🐝', c: 'πŸ‘€' } },
  moduleObjectExport: { a: 'πŸ‡¨πŸ‡¦', b: '🐝', c: 'πŸ‘€' }
}
import moduleA from './module-a.mjs';
import moduleB from './module-b.mjs';
import moduleObjectExport from './module-object-export.mjs';
moduleObjectExport.c = 'πŸ‘€';
console.log({ moduleA, moduleB, moduleObjectExport });
import obj from './module-scope-object-export.mjs';
obj.a = 'πŸ‡¨πŸ‡¦';
const result = {
name: 'module-a 🌞',
obj
};
console.log('🌞 module-a pre-export', result);
export default result;
import obj from './module-scope-object-export.mjs';
obj.b = '🐝';
const result = {
name: 'module-b πŸ‘»',
obj
};
console.log('πŸ‘» module-b pre-export', result);
export default result;
export default {
a: 'eh',
b: 'bee',
c: 'see',
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment