{"id":19241670,"url":"https://github.com/exuanbo/module-from-string","last_synced_at":"2025-05-09T00:08:59.339Z","repository":{"id":45503294,"uuid":"311758270","full_name":"exuanbo/module-from-string","owner":"exuanbo","description":"Load module from string using require or import.","archived":false,"fork":false,"pushed_at":"2024-07-29T05:07:23.000Z","size":1217,"stargazers_count":51,"open_issues_count":6,"forks_count":4,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-05-05T00:44:49.733Z","etag":null,"topics":["esbuild","import","load","module","require","require-from-string","string","vm"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/exuanbo.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-11-10T19:06:20.000Z","updated_at":"2025-04-25T02:44:14.000Z","dependencies_parsed_at":"2024-11-09T17:12:23.858Z","dependency_job_id":"ce8e8d8a-364e-4c9f-be93-a4daeebda1f0","html_url":"https://github.com/exuanbo/module-from-string","commit_stats":{"total_commits":246,"total_committers":2,"mean_commits":123.0,"dds":"0.016260162601625994","last_synced_commit":"78ea0561bfede38a87b8f63ae8ea0dcc2b155536"},"previous_names":[],"tags_count":21,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/exuanbo%2Fmodule-from-string","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/exuanbo%2Fmodule-from-string/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/exuanbo%2Fmodule-from-string/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/exuanbo%2Fmodule-from-string/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/exuanbo","download_url":"https://codeload.github.com/exuanbo/module-from-string/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252420962,"owners_count":21745152,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["esbuild","import","load","module","require","require-from-string","string","vm"],"created_at":"2024-11-09T17:12:12.912Z","updated_at":"2025-05-09T00:08:59.305Z","avatar_url":"https://github.com/exuanbo.png","language":"TypeScript","readme":"# module-from-string\n\n\u003e Load module from string using `require` or `import`.\n\n[![npm](https://img.shields.io/npm/v/module-from-string.svg)](https://www.npmjs.com/package/module-from-string)\n[![GitHub Workflow Status (branch)](https://img.shields.io/github/actions/workflow/status/exuanbo/module-from-string/nodejs.yml.svg?branch=main)](https://github.com/exuanbo/module-from-string/actions?query=workflow)\n[![Node.js](https://img.shields.io/badge/node-%3E%3D12.20.0-brightgreen.svg)](https://nodejs.org/)\n[![libera manifesto](https://img.shields.io/badge/libera-manifesto-lightgrey.svg)](https://liberamanifesto.com)\n\n## Install\n\n```sh\nnpm install module-from-string\n```\n\n## Usage\n\n```js\nimport {\n  requireFromString,\n  importFromString,\n  importFromStringSync\n} from 'module-from-string'\n\nrequireFromString(\"module.exports = 'hi'\") // =\u003e 'hi'\nrequireFromString(\"exports.greet = 'hi'\") // =\u003e { greet: 'hi' }\n\n;(async () =\u003e {\n  await importFromString(\"export default 'hi'\") // =\u003e { default: 'hi' }\n})()\n\nimportFromStringSync(\n  \"export const greet = Buffer.from([0x68, 0x69]).toString('utf8')\",\n  { globals: { Buffer } }\n) // =\u003e { greet: 'hi' }\n```\n\n## API\n\n```ts\nimport { Context } from 'vm';\nimport { TransformOptions } from 'esbuild';\n\ninterface Options {\n  filename?: string\n  dirname?: string\n  globals?: Context\n  useCurrentGlobal?: boolean\n}\n\ndeclare const requireFromString: (code: string, options?: Options) =\u003e any\ndeclare const createRequireFromString: (options?: Options) =\u003e typeof requireFromString\n\ninterface ImportOptions extends Options {\n  transformOptions?: TransformOptions\n}\n\ndeclare const importFromString: (code: string, options?: ImportOptions) =\u003e Promise\u003cany\u003e\ndeclare const createImportFromString: (options?: ImportOptions) =\u003e typeof importFromString\n\ndeclare const importFromStringSync: (code: string, options?: ImportOptions) =\u003e any\ndeclare const createImportFromStringSync: (options?: ImportOptions) =\u003e typeof importFromStringSync\n```\n\n### filename\n\nName, path or URL string of the virtual file for better exception stack trace.\n\n```js\nrequireFromString(\n  \"throw new Error('boom!')\",\n  { filename: '/home/foo.js' }\n)\n// /home/foo.js:1\n// throw new Error('boom!')\n// ^\n//\n// Error: boom!\n//     at /home/foo.js:1:7\n//     at ...\n```\n\n### dirname\n\nPath or URL string of the directory for resolving `require` or `import` from relative path.\n\n```js\nrequireFromString(\n  \"module.exports = require('.')\",\n  { dirname: path.join(__dirname, \"../lib\") }\n) // =\u003e require('../lib')\n```\n\nIf not specified, the default value will be the current file's directory.\n\n### globals\n\nUnderneath the hood, `module-from-string` uses Node.js built-in `vm` module to execute code.\n\n```js\n// requireFromString\n\nvm.runInNewContext(\n  code,\n  {\n    __dirname: contextModule.path,\n    __filename: contextModule.filename,\n    exports: contextModule.exports,\n    module: contextModule,\n    require: contextModule.require,\n    ...globals\n  },\n```\n\nTake `requireFromString` for example, only the module scope variables are passed into the `contextObject`.\n\nIn order to use other [global objects](https://nodejs.org/api/globals.html) that are specific to Node.js, they need to be added to option `globals` **or** set option [`useCurrentGlobal`](#usecurrentglobal) to `true`.\n\n```js\nrequireFromString(\n  'module.exports = process.cwd()',\n  { globals: { process } }\n) // =\u003e $PWD\n```\n\n**Note**: by default the [built-in objects](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects) have a different prototype.\n\n```js\nconst error = requireFromString('module.exports = new Error()')\nerror instanceof Error // =\u003e false\n```\n\n### useCurrentGlobal\n\nDefault to `false`. If set to `true`, all the available variables from the current `global` (or `globalThis`) are passed into the context.\n\n### transformOptions\n\nFunction `importFromString` and `importFromStringSync` can use `esbuild` to transform code syntax. See [esbuild Transform API](https://esbuild.github.io/api/#transform-api) for documentation.\n\n```js\nconst { greet } = importFromStringSync(\n  \"export const greet: () =\u003e string = () =\u003e 'hi'\",\n  { transformOptions: { loader: 'ts' } }\n)\n\ngreet() // =\u003e 'hi'\n```\n\n## ES modules\n\nDynamic `import()` expression of ES modules is supported by all three functions `requireFromString`, `importFromString` and `importFromStringSync`.\n\n```js\n;(async () =\u003e {\n  await requireFromString(\"module.exports = import('./index.mjs')\")\n})()\n```\n\n`import` statement of ES modules is supported only by asynchronous function `importFromString` using Node.js experimental API [`vm.Module`](https://nodejs.org/api/vm.html#vm_class_vm_module).\n\n```sh\nnode --experimental-vm-modules index.js\n\n# Or use environment variable\nNODE_OPTIONS=--experimental-vm-modules node index.js\n```\n\n```js\n// with top-level await\n\nawait importFromString(\"export { foo as default } from './index.mjs'\")\n```\n\n## License\n\n[MIT License](https://github.com/exuanbo/module-from-string/blob/main/LICENSE) © 2021 [Exuanbo](https://github.com/exuanbo)\n","funding_links":[],"categories":["TypeScript"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fexuanbo%2Fmodule-from-string","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fexuanbo%2Fmodule-from-string","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fexuanbo%2Fmodule-from-string/lists"}