usebruno/bruno · error · Error
Error loading local module ${moduleName}: ${error.message}
Error message
Error loading local module ${moduleName}: ${error.message} What it means
Thrown by Bruno's node-vm CommonJS loader when a LOCAL module (a file resolved relative to the collection, e.g. require('./helper')) raises during execution inside the isolated vm.Script context. The loader wraps the user's module source in a CJS function wrapper, runs it, and if that function throws it re-throws a wrapped error with the module name and the original message. The cache entry is purged so a later require() can retry.
Source
Thrown at packages/bruno-js/src/sandbox/node-vm/cjs-loader.js:198
const moduleRequire = createCustomRequire({
collectionPath,
isolatedContext,
currentModuleDir: moduleDir,
localModuleCache,
additionalContextRootsAbsolute
});
try {
// Wrap module code in a function that receives CJS parameters
const wrappedCode = `(function(module, exports, require, __filename, __dirname) {\n${moduleCode}\n})`;
const compiledScript = new vm.Script(wrappedCode, { filename: normalizedFilePath });
const moduleFunction = compiledScript.runInContext(isolatedContext);
moduleFunction(moduleObj, moduleObj.exports, moduleRequire, normalizedFilePath, moduleDir);
return moduleObj.exports;
} catch (error) {
// Remove failed module from cache to allow retry
localModuleCache.delete(normalizedFilePath);
throw new Error(`Error loading local module ${moduleName}: ${error.message}`);
}
}
/**
* Executes a module in the VM context with caching and special file handling
* @param {Object} options - Configuration options
* @returns {*} The exported content of the loaded module
* @throws {Error} When module cannot be loaded
*/
function executeModuleInVmContext({
resolvedPath,
moduleName,
isolatedContext,
collectionPath,
localModuleCache
}) {
// Check cache - we cache moduleObj, return its exports
if (localModuleCache.has(resolvedPath)) {View on GitHub (pinned to 9bdd81c7bd)
Solutions
- Read the appended original message — it identifies the real failure; fix the offending line in the local module file and re-run the request.
- Confirm the module only uses globals Bruno injects into the VM context (bru, Buffer, console, permitted libs); remove or stub any host-only API call.
- Run the local module with plain `node path/to/file.js` outside Bruno to reproduce the error quickly.
- If a circular require is involved, reorder exports so the consuming module reads the property lazily rather than at top level.
Example fix
// before — local module references undefined `req`
module.exports = { sign: () => req.headers['x-foo'] };
// after — pass dependencies in explicitly
module.exports = { sign: (headers) => headers['x-foo'] }; Defensive patterns
Strategy: try-catch
Validate before calling
// Validate the local module file exists and is readable before requiring
const fs = require('fs');
const p = require('path').join(bru.cwd(), 'my-helper.js');
if (!fs.existsSync(p)) throw new Error('local module missing: ' + p); Try / catch
try {
const helper = require('./my-helper');
} catch (err) {
if (/Error loading local module/.test(err.message)) {
// surface the inner message and stop the test cleanly
throw new Error('local module failed to load: ' + err.message);
}
throw err;
} Prevention
- Keep local modules small and dependency-free so they always load inside the sandbox.
- Run any new local module through `node file.js` before referencing it from a Bru script.
- Avoid host-only Node APIs in local modules executed under the VM sandbox.
When it happens
Trigger: Calling require('./my-helper') in a Bru test/pre-request script where my-helper.js has a top-level throw, references an undefined variable, calls an API not exposed to the sandbox, or contains a syntax error that only surfaces at runtime inside the wrapper.
Common situations: A typo or undefined variable in the local module file; the local module references Node host APIs (fs, process, child_process) not injected into the VM context; a circular require that returns partial exports whose property is then called; editing the file while Bruno holds a stale cache (the cache is cleared on failure, so a re-run after fixing the source usually clears it).
Related errors
- Error loading module ${moduleName}: ${error.message} Stack:
- Could not resolve module "${moduleName}": ${mainError.messag
- Access to files outside of the collectionPath is not allowed
- Cannot find module ${filename}
- The "size" argument must be of type number
AI-assisted analysis of usebruno/bruno@9bdd81c7bd (2026-08-13).
Data as JSON: /api/errors/cf4a3d04662c7ede.
Report an issue: GitHub.