Load module from string using require or import.
- Support ESM and CJS environments
- Support dynamic import
- Support
import.meta.url
- Support access to global variables
- No asynchronous IO operations
- No module cache
npm install import-from-string
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!
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();
Type: string
The code to import.
Type: string
Default: {Date.now()}.js
The virtual file name of the code to import.
Type: string
Default: The directory where the function is called
The virtual directory to import the code into.
Type: TransformOptions
esbuild transform options.
Type: BuildOptions
esbuild options.
Type: boolean
Default: false
skip esbuild build.
Type: string
The code to import.
Type: string
Default: {Date.now()}.js
The virtual file name of the code to import.
Type: string
Default: The directory where the function is called
The virtual directory to import the code into.
Type: string[]
Default: []
An array of additional paths to append when resolving modules.
Type: string[]
Default: []
An array of additional paths to prepend when resolving modules.
MIT License © 2023-Present Condor Hero