Gathering detailed insights and metrics for module-from-string
Gathering detailed insights and metrics for module-from-string
Gathering detailed insights and metrics for module-from-string
Gathering detailed insights and metrics for module-from-string
npm install module-from-string
Typescript
Module System
Min. Node Version
Node Version
NPM Version
TypeScript (93.32%)
JavaScript (6.68%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
52 Stars
252 Commits
4 Forks
1 Watchers
1 Branches
2 Contributors
Updated on May 15, 2025
Latest Version
3.3.1
Package Id
module-from-string@3.3.1
Unpacked Size
51.93 kB
Size
9.77 kB
File Count
15
NPM Version
10.7.0
Node Version
18.20.4
Published on
Jul 29, 2024
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
Load module from string using
require
orimport
.
1npm install module-from-string
1import { 2 requireFromString, 3 importFromString, 4 importFromStringSync 5} from 'module-from-string' 6 7requireFromString("module.exports = 'hi'") // => 'hi' 8requireFromString("exports.greet = 'hi'") // => { greet: 'hi' } 9 10;(async () => { 11 await importFromString("export default 'hi'") // => { default: 'hi' } 12})() 13 14importFromStringSync( 15 "export const greet = Buffer.from([0x68, 0x69]).toString('utf8')", 16 { globals: { Buffer } } 17) // => { greet: 'hi' }
1import { Context } from 'vm'; 2import { TransformOptions } from 'esbuild'; 3 4interface Options { 5 filename?: string 6 dirname?: string 7 globals?: Context 8 useCurrentGlobal?: boolean 9} 10 11declare const requireFromString: (code: string, options?: Options) => any 12declare const createRequireFromString: (options?: Options) => typeof requireFromString 13 14interface ImportOptions extends Options { 15 transformOptions?: TransformOptions 16} 17 18declare const importFromString: (code: string, options?: ImportOptions) => Promise<any> 19declare const createImportFromString: (options?: ImportOptions) => typeof importFromString 20 21declare const importFromStringSync: (code: string, options?: ImportOptions) => any 22declare const createImportFromStringSync: (options?: ImportOptions) => typeof importFromStringSync
Name, path or URL string of the virtual file for better exception stack trace.
1requireFromString( 2 "throw new Error('boom!')", 3 { filename: '/home/foo.js' } 4) 5// /home/foo.js:1 6// throw new Error('boom!') 7// ^ 8// 9// Error: boom! 10// at /home/foo.js:1:7 11// at ...
Path or URL string of the directory for resolving require
or import
from relative path.
1requireFromString(
2 "module.exports = require('.')",
3 { dirname: path.join(__dirname, "../lib") }
4) // => require('../lib')
If not specified, the default value will be the current file's directory.
Underneath the hood, module-from-string
uses Node.js built-in vm
module to execute code.
1// requireFromString
2
3vm.runInNewContext(
4 code,
5 {
6 __dirname: contextModule.path,
7 __filename: contextModule.filename,
8 exports: contextModule.exports,
9 module: contextModule,
10 require: contextModule.require,
11 ...globals
12 },
Take requireFromString
for example, only the module scope variables are passed into the contextObject
.
In order to use other global objects that are specific to Node.js, they need to be added to option globals
or set option useCurrentGlobal
to true
.
1requireFromString(
2 'module.exports = process.cwd()',
3 { globals: { process } }
4) // => $PWD
Note: by default the built-in objects have a different prototype.
1const error = requireFromString('module.exports = new Error()') 2error instanceof Error // => false
Default to false
. If set to true
, all the available variables from the current global
(or globalThis
) are passed into the context.
Function importFromString
and importFromStringSync
can use esbuild
to transform code syntax. See esbuild Transform API for documentation.
1const { greet } = importFromStringSync( 2 "export const greet: () => string = () => 'hi'", 3 { transformOptions: { loader: 'ts' } } 4) 5 6greet() // => 'hi'
Dynamic import()
expression of ES modules is supported by all three functions requireFromString
, importFromString
and importFromStringSync
.
1;(async () => { 2 await requireFromString("module.exports = import('./index.mjs')") 3})()
import
statement of ES modules is supported only by asynchronous function importFromString
using Node.js experimental API vm.Module
.
1node --experimental-vm-modules index.js 2 3# Or use environment variable 4NODE_OPTIONS=--experimental-vm-modules node index.js
1// with top-level await 2 3await importFromString("export { foo as default } from './index.mjs'")
MIT License © 2021 Exuanbo
7.5/10
Summary
module-from-string prototype pollution
Affected Versions
<= 3.3.1
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
license file detected
Details
Reason
dependency not pinned by hash detected -- score normalized to 3
Details
Reason
7 existing vulnerabilities detected
Details
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
Found 0/30 approved changesets -- score normalized to 0
Reason
no SAST tool detected
Details
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
security policy file not detected
Details
Score
Last Scanned on 2025-07-07
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More