Registry / web-framework / src-to-module

src-to-module

JSON →
library2.0.28jsnpmunverified

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.

npm install src-to-module
INSTALL
IMPORT
SIG · SRC-TO-MODULE
S
src-to-module
web-frameworkjavascriptv2.0.28
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

requireSync
import { requireSync } from 'src-to-module'
const srcToModule = require('src-to-module').requireSync
ESM default export does not exist; use named import. In CommonJS, use destructuring: const { requireSync } = require('src-to-module').
runAsync
import { runAsync } from 'src-to-module'
import runAsync from 'src-to-module'
runAsync is a named export, not the default export. TypeScript types are included.
Transpiler
import type { Transpiler } from 'src-to-module'
import { Transpiler } from 'src-to-module'
Transpiler is an interface, use `import type` to avoid runtime overhead in TypeScript projects.
enableLastModifiedCheck
import { enableLastModifiedCheck } from 'src-to-module'; enableLastModifiedCheck(false)
enableLastModifiedCheck(false) // forgetting to import
Only available starting v2.0.0.

Shows synchronous loading from file and from plain text string using src-to-module.

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
Debug
Known issues
breakingVersion 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').
affects: >=2.0.0
deprecatedenableLastModifiedCheck is deprecated in v2.0.0, replaced by a configuration object.
fix
Use configuration: import { configure } from 'src-to-module'; configure({ checkLastModified: false }).
affects: >=2.0.0
gotcharunAsync 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.
affects: >=1.0.0
gotchaVirtual 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.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: srcToModule.requireSync is not a function
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: The requireAsync function is not defined
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.
SyntaxError: await is only valid in async function
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.
Upgrade
Version history
2.0.28latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
3 hits · last 30 days
node
2
Resources
src-to-module — npm install src-to-module · libregistry