1

I am importing some modules

 import foo from 'foo.js'
 import bar from 'bar.js'

I want to be able to select them dynamically, using a string to refer to their name, so instead of:

setConfig(foo)

I want to do something like:

setConfig( context['foo'])

Is this possible? what is the context (ie. the equivalent of 'this', 'global', 'window') where the module might be resolved?

xeno.be
  • 91
  • 1
  • 10
  • 1
    I'm not sure what you mean by `scope`. Can you please add a clearer example? – briosheje Mar 21 '19 at 14:24
  • [How do I format my code blocks?](https://meta.stackoverflow.com/questions/251361/how-do-i-format-my-code-blocks) – Liam Mar 21 '19 at 14:26
  • *load them dynamically* doesn't make a lot of sense. You can't get a file when you need it. It'll also be slower and less efficient than just loading all the "files" up front – Liam Mar 21 '19 at 14:27
  • Does this answer your question? [In what scope are module variables stored in node.js?](https://stackoverflow.com/questions/15406062/in-what-scope-are-module-variables-stored-in-node-js) – HerrAlvé Jun 25 '22 at 08:39

1 Answers1

0

Think I've found an answer in this response to another question: https://stackoverflow.com/a/50440026/521543

And there is no way for direct access for module variables (except for imported modules):

Lexical Environments and Environment Record values are purely specification mechanisms and need not correspond to any specific artefact of an ECMAScript implementation. It is impossible for an ECMAScript program to directly access or manipulate such values.

Community
  • 1
  • 1
xeno.be
  • 91
  • 1
  • 10