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, require and import.
npm install module-from-string
const { requireFromString, importFromString } = require('module-from-string')
requireFromString("module.exports = 'hi'") // => 'hi'
requireFromString("exports.greet = 'hi'") // => { greet: 'hi' }
;(async () => {
await importFromString("export default 'hi'" ) // => { default: 'hi' }
await importFromString("export const greet = 'hi'") // => { greet: 'hi' }
})()
import { TransformOptions } from 'esbuild'
interface Options {
code: string
globals?: Record<string, unknown>
}
declare const requireFromString: (options: string | Options) => any
interface ImportOptions extends Options {
transformOptions?: TransformOptions
}
declare const importFromString: (
options: string | ImportOptions
) => Promise<any>
declare const importFromStringSync: (options: string | ImportOptions) => any
export { importFromString, importFromStringSync, requireFromString }
Underneath the hood, module-from-string
uses Node.js built-in vm
module to execute code.
const _module = new Module(String(new Date().valueOf()))
const context = vm.createContext({
exports: _module.exports,
module: _module,
require,
...globals
})
vm.runInContext(code, context)
By default, only above variables are passed into the contextObject
. In order to use other global objects you need to add them to option globals
.
requireFromString('module.exports = process.cwd()', { process })
importFromStringSync({
code: 'export default process.cwd()',
globals: { process }
})
Function importFromString
and importFromStringSync
use esbuild to transform ES Module syntax to CommonJS. So it can do much more by providing transform options to esbuild. See esbuild Transform API for documentation.
const { greet } = importFromStringSync({
code: "export const greet: () => string = () => 'hi'",
transformOptions: { loader: 'ts' }
})
greet() // => 'hi'
MIT License © 2020 Exuanbo
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.