@peggyjs/from-mem
Load a string as if it was an esm or commonjs module with a given filename.
Last updated a month ago by GitHub Actions .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install @peggyjs/from-mem 
SYNC missed versions from official npm registry.

@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, but was refactored out when it was needed in a related project. Several ideas in this code came from the module-from-string and eval modules -- thanks to those authors.

Installation

npm install @peggyjs/from-mem

Usage

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 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 codecov

Current Tags

  • 3.1.3                                ...           latest (a month ago)

21 Versions

  • 3.1.3                                ...           a month ago
  • 3.1.2                                ...           4 months ago
  • 3.1.1                                ...           8 months ago
  • 3.1.0                                ...           9 months ago
  • 3.0.3                                ...           9 months ago
  • 3.0.2                                ...           9 months ago
  • 3.0.1                                ...           9 months ago
  • 3.0.0                                ...           9 months ago
  • 2.0.3                                ...           9 months ago
  • 2.0.2                                ...           9 months ago
  • 2.0.1                                ...           10 months ago
  • 2.0.0                                ...           a year ago
  • 1.4.0                                ...           a year ago
  • 1.3.5                                ...           2 years ago
  • 1.3.4                                ...           2 years ago
  • 1.3.3                                ...           2 years ago
  • 1.3.0                                ...           2 years ago
  • 1.2.1                                ...           2 years ago
  • 1.2.0                                ...           2 years ago
  • 1.1.0                                ...           2 years ago
  • 1.0.0                                ...           2 years ago
Maintainers (2)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (1)
Dev Dependencies (0)
None
Dependents (1)

Copyright 2013 - present © cnpmjs.org | Home |