{"id":26430,"library":"src-to-module","title":"src-to-module","description":"Converts source code to a reusable Node.js module at runtime. Version 2.0.28 (stable) is actively maintained. Supports synchronous and asynchronous loading from files or plain text, with built-in transpiler registration for JavaScript and JSON. Unlike vm.runInNewContext, it provides module caching, last-modified checks, and a familiar Node.js module environment. Key differentiator: full control over transpilation pipeline and context per loaded module, enabling custom transpilers (e.g., Babel, TypeScript) and virtual file paths with cache expiry.","status":"active","version":"2.0.28","language":"javascript","source_language":"en","source_url":"https://kennysng@github.com/kennysng/src-to-module","tags":["javascript","typescript"],"install":[{"cmd":"npm install src-to-module","lang":"bash","label":"npm"},{"cmd":"yarn add src-to-module","lang":"bash","label":"yarn"},{"cmd":"pnpm add src-to-module","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"ESM default export does not exist; use named import. In CommonJS, use destructuring: const { requireSync } = require('src-to-module').","wrong":"const srcToModule = require('src-to-module').requireSync","symbol":"requireSync","correct":"import { requireSync } from 'src-to-module'"},{"note":"runAsync is a named export, not the default export. TypeScript types are included.","wrong":"import runAsync from 'src-to-module'","symbol":"runAsync","correct":"import { runAsync } from 'src-to-module'"},{"note":"Transpiler is an interface, use `import type` to avoid runtime overhead in TypeScript projects.","wrong":"import { Transpiler } from 'src-to-module'","symbol":"Transpiler","correct":"import type { Transpiler } from 'src-to-module'"},{"note":"Only available starting v2.0.0.","wrong":"enableLastModifiedCheck(false) // forgetting to import","symbol":"enableLastModifiedCheck","correct":"import { enableLastModifiedCheck } from 'src-to-module'; enableLastModifiedCheck(false)"}],"quickstart":{"code":"import { requireSync, runSync } from 'src-to-module';\n\n// Load a module from file with context\nconst result = requireSync('/path/to/module.js', { apiKey: process.env.API_KEY ?? '' });\n\n// Run code from string (virtual filepath)\nconst code = `module.exports = { sum: (a, b) => a + b }`;\nconst mod = runSync(code, '/virtual/module.js');\nconsole.log(mod.sum(2, 3)); // 5","lang":"typescript","description":"Shows synchronous loading from file and from plain text string using src-to-module."},"warnings":[{"fix":"Use named imports: import { requireSync } from 'src-to-module' instead of const srcToModule = require('src-to-module').","message":"Version 2.x changed exports from default export to named exports.","severity":"breaking","affected_versions":">=2.0.0"},{"fix":"Use configuration: import { configure } from 'src-to-module'; configure({ checkLastModified: false }).","message":"enableLastModifiedCheck is deprecated in v2.0.0, replaced by a configuration object.","severity":"deprecated","affected_versions":">=2.0.0"},{"fix":"Avoid naming your own global variables requireAsync. Alternatively, use a separate sandbox if available in future versions.","message":"runAsync and requireAsync inject a requireAsync function into the global context of the loaded module; it may conflict with other globals.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Provide a maxAge option (positive number) or call clearCache() to invalidate cache.","message":"Virtual file paths used with runSync/runAsync are cached for 30 minutes by default (maxAge), which may cause stale code.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-05-01T00:00:00.000Z","next_check":"2026-07-30T00:00:00.000Z","problems":[{"fix":"Use import { requireSync } from 'src-to-module' or const { requireSync } = require('src-to-module').","cause":"Using default import while library exports named exports since v2.0.0.","error":"TypeError: srcToModule.requireSync is not a function"},{"fix":"Load the module with requireAsync to make the global requireAsync available.","cause":"Trying to use requireAsync inside a module loaded with requireSync (sync context) instead of requireAsync.","error":"Error: The requireAsync function is not defined"},{"fix":"Wrap your code in an async function, or use runAsync which adds async wrapper.","cause":"Using await at top level without wrapping in async function inside the loaded module.","error":"SyntaxError: await is only valid in async function"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}