Research
Security News
Threat Actor Exposes Playbook for Exploiting npm to Build Blockchain-Powered Botnets
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.
module-from-string
Advanced tools
Load module from string using
require
orimport
.
npm install module-from-string
import {
requireFromString,
importFromString,
importFromStringSync
} from 'module-from-string'
requireFromString("module.exports = 'hi'") // => 'hi'
requireFromString("exports.greet = 'hi'") // => { greet: 'hi' }
;(async () => {
await importFromString("export default 'hi'") // => { default: 'hi' }
})()
importFromStringSync(
"export const greet = Buffer.from([0x68, 0x69]).toString('utf8')",
{ globals: { Buffer } }
) // => { greet: 'hi' }
import { TransformOptions } from 'esbuild'
interface Options {
dirname?: string
globals?: Record<string, unknown>
}
interface ImportOptions extends Options {
transformOptions?: TransformOptions
}
declare const requireFromString: (
code: string,
{ dirname, globals }?: Options
) => any
declare const importFromString: (
code: string,
{ dirname, globals, transformOptions }?: ImportOptions
) => Promise<any>
declare const importFromStringSync: (
code: string,
{ dirname, globals, transformOptions }?: ImportOptions
) => any
An absolute path of the directory for resolving require
or import
from relative path.
requireFromString("module.exports = require('.')", {
dirname: path.join(__dirname, "../lib")
}) // => 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.
// requireFromString
vm.runInNewContext(
code,
{
__dirname: contextModule.path,
__filename: contextModule.filename,
exports: contextModule.exports,
module: contextModule,
require: contextModule.require,
...globals
},
Take requireFromString
for example, only the above variables are passed into the contextObject
. In order to use other global objects and built-in modules you need to add them to option globals
.
requireFromString(
'module.exports = process.cwd()',
{ globals: { process } }
) // => $PWD
Function importFromString
and importFromStringSync
can use esbuild
to transform code syntax. See esbuild Transform API for documentation.
const { greet } = importFromStringSync(
"export const greet: () => string = () => 'hi'",
{ transformOptions: { loader: 'ts' } }
)
greet() // => 'hi'
Dynamic import()
expression of ES modules is supported by all three functions requireFromString
, importFromString
and importFromStringSync
.
;(async () => {
await requireFromString("module.exports = import('./index.mjs')")
})()
import
statement of ES modules is supported only by asynchronous function importFromString
using Node.js experimental API vm.Module
.
node --experimental-vm-modules index.js
# Or use environment variable
NODE_OPTIONS=--experimental-vm-modules node index.js
// with top-level await
await importFromString("export { foo as default } from './index.mjs'")
MIT License © 2021 Exuanbo
3.1.0 (2021-08-21)
import
statement of ES modules.dirname
to specify the directory path for resolving relative path require
or import
.FAQs
Load module from string using require or import.
The npm package module-from-string receives a total of 11,098 weekly downloads. As such, module-from-string popularity was classified as popular.
We found that module-from-string demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
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.
Research
Security News
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.
Security News
NVD’s backlog surpasses 20,000 CVEs as analysis slows and NIST announces new system updates to address ongoing delays.
Security News
Research
A malicious npm package disguised as a WhatsApp client is exploiting authentication flows with a remote kill switch to exfiltrate data and destroy files.