Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

import-from-string

Package Overview
Dependencies
Maintainers
0
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

import-from-string

Load module from string using require or import.

  • 0.0.5
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
0
Created
Source

Import-from-string

NPM version Downloads License GitHub stars

Load module from string using require or import.

Features

  • Support ESM and CJS environments
  • Support dynamic import
  • Support import.meta.url
  • Support access to global variables
  • No asynchronous IO operations
  • No module cache

Install

npm install import-from-string

Usage

ESM

import { importFromString, requireFromString } from "import-from-string";

const cjs = requireFromString("module.exports = 'Hi World!'");
console.log(cjs); // Hi World!

const esm = await importFromString("export default 'Hello World!'");
console.log(esm.default); // Hello World!

CJS

const { requireFromString, importFromString } = require("import-from-string");

async function main() {
	const cjs = requireFromString("module.exports = 'Hi World!'");
	console.log(cjs); // Hi World!

	const esm = await importFromString("export default 'Hello World!'");
	console.log(esm.default); // Hello World!
}

main();

API

importFromString(code, options?)

code

Type: string

The code to import.

options
filename

Type: string
Default: {Date.now()}.js

The virtual file name of the code to import.

dirname

Type: string
Default: The directory where the function is called

The virtual directory to import the code into.

transformOptions

Type: TransformOptions

esbuild transform options.

esbuildOptions

Type: BuildOptions

esbuild options.

skipBuild

Type: boolean
Default: false

skip esbuild build.

requireFromString(code, options?)

code

Type: string

The code to import.

options
filename

Type: string
Default: {Date.now()}.js

The virtual file name of the code to import.

dirname

Type: string
Default: The directory where the function is called

The virtual directory to import the code into.

appendPaths

Type: string[]
Default: []

An array of additional paths to append when resolving modules.

prependPaths

Type: string[]
Default: []

An array of additional paths to prepend when resolving modules.

Appreciation

License

MIT License © 2023-Present Condor Hero

Keywords

FAQs

Package last updated on 06 Nov 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc