npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

import-module-string

v2.0.3

Published

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

Downloads

2,688

Readme

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)
  • Supports data object to pass in data (must be JSON.stringify friendly, more serialization options may be added later)
  • Subject to URL content size maximums: Chrome 512MB, Safari 2048MB, Firefox 512MB, Firefox prior to v137 32MB

|Feature|Server|Browser| |---|---|---| |import('./file.js')|✅|✅ (Import Map-friendly)| |import('bare')|✅|✅ (Import Map-friendly)| |import('built-in')|✅|N/A| |require()|✅ with addRequire option|❌| |import.meta.url|✅ with filePath option|✅ with filePath option|

Notes:

  • built-in modules are provided by the JavaScript runtime. node:fs is one example.
  • bare specifiers are packages referenced by their bare name. In Node this might be a package installed from npm.

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

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

// Returns
{ a: 2 }

Pass in filePath

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

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

Imports (experimental feature)

Relative references

// `dependency.js` has the content `export default 2;`
await importFromString("import dep from './dependency.js';");

// Returns
{ dep: 2 }

Bare references

Uses import.meta.resolve to resolve paths, which will also resolve using Import Maps (where available).

// maps with `import.meta.resolve("@zachleat/noop"))` in-browser (Import Map friendly)
await importFromString("import {noop} from '@zachleat/noop';");

// Returns
{ noop: function() {} }

Builtins

await importFromString("import fs from 'node:fs';");

// Returns (where available: `node:fs` is not typically available in browser)
{ fs: { /* … */ } }

As a side note, you can shim fs into the browser with memfs.

Changelog

  • v2.0.0 removes adapter (no longer necessary!)
  • v1.0.5 bug fixes
  • v1.0.4 add adapter option (add adapter: "fs" or adapter: "fetch") to resolve imports in various environments.