Create a require that just has a {} on it and use that as your global:
//context.js const context={} module.exports = {context}
//someModule.js const {context} = require('./context'); if(context.foo) { context.foo(); }
//oh-look-more-code.js const {context} = require('./context'); context.foo = ()=>console.log("Yep");
//something.mjs import * as Context from 'context' Context.context.foo && Context.context.foo()
Bear in mind that in ES6 all imports are hoisted, so you will need to configure foo in an import or before all of this code executes. Can't have it inline like in cjs.
Thanks a lot!! This is perfect!!
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Create a require that just has a {} on it and use that as your global:
Bear in mind that in ES6 all imports are hoisted, so you will need to configure foo in an import or before all of this code executes. Can't have it inline like in cjs.
Thanks a lot!!
This is perfect!!