src-to-module
raw JSON → 2.0.28 verified Fri May 01 auth: no javascript
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.
Common errors
error TypeError: srcToModule.requireSync is not a function ↓
cause Using default import while library exports named exports since v2.0.0.
fix
Use import { requireSync } from 'src-to-module' or const { requireSync } = require('src-to-module').
error Error: The requireAsync function is not defined ↓
cause Trying to use requireAsync inside a module loaded with requireSync (sync context) instead of requireAsync.
fix
Load the module with requireAsync to make the global requireAsync available.
error SyntaxError: await is only valid in async function ↓
cause Using await at top level without wrapping in async function inside the loaded module.
fix
Wrap your code in an async function, or use runAsync which adds async wrapper.
Warnings
breaking Version 2.x changed exports from default export to named exports. ↓
fix Use named imports: import { requireSync } from 'src-to-module' instead of const srcToModule = require('src-to-module').
deprecated enableLastModifiedCheck is deprecated in v2.0.0, replaced by a configuration object. ↓
fix Use configuration: import { configure } from 'src-to-module'; configure({ checkLastModified: false }).
gotcha runAsync and requireAsync inject a requireAsync function into the global context of the loaded module; it may conflict with other globals. ↓
fix Avoid naming your own global variables requireAsync. Alternatively, use a separate sandbox if available in future versions.
gotcha Virtual file paths used with runSync/runAsync are cached for 30 minutes by default (maxAge), which may cause stale code. ↓
fix Provide a maxAge option (positive number) or call clearCache() to invalidate cache.
Install
npm install src-to-module yarn add src-to-module pnpm add src-to-module Imports
- requireSync wrong
const srcToModule = require('src-to-module').requireSynccorrectimport { requireSync } from 'src-to-module' - runAsync wrong
import runAsync from 'src-to-module'correctimport { runAsync } from 'src-to-module' - Transpiler wrong
import { Transpiler } from 'src-to-module'correctimport type { Transpiler } from 'src-to-module' - enableLastModifiedCheck wrong
enableLastModifiedCheck(false) // forgetting to importcorrectimport { enableLastModifiedCheck } from 'src-to-module'; enableLastModifiedCheck(false)
Quickstart
import { requireSync, runSync } from 'src-to-module';
// Load a module from file with context
const result = requireSync('/path/to/module.js', { apiKey: process.env.API_KEY ?? '' });
// Run code from string (virtual filepath)
const code = `module.exports = { sum: (a, b) => a + b }`;
const mod = runSync(code, '/virtual/module.js');
console.log(mod.sum(2, 3)); // 5