# node-retrieve-globals

> Execute a string of JavaScript using Node.js and return the global variable values and functions.

Latest version **6.0.1** (published 2025-04-17) · MIT license · 0 weekly downloads

## Install

```sh
npm install node-retrieve-globals
pnpm add node-retrieve-globals
yarn add node-retrieve-globals
bun add node-retrieve-globals
```

## Health

**Score 30/100 (F)** — status: maintenance-mode.

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 6.0.1 |
| Published | 2025-04-17 |
| First published | 2023-02-19 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 3 |
| Unpacked size | 15.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 12 |
| Author | Zach Leatherman |
| Maintainers | zachleat |

## Links

- npm: https://www.npmjs.com/package/node-retrieve-globals
- Repository: https://github.com/zachleat/node-retrieve-globals
- Homepage: https://github.com/zachleat/node-retrieve-globals#readme
- Issues: https://github.com/zachleat/node-retrieve-globals/issues
- npm.io page: https://npm.io/package/node-retrieve-globals

## Dependencies (3)

- [acorn](https://npm.io/package/acorn.md) ^8.14.1
- [acorn-walk](https://npm.io/package/acorn-walk.md) ^8.3.4
- [esm-import-transformer](https://npm.io/package/esm-import-transformer.md) ^3.0.3

## Recent versions

- 6.0.1 (latest) — 2025-04-17
- 6.0.0 — 2024-04-17
- 5.0.0 — 2023-12-15
- 4.0.0 — 2023-12-14
- 3.0.1 — 2023-11-03
- 3.0.0 — 2023-11-03
- 2.0.8 — 2023-04-19
- 2.0.7 — 2023-03-30
- 2.0.6 — 2023-03-11
- 2.0.5 — 2023-03-10
- 2.0.4 — 2023-02-21
- 2.0.3 — 2023-02-21
- 2.0.2 — 2023-02-19
- 2.0.1 — 2023-02-19
- 2.0.0 — 2023-02-19
- … 2 more at https://npm.io/package/node-retrieve-globals/versions

## README

# node-retrieve-globals

Execute a string of JavaScript using Node.js and return the global variable values and functions.

* Supported on Node.js 16 and newer.
* Uses `var`, `let`, `const`, `function`, Array and Object [destructuring assignment](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment).
* Async-only as of v5.0.
* Can return any valid JS data type (including functions).
* Can provide an external data object as context to the local execution scope
* Transforms ESM import statements to work with current CommonJS limitations in Node’s `vm`.
* Uses [Node’s `vm` module to execute JavaScript](https://nodejs.org/api/vm.html#vmruninthiscontextcode-options)
	* ⚠️ The `node:vm` module is not a security mechanism. Do not use it to run untrusted code.
	* `codeGeneration` (e.g. `eval`) is disabled by default; use `setCreateContextOptions({codeGeneration: { strings: true, wasm: true } })` to re-enable.
	* Works _with or without_ `--experimental-vm-modules` flag (for `vm.Module` support). _(v5.0.0 and newer)_
	* Future-friendly feature tests for when `vm.Module` is stable and `--experimental-vm-modules` is no longer necessary. _(v5.0.0 and newer)_
* In use on:
	* [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))
	* [WebC’s `<script webc:setup>`](https://www.11ty.dev/docs/languages/webc/#using-java-script-to-setup-your-component)

## Installation

Available on [npm](https://www.npmjs.com/package/node-retrieve-globals)

```
npm install node-retrieve-globals
```

## Usage

Works from Node.js with ESM and CommonJS:

```js
import { RetrieveGlobals } from "node-retrieve-globals";
// const { RetrieveGlobals } = await import("node-retrieve-globals");
```

And then:

```js
let code = `var a = 1;
const b = "hello";

function hello() {}`;

let vm = new RetrieveGlobals(code);

await vm.getGlobalContext();
```

Returns:

```js
{ a: 1, b: "hello", hello: function hello() {} }
```

### Pass in your own Data and reference it in the JavaScript code

```js
let code = `let ref = myData;`;

let vm = new RetrieveGlobals(code);

await vm.getGlobalContext({ myData: "hello" });
```

Returns:

```js
{ ref: "hello" }
```

### Advanced options

```js
// Defaults shown
let options = {
	reuseGlobal: false, // re-use Node.js `global`, important if you want `console.log` to log to your console as expected.
	dynamicImport: false, // allows `import()`
	addRequire: false, // allows `require()`
	experimentalModuleApi: false, // uses Module#_compile instead of `vm` (you probably don’t want this and it is bypassed by default when vm.Module is supported)
};

await vm.getGlobalContext({}, options);
```

## Changelog

* `v6.0.0` Changes `import` and `require` to be project relative (not relative to this package on the file system).
* `v5.0.0` Removes sync API, swap to async-only. Better compatibility with `--experimental-vm-modules` Node flag.
* `v4.0.0` Swap to use `Module._compile` as a workaround for #2 (Node regression with experimental modules API in Node v20.10+)
* `v3.0.0` ESM-only package. Node 16+

---
_Source: https://npm.io/package/node-retrieve-globals · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
