# @peggyjs/from-mem

> Load a string as if it was an esm or commonjs module with a given filename.

Latest version **3.1.4** (published 2026-06-07) · MIT license · 0 weekly downloads

## Install

```sh
npm install @peggyjs/from-mem
pnpm add @peggyjs/from-mem
yarn add @peggyjs/from-mem
bun add @peggyjs/from-mem
```

## Health

**Score 55/100 (C)** — status: active.

Positive: has types; no vulnerabilities; has provenance.

Warnings: low downloads; no esm support.

## Facts

| | |
|---|---|
| Version | 3.1.4 |
| Published | 2026-06-07 |
| First published | 2024-02-17 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >=20.8 |
| Dependencies | 1 |
| Unpacked size | 31.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 1 |
| Author | Joe Hildebrand |
| Maintainers | charlespick, hildjj |
| Keywords | vm, module, esm, eval |

## Links

- npm: https://www.npmjs.com/package/@peggyjs/from-mem
- Repository: https://github.com/peggyjs/from-mem
- Homepage: https://github.com/peggyjs/from-mem#readme
- Issues: https://github.com/peggyjs/from-mem/issues
- npm.io page: https://npm.io/package/@peggyjs/from-mem

## Dependencies (1)

- [semver](https://npm.io/package/semver.md) ~7.8.2

## Recent versions

- 3.1.4 (latest) — 2026-06-07
- 3.1.3 — 2026-02-28
- 3.1.2 — 2025-12-15
- 3.1.1 — 2025-08-05
- 3.1.0 — 2025-07-15
- 3.0.3 — 2025-07-14
- 3.0.2 — 2025-07-13
- 3.0.1 — 2025-07-13
- 3.0.0 — 2025-07-12
- 2.0.3 — 2025-07-11
- 2.0.2 — 2025-07-10
- 2.0.1 — 2025-06-20
- 2.0.0 — 2025-05-01
- 1.4.0 — 2025-04-11
- 1.3.5 — 2024-10-10
- … 7 more at https://npm.io/package/@peggyjs/from-mem/versions

## README

# @peggyjs/from-mem

Execute some text in memory as if it was a file with a given name, so that all
of the imports/require's happen out of that directory's associated
node_modules directory or relative to where the file would have been.

This is NOT intended to be a security boundary.  In particular, all files
will be required or imported through the node module cache.

This code was originally a part of [peggy](https://peggyjs.org/), but was
refactored out when it was needed in a related project.  Several ideas in this
code came from the
[module-from-string](https://github.com/exuanbo/module-from-string) and
[eval](https://github.com/pierrec/node-eval) modules -- thanks to those authors.

## Installation

```sh
npm install @peggyjs/from-mem
```

## Usage

```js
import fromMem from "@peggyjs/from-mem"; // or require("@peggyjs/from-mem")
const consoleOutput = {};
const result = await fromMem(`
import foo from "../foo.js"; // Loads ./test/foo.js

console.log("hi");
export async function bar() {
  return await foo() + 2;
}
`, {
  filename: path.join(__dirname, "test", "fixtures", "test.js"),
  format: "es",
  consoleOutput,
  exec: `
console.log("there");
const b = await IMPORTED.bar();
return b + arg;
`;
  arg: 12,
});

// consoleOutput.out === "hi\nthere\n"
```

`filename` is the only required option, unless you are processing an ES6
module and your runtime does not have --experimental_vm_modules set.  In that
case, you must pass in an `exec` option, and may pass in an `arg` also.

Full [API docs](http://peggyjs.github.io/from-mem/) are available.

## Caveats

- This module has a strong requirement for node 20.8+ at runtime when using
  the es6 format, due to a bug that crashes node in node's vm module that got
  fixed there and in 21.0.  There is a runtime check to prevent the crash.

[![Tests](https://github.com/peggyjs/from-mem/actions/workflows/node.js.yml/badge.svg)](https://github.com/peggyjs/from-mem/actions/workflows/node.js.yml)
[![codecov](https://codecov.io/gh/peggyjs/from-mem/graph/badge.svg?token=CWQ7GSH0ZI)](https://codecov.io/gh/peggyjs/from-mem)

---
_Source: https://npm.io/package/@peggyjs/from-mem · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
