{"record":{"id":"6b33b959dd0591cb","repo":"usebruno/bruno","slug":"error-loading-module-modulename-error-messag","errorCode":null,"errorMessage":"Error loading module ${moduleName}: ${error.message}\nStack: ${stack}","messagePattern":"Error loading module (.+?): (.+?)\nStack: (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/bruno-js/src/sandbox/node-vm/cjs-loader.js","lineNumber":266,"sourceCode":"\n  const moduleRequire = createNpmModuleRequire({\n    collectionPath,\n    isolatedContext,\n    currentModuleDir: moduleDir,\n    localModuleCache\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${moduleSource}\\n})`;\n    const compiledScript = new vm.Script(wrappedCode, { filename: resolvedPath });\n    const moduleFunction = compiledScript.runInContext(isolatedContext);\n    moduleFunction(moduleObj, moduleObj.exports, moduleRequire, resolvedPath, moduleDir);\n  } catch (error) {\n    // Remove failed module from cache to allow retry\n    localModuleCache.delete(resolvedPath);\n    const stack = error.stack || '';\n    throw new Error(`Error loading module ${moduleName}: ${error.message}\\nStack: ${stack}`);\n  }\n\n  return moduleObj.exports;\n}\n\n/**\n * Loads an npm module into the vm context.\n *\n * Resolution order matches standard Node.js walk-up:\n *   1. currentModuleDir/node_modules → walk up parent dirs\n *   2. collectionPath/node_modules\n *   3. Bruno's bundled node_modules (final fallback for chai/ajv/axios/etc.)\n *\n * @param {Object} options - Configuration options\n * @returns {*} The exported content of the loaded module\n * @throws {Error} When module cannot be resolved or loaded\n */\nfunction loadNpmModule({","sourceCodeStart":248,"sourceCodeEnd":284,"githubUrl":"https://github.com/usebruno/bruno/blob/9bdd81c7bdc57006e5f5ebffb79321a8d979f712/packages/bruno-js/src/sandbox/node-vm/cjs-loader.js#L248-L284","documentation":"Thrown by Bruno's node-vm CommonJS loader when an NPM module's top-level code throws while being executed inside the isolated vm.Script context. It is the npm-module counterpart of error 360: the resolved path is loaded, wrapped in the CJS function wrapper, run in the VM, and any exception is re-thrown with the module name and the full original stack trace. The cache entry for resolvedPath is purged first.","triggerScenarios":"require('some-npm-pkg') in a Bru script where the package's top-level initialization throws — e.g. it calls an unsupported Node API, performs feature detection that fails inside the VM, or its main file references globals Bruno did not inject.","commonSituations":"Package version that relies on Node host APIs unavailable in the sandbox (worker_threads, node:net, native .node addons called from JS); package that throws if `process.env.NODE_ENV` or a required config is missing; ESM-only package that the CJS wrapper mis-loads; stale install where the package's index file was deleted.","solutions":["Read the full appended `Stack:` — it points at the line inside node_modules that failed; address that cause (missing env var, unsupported API, etc.).","Pin or upgrade the package to a version known to run inside Bruno's sandbox (check Bruno's bundled-modules list).","If the package needs Node host APIs, load it via a different sandbox mode (developer/node-vm vs QuickJS) or move the logic out of the sandbox.","Reinstall dependencies: `npm install <pkg>` in the collection directory so the resolved path exists and is intact."],"exampleFix":"// before — package throws because process.env.KEY is undefined at load\nrequire('some-pkg');\n\n// after — set the env before requiring\nprocess.env.KEY = bru.getVar('apiKey');\nrequire('some-pkg');","handlingStrategy":"try-catch","validationCode":"// Confirm the npm package is resolvable from the collection before requiring\ntry { require.resolve('some-pkg', { paths: [bru.cwd()] }); } catch { throw new Error('install some-pkg first'); }","typeGuard":null,"tryCatchPattern":"try {\n  const lib = require('some-pkg');\n} catch (err) {\n  if (/Error loading module/.test(err.message)) {\n    console.error('npm module load failed:', err.message);\n    // fall back to a bundled alternative or stop the run\n    throw err;\n  }\n  throw err;\n}","preventionTips":["Pin package versions known to run inside Bruno's VM sandbox.","Provide required env vars before requiring packages that read them at load time.","Keep a list of packages Bruno bundles vs. ones you must install locally."],"tags":["bruno-js","node-vm","sandbox","cjs-loader","npm-module"],"backgroundTag":null,"analyzedSha":"9bdd81c7bdc57006e5f5ebffb79321a8d979f712","analyzedAt":"2026-08-13T04:09:25.751Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}