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

module-from-string

Package Overview
Dependencies
Maintainers
1
Versions
21
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

module-from-string

Load module from string, require and import.

  • 2.2.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
17K
decreased by-30.19%
Maintainers
1
Weekly downloads
 
Created
Source

module-from-string

Load module from string, require and import.

npm GitHub Workflow Status (branch) Codecov branch libera manifesto

Install

npm install module-from-string

Usage

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' }
})()

API

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 }

globals

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 }
})

transformOptions

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'

License

MIT License © 2020 Exuanbo

Keywords

FAQs

Package last updated on 13 Feb 2021

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