New:Socket for Asana Is Now Available.Learn more
Get Started

import-module-string

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

import-module-string

Use import('data:') and import(Blob) to execute arbitrary JavaScript strings

Source
npmnpm
Version
1.0.3
Version published
Weekly downloads
891
-51.23%
Maintainers
1
Weekly downloads
 
Created
Source

import-module-string

Use import('data:') and import(Blob) to execute arbitrary JavaScript strings. A simpler alternative to node-retrieve-globals that works in more runtimes.

Installation

Available on npm as import-module-string.

npm install import-module-string

Features

  • Multi-runtime: tested with Node (18+), Deno (limited), Chromium, Firefox, and WebKit.
  • Defers to export when used, otherwise implicitly export all globals (via var, let, const, function, Array or Object destructuring assignment, import specifiers, etc)
  • Supports top-level async/await (as expected for ES modules)
  • Emulates import.meta.url when filePath option is supplied
  • addRequire option adds support for require() (in Node)
  • Extremely limited dependency footprint (acorn for JS parsing only)
  • Allows providing an external data object to pass in data (must be JSON.stringify friendly)

Limitations

  • import (in Browser) targets must be mapped via an Import Map in HTML.
  • import (in Node) targets are limited to built-in modules only.
  • Context data passed to script must be JSON.stringify-friendly (more serialization options may be added later).
  • Script content size maximums: Chrome 512MB, Safari 2048MB, Firefox 512MB, Firefox prior to v137 32MB

Usage

Import the script first!

import { importFromString } from "import-module-string";

View the test suite file for more examples.

Export

await importFromString(`export var a = 1;
export const c = 3;
export let b = 2;`);

// Returns
{ a: 1, c: 3, b: 2 }

No export

import { importFromString } from "import-module-string";

await importFromString(`var a = 1;
const c = 3;
let b = 2;`);

// Returns
{ a: 1, c: 3, b: 2 }

Pass in data

let data = { b: 2 };
await importFromString("const a = b;", { data });

// Returns
{ a: 2 }

Pass in filePath

let data = { b: 2 };
await importFromString("const a = import.meta.url;", { filePath: import.meta.url });

// Returns value for import.meta.url, example shown
{ a: `file:///…` }

FAQs

Package last updated on 02 May 2025

Related posts