Skip to content

Playground for testing various dynamic script execution methods in Node.js

Notifications You must be signed in to change notification settings

zachleat/node-eval-modules

Repository files navigation

Script Evaluation in Node.js

You have a String of JavaScript code. How can you execute it? This is a playground for testing various dynamic script execution methods in Node.js and what features they may or may not support.

JavaScript Feature Module#_compile Function() vm.Script vm.Module import("data:…")
Assign module.exports (CommonJS-only) Yes No Yes No5 No
export (ESM-only) No No No Yes1 Yes
require Yes Yes Yes Yes1 No
import (ESM-only) No4 No4 No4 Yes1 No
Dynamic import() Yes Yes Yes2 Yes1 No
Top level async or await Faux3 Faux3 Faux3 Yes1 Yes
Can leak to global scope Yes Yes No No Yes

Notes:

  1. Requires --experimental-vm-modules. Use outputs an ExperimentalWarning to the console.
  2. Requires vm.constants.USE_MAIN_CONTEXT_DEFAULT_LOADER an experimental Node feature added in v21.7.0, v20.12.0. Any use outputs an ExperimentalWarning to the console.
  3. Requires the code to be wrapped in an (async () => {})() IIFE wrapper.
  4. Can use esm-import-transformer to transform static import to dynamic import() or require: https://github.com/zachleat/esm-import-transformer
  5. Probably shimmable but I think that would cause more confusion than it’s worth.

Alternate methods

About

Playground for testing various dynamic script execution methods in Node.js

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published