dynamic-imports
Import / require dynamically from string sources.
dynamic-imports is a wrapper on top of vm.Script and vm.Module for require/provide and import/export correspondingly.
Currently only a subset of features of vm are implemented as I am trying to create consistent and simple API, more features such as module, script and context options might be added in the future
Note that nested imports are currently not allowed in exported module sources, so the following will throw an error:
dynamicImports
Also note that using import
/export
functionality requires Node 9.6.0, as well as running it with --experimental-vm-modules
and --experimental-modules
, like so:
node --experimental-vm-modules --experimental-modules your-file.js
Current API:
The most basic usage is as follows:
dynamicImports console// { default: 'wow', more: 'such more', stuff: 'very stuff' } dynamicImports console// { default: 'wow', more: 'such more', stuff: 'very stuff' }
You can also provide context sandboxes to modules
Do note how sandbox.doge
reacts to changes in all scopes, since you are sharing it between contexts
const sandbox = doge: 'wow' dynamicImports console// 'wow'console// 'nova'console// 'nova' dynamicImports console// 'nova'console// 'supernova'console// 'supernova' console// 'supernova'