{"id":14156339,"url":"https://github.com/zachleat/node-retrieve-globals","last_synced_at":"2025-07-21T21:35:09.891Z","repository":{"id":65965511,"uuid":"603602315","full_name":"zachleat/node-retrieve-globals","owner":"zachleat","description":"Execute a string of JavaScript using Node.js and return the global variable values and functions.","archived":false,"fork":false,"pushed_at":"2025-04-17T20:42:32.000Z","size":49,"stargazers_count":11,"open_issues_count":2,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-06-22T15:47:01.019Z","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":"mit","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":"LICENSE","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}},"created_at":"2023-02-19T02:24:09.000Z","updated_at":"2025-04-17T20:41:36.000Z","dependencies_parsed_at":"2024-01-09T21:42:11.836Z","dependency_job_id":"762de5d1-cc4b-4281-814e-6bbc04a2dc21","html_url":"https://github.com/zachleat/node-retrieve-globals","commit_stats":{"total_commits":28,"total_committers":1,"mean_commits":28.0,"dds":0.0,"last_synced_commit":"cfff3d4b10eda9466215c0698a94a45fde07fd40"},"previous_names":[],"tags_count":16,"template":false,"template_full_name":null,"purl":"pkg:github/zachleat/node-retrieve-globals","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zachleat%2Fnode-retrieve-globals","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zachleat%2Fnode-retrieve-globals/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zachleat%2Fnode-retrieve-globals/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zachleat%2Fnode-retrieve-globals/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zachleat","download_url":"https://codeload.github.com/zachleat/node-retrieve-globals/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zachleat%2Fnode-retrieve-globals/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266383687,"owners_count":23920948,"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-07-21T11:47:31.412Z","response_time":64,"last_error":null,"robots_txt_status":null,"robots_txt_updated_at":null,"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":"2024-08-17T08:05:23.428Z","updated_at":"2025-07-21T21:35:09.882Z","avatar_url":"https://github.com/zachleat.png","language":"JavaScript","funding_links":[],"categories":["others"],"sub_categories":[],"readme":"# node-retrieve-globals\n\nExecute a string of JavaScript using Node.js and return the global variable values and functions.\n\n* Supported on Node.js 16 and newer.\n* Uses `var`, `let`, `const`, `function`, Array and Object [destructuring assignment](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment).\n* Async-only as of v5.0.\n* Can return any valid JS data type (including functions).\n* Can provide an external data object as context to the local execution scope\n* Transforms ESM import statements to work with current CommonJS limitations in Node’s `vm`.\n* Uses [Node’s `vm` module to execute JavaScript](https://nodejs.org/api/vm.html#vmruninthiscontextcode-options)\n\t* ⚠️ The `node:vm` module is not a security mechanism. Do not use it to run untrusted code.\n\t* `codeGeneration` (e.g. `eval`) is disabled by default; use `setCreateContextOptions({codeGeneration: { strings: true, wasm: true } })` to re-enable.\n\t* Works _with or without_ `--experimental-vm-modules` flag (for `vm.Module` support). _(v5.0.0 and newer)_\n\t* Future-friendly feature tests for when `vm.Module` is stable and `--experimental-vm-modules` is no longer necessary. _(v5.0.0 and newer)_\n* In use on:\n\t* [JavaScript in Eleventy Front Matter](https://www.11ty.dev/docs/data-frontmatter-customize/#example-use-javascript-in-your-front-matter) (and [Demo](https://github.com/11ty/demo-eleventy-js-front-matter))\n\t* [WebC’s `\u003cscript webc:setup\u003e`](https://www.11ty.dev/docs/languages/webc/#using-java-script-to-setup-your-component)\n\n## Installation\n\nAvailable on [npm](https://www.npmjs.com/package/node-retrieve-globals)\n\n```\nnpm install node-retrieve-globals\n```\n\n## Usage\n\nWorks from Node.js with ESM and CommonJS:\n\n```js\nimport { RetrieveGlobals } from \"node-retrieve-globals\";\n// const { RetrieveGlobals } = await import(\"node-retrieve-globals\");\n```\n\nAnd then:\n\n```js\nlet code = `var a = 1;\nconst b = \"hello\";\n\nfunction hello() {}`;\n\nlet vm = new RetrieveGlobals(code);\n\nawait vm.getGlobalContext();\n```\n\nReturns:\n\n```js\n{ a: 1, b: \"hello\", hello: function hello() {} }\n```\n\n### Pass in your own Data and reference it in the JavaScript code\n\n```js\nlet code = `let ref = myData;`;\n\nlet vm = new RetrieveGlobals(code);\n\nawait vm.getGlobalContext({ myData: \"hello\" });\n```\n\nReturns:\n\n```js\n{ ref: \"hello\" }\n```\n\n### Advanced options\n\n```js\n// Defaults shown\nlet options = {\n\treuseGlobal: false, // re-use Node.js `global`, important if you want `console.log` to log to your console as expected.\n\tdynamicImport: false, // allows `import()`\n\taddRequire: false, // allows `require()`\n\texperimentalModuleApi: false, // uses Module#_compile instead of `vm` (you probably don’t want this and it is bypassed by default when vm.Module is supported)\n};\n\nawait vm.getGlobalContext({}, options);\n```\n\n## Changelog\n\n* `v6.0.0` Changes `import` and `require` to be project relative (not relative to this package on the file system).\n* `v5.0.0` Removes sync API, swap to async-only. Better compatibility with `--experimental-vm-modules` Node flag.\n* `v4.0.0` Swap to use `Module._compile` as a workaround for #2 (Node regression with experimental modules API in Node v20.10+)\n* `v3.0.0` ESM-only package. Node 16+","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzachleat%2Fnode-retrieve-globals","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzachleat%2Fnode-retrieve-globals","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzachleat%2Fnode-retrieve-globals/lists"}