{"record":{"id":"cf4a3d04662c7ede","repo":"usebruno/bruno","slug":"error-loading-local-module-modulename-error","errorCode":null,"errorMessage":"Error loading local module ${moduleName}: ${error.message}","messagePattern":"Error loading local module (.+?): (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/bruno-js/src/sandbox/node-vm/cjs-loader.js","lineNumber":198,"sourceCode":"  const moduleRequire = createCustomRequire({\n    collectionPath,\n    isolatedContext,\n    currentModuleDir: moduleDir,\n    localModuleCache,\n    additionalContextRootsAbsolute\n  });\n\n  try {\n    // Wrap module code in a function that receives CJS parameters\n    const wrappedCode = `(function(module, exports, require, __filename, __dirname) {\\n${moduleCode}\\n})`;\n    const compiledScript = new vm.Script(wrappedCode, { filename: normalizedFilePath });\n    const moduleFunction = compiledScript.runInContext(isolatedContext);\n    moduleFunction(moduleObj, moduleObj.exports, moduleRequire, normalizedFilePath, moduleDir);\n    return moduleObj.exports;\n  } catch (error) {\n    // Remove failed module from cache to allow retry\n    localModuleCache.delete(normalizedFilePath);\n    throw new Error(`Error loading local module ${moduleName}: ${error.message}`);\n  }\n}\n\n/**\n * Executes a module in the VM context with caching and special file handling\n * @param {Object} options - Configuration options\n * @returns {*} The exported content of the loaded module\n * @throws {Error} When module cannot be loaded\n */\nfunction executeModuleInVmContext({\n  resolvedPath,\n  moduleName,\n  isolatedContext,\n  collectionPath,\n  localModuleCache\n}) {\n  // Check cache - we cache moduleObj, return its exports\n  if (localModuleCache.has(resolvedPath)) {","sourceCodeStart":180,"sourceCodeEnd":216,"githubUrl":"https://github.com/usebruno/bruno/blob/9bdd81c7bdc57006e5f5ebffb79321a8d979f712/packages/bruno-js/src/sandbox/node-vm/cjs-loader.js#L180-L216","documentation":"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.","triggerScenarios":"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.","commonSituations":"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).","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."],"exampleFix":"// before — local module references undefined `req`\nmodule.exports = { sign: () => req.headers['x-foo'] };\n\n// after — pass dependencies in explicitly\nmodule.exports = { sign: (headers) => headers['x-foo'] };","handlingStrategy":"try-catch","validationCode":"// Validate the local module file exists and is readable before requiring\nconst fs = require('fs');\nconst p = require('path').join(bru.cwd(), 'my-helper.js');\nif (!fs.existsSync(p)) throw new Error('local module missing: ' + p);","typeGuard":null,"tryCatchPattern":"try {\n  const helper = require('./my-helper');\n} catch (err) {\n  if (/Error loading local module/.test(err.message)) {\n    // surface the inner message and stop the test cleanly\n    throw new Error('local module failed to load: ' + err.message);\n  }\n  throw err;\n}","preventionTips":["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."],"tags":["bruno-js","node-vm","sandbox","cjs-loader","local-module"],"backgroundTag":null,"analyzedSha":"9bdd81c7bdc57006e5f5ebffb79321a8d979f712","analyzedAt":"2026-08-13T04:09:25.751Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}