{"id":31646814,"url":"https://github.com/zachleat/javascript-eval-modules","last_synced_at":"2025-10-07T05:56:20.102Z","repository":{"id":297511508,"uuid":"860082869","full_name":"zachleat/javascript-eval-modules","owner":"zachleat","description":"Playground for testing various dynamic script execution methods in JavaScript","archived":false,"fork":false,"pushed_at":"2025-06-05T21:43:44.000Z","size":28,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-06-05T22:23:58.066Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/zachleat.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2024-09-19T19:39:26.000Z","updated_at":"2025-06-05T21:55:56.000Z","dependencies_parsed_at":"2025-06-06T19:34:41.633Z","dependency_job_id":null,"html_url":"https://github.com/zachleat/javascript-eval-modules","commit_stats":null,"previous_names":["zachleat/javascript-eval-modules"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/zachleat/javascript-eval-modules","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zachleat%2Fjavascript-eval-modules","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zachleat%2Fjavascript-eval-modules/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zachleat%2Fjavascript-eval-modules/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zachleat%2Fjavascript-eval-modules/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zachleat","download_url":"https://codeload.github.com/zachleat/javascript-eval-modules/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zachleat%2Fjavascript-eval-modules/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":278728316,"owners_count":26035412,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-07T02:00:06.786Z","response_time":59,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-10-07T05:56:14.225Z","updated_at":"2025-10-07T05:56:20.096Z","avatar_url":"https://github.com/zachleat.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Dynamic Script Evaluation in JavaScript\n\nYou 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 support.\n\nThis is research for [`import-module-string`](https://github.com/zachleat/import-module-string) (the approach I currently use and recommend for ESM code) and [`node-retrieve-globals`](https://github.com/zachleat/node-retrieve-globals/) (legacy but more CommonJS friendly), whose features I have documented in a separate compatibility table below.\n\n* [`vm` Node.js documentation](https://nodejs.org/docs/latest/api/vm.html)\n* [MDN docs for `eval`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval)\n* [`import(\"data:…\")` approach from `2ality.com`](https://2ality.com/2019/10/eval-via-import.html)\n* [`import(\"blob:…\")` approach suggested by David Bushnell](https://github.com/dbushell/dinossr/blob/main/src/bundle/import.ts#L13) (not currently supported in Node.js but works in Deno!)\n\n## Runtime Native Methods\n\n\u003ctable\u003e\n  \u003cthead\u003e\n    \u003ctr\u003e\n      \u003cth\u003eJavaScript Feature\u003c/th\u003e\n      \u003cth\u003e\u003ccode\u003eModule#_compile\u003c/code\u003e\u003c/th\u003e\n      \u003cth\u003e\u003ccode\u003eFunction()\u003c/code\u003e\u003c/th\u003e\n      \u003cth\u003e\u003ccode\u003evm.Script\u003c/code\u003e\u003c/th\u003e\n      \u003cth\u003e\u003ccode\u003evm.Module\u003c/code\u003e\u003c/th\u003e\n      \u003cth\u003e\u003ccode\u003eimport(\"data:…\")\u003c/code\u003e (or \u003ccode\u003eblob:…\u003c/code\u003e)\u003c/th\u003e\n    \u003c/tr\u003e\n  \u003c/thead\u003e\n  \u003ctbody\u003e\n    \u003ctr\u003e\n      \u003ctd\u003eAssign \u003ccode\u003emodule.exports\u003c/code\u003e (CommonJS-only)\u003c/td\u003e\n      \u003ctd\u003eYes\u003c/td\u003e\n      \u003ctd\u003eNo\u003c/td\u003e\n      \u003ctd\u003eYes\u003c/td\u003e\n      \u003ctd\u003eNo\u003csup\u003e5\u003c/sup\u003e\u003c/td\u003e\n      \u003ctd\u003eNo\u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n      \u003ctd\u003e\u003ccode\u003eexport\u003c/code\u003e (ESM-only)\u003c/td\u003e\n      \u003ctd\u003eNo\u003c/td\u003e\n      \u003ctd\u003eNo\u003c/td\u003e\n      \u003ctd\u003eNo\u003c/td\u003e\n      \u003ctd\u003eYes\u003csup\u003e1\u003c/sup\u003e\u003c/td\u003e\n      \u003ctd\u003eYes\u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n      \u003ctd\u003e\u003ccode\u003erequire\u003c/code\u003e\u003c/td\u003e\n      \u003ctd\u003eYes\u003c/td\u003e\n      \u003ctd\u003eYes\u003c/td\u003e\n      \u003ctd\u003eYes\u003c/td\u003e\n      \u003ctd\u003eYes\u003csup\u003e1\u003c/sup\u003e\u003c/td\u003e\n      \u003ctd\u003eNo\u003csup\u003e6\u003c/sup\u003e\u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n      \u003ctd\u003e\u003ccode\u003eimport\u003c/code\u003e (ESM-only)\u003c/td\u003e\n      \u003ctd\u003eNo\u003csup\u003e4\u003c/sup\u003e\u003c/td\u003e\n      \u003ctd\u003eNo\u003csup\u003e4\u003c/sup\u003e\u003c/td\u003e\n      \u003ctd\u003eNo\u003csup\u003e4\u003c/sup\u003e\u003c/td\u003e\n      \u003ctd\u003eYes\u003csup\u003e1\u003c/sup\u003e\u003c/td\u003e\n      \u003ctd\u003eNo\u003csup\u003e7\u003c/sup\u003e\u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n      \u003ctd\u003eDynamic \u003ccode\u003eimport()\u003c/code\u003e\u003c/td\u003e\n      \u003ctd\u003eYes\u003c/td\u003e\n      \u003ctd\u003eYes\u003c/td\u003e\n      \u003ctd\u003eYes\u003csup\u003e2\u003c/sup\u003e\u003c/td\u003e\n      \u003ctd\u003eYes\u003csup\u003e1\u003c/sup\u003e\u003c/td\u003e\n      \u003ctd\u003eNo\u003csup\u003e7\u003c/sup\u003e\u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n      \u003ctd\u003eTop level \u003ccode\u003easync\u003c/code\u003e or \u003ccode\u003eawait\u003c/code\u003e\u003c/td\u003e\n      \u003ctd\u003eFaux\u003csup\u003e3\u003c/sup\u003e\u003c/td\u003e\n      \u003ctd\u003eFaux\u003csup\u003e3\u003c/sup\u003e\u003c/td\u003e\n      \u003ctd\u003eFaux\u003csup\u003e3\u003c/sup\u003e\u003c/td\u003e\n      \u003ctd\u003eYes\u003csup\u003e1\u003c/sup\u003e\u003c/td\u003e\n      \u003ctd\u003eYes\u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n      \u003ctd\u003e\u003cem\u003eCan\u003c/em\u003e leak to global scope\u003c/td\u003e\n      \u003ctd\u003eYes\u003c/td\u003e\n      \u003ctd\u003eYes\u003c/td\u003e\n      \u003ctd\u003eNo\u003c/td\u003e\n      \u003ctd\u003eNo\u003c/td\u003e\n      \u003ctd\u003eYes\u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n      \u003ctd\u003eRuntime Compatibility\u003c/td\u003e\n      \u003ctd\u003eNode.js\u003c/td\u003e\n      \u003ctd\u003e\u003cem\u003eAny\u003c/em\u003e\u003c/td\u003e\n      \u003ctd\u003eNode.js\u003c/td\u003e\n      \u003ctd\u003eNode.js\u003c/td\u003e\n      \u003ctd\u003e\u003cem\u003eAny\u003c/em\u003e\u003c/td\u003e\n    \u003c/tr\u003e\n  \u003c/tbody\u003e\n\u003c/table\u003e\n\nNotes:\n\n1. Requires `--experimental-vm-modules`. Use outputs an `ExperimentalWarning` to the console.\n2. 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.\n3. Requires the code to be wrapped in an `(async () =\u003e {})()` IIFE wrapper.\n4. Can use `esm-import-transformer` to transform static `import` to dynamic `import()` or `require`: https://github.com/zachleat/esm-import-transformer\n5. Probably shimmable but not worth it.\n6. `require` is shimmable in Node.js via [`node:module#createRequire`](https://nodejs.org/docs/latest/api/module.html#modulecreaterequirefilename)\n7. `import` of runtime built-ins (e.g. `node:` prefixed modules in Node.js) are allowed.\n\n## npm Packages\n\n\u003ctable\u003e\n  \u003cthead\u003e\n    \u003ctr\u003e\n      \u003cth\u003eJavaScript Feature\u003c/th\u003e\n      \u003cth\u003e\u003ccode\u003enpm:node-retrieve-globals\u003c/code\u003e\u003c/th\u003e\n      \u003cth\u003e\u003ccode\u003enpm:import-module-string\u003c/code\u003e\u003c/th\u003e\n    \u003c/tr\u003e\n  \u003c/thead\u003e\n  \u003ctbody\u003e\n    \u003ctr\u003e\n      \u003ctd\u003eAssign \u003ccode\u003emodule.exports\u003c/code\u003e (CommonJS-only)\u003c/td\u003e\n      \u003ctd\u003eYes\u003c/td\u003e\n      \u003ctd\u003eNo\u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n      \u003ctd\u003e\u003ccode\u003eexport\u003c/code\u003e (ESM-only)\u003c/td\u003e\n      \u003ctd\u003eNo\u003c/td\u003e\n      \u003ctd\u003eYes\u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n      \u003ctd\u003e\u003ccode\u003erequire\u003c/code\u003e\u003c/td\u003e\n      \u003ctd\u003eYes\u003c/td\u003e\n      \u003ctd\u003eYes\u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n      \u003ctd\u003e\u003ccode\u003eimport\u003c/code\u003e (ESM-only)\u003c/td\u003e\n      \u003ctd\u003eNo\u003c/td\u003e\n      \u003ctd\u003eYes\u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n      \u003ctd\u003eDynamic \u003ccode\u003eimport()\u003c/code\u003e\u003c/td\u003e\n      \u003ctd\u003eYes\u003c/td\u003e\n      \u003ctd\u003eYes\u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n      \u003ctd\u003eTop level \u003ccode\u003easync\u003c/code\u003e or \u003ccode\u003eawait\u003c/code\u003e\u003c/td\u003e\n      \u003ctd\u003eFaux\u003csup\u003e3\u003c/sup\u003e\u003c/td\u003e\n      \u003ctd\u003eYes\u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n      \u003ctd\u003e\u003cem\u003eCan\u003c/em\u003e leak to global scope\u003c/td\u003e\n      \u003ctd\u003eYes\u003c/td\u003e\n      \u003ctd\u003eYes\u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n      \u003ctd\u003eRuntime Compatibility\u003c/td\u003e\n      \u003ctd\u003eNode.js\u003c/td\u003e\n      \u003ctd\u003e\u003cem\u003eAny\u003c/em\u003e\u003c/td\u003e\n    \u003c/tr\u003e\n  \u003c/tbody\u003e\n\u003c/table\u003e\n\n## Alternate methods\n\n* A lot of the pain here is due to an experimental `vm.Module`. If you already have access to a bundler (e.g. `esbuild`), use that to output CommonJS code and run it through `Module#_compile` to bypass current limitations with dynamic ESM in Node.js.\n\t* [Vite writes a temporary file to the filesystem](https://github.com/vitejs/vite/blob/77d5165e2f252bfecbb0eebccc6f04dc8be0c5ba/packages/vite/src/node/config.ts#L1172-L1184) to workaround this issue.\n\t* Some [more discussion on Mastodon](https://fediverse.zachleat.com/@zachleat/111580482330587997).\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzachleat%2Fjavascript-eval-modules","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzachleat%2Fjavascript-eval-modules","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzachleat%2Fjavascript-eval-modules/lists"}