{"record":{"id":"e7a9470d171307b2","repo":"usebruno/bruno","slug":"cannot-find-module-mod","errorCode":null,"errorMessage":"Cannot find module ${mod}","messagePattern":"Cannot find module (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/bruno-js/src/sandbox/quickjs/shims/require.js","lineNumber":38,"sourceCode":"        let localModuleCode = globalThis.__brunoLoadLocalModule(mod);\n\n        // compile local module as iife\n        (function (){\n          const initModuleExportsCode = \"const module = { exports: {} };\"\n          const copyModuleExportsCode = \"\\\\n;globalThis.requireObject[mod] = module.exports;\";\n          const patchedRequire = ${`\n            \"\\\\n;\" +\n            \"let require = (subModule) => isModuleAPath(subModule) ? globalThis.require(path.resolve(bru.cwd(), mod, '..', subModule)) : globalThis.require(subModule)\" +\n            \"\\\\n;\"\n          `}\n          eval(initModuleExportsCode + patchedRequire + localModuleCode + copyModuleExportsCode);\n        })();\n\n        // resolve module\n        return globalThis.requireObject[mod];\n      }\n      else {\n        throw new Error(\"Cannot find module \" + mod);\n      }\n    }\n  `;\n}\n\n/**\n * Adds the require() function to a QuickJS VM context\n * @param {Object} vm - QuickJS VM context\n * @param {Object} options - Options passed to getRequireCode\n */\nfunction addRequireShimToContext(vm) {\n  vm.evalCode(getRequireCode());\n}\n\nmodule.exports = {\n  getRequireCode,\n  addRequireShimToContext\n};","sourceCodeStart":20,"sourceCodeEnd":56,"githubUrl":"https://github.com/usebruno/bruno/blob/9bdd81c7bdc57006e5f5ebffb79321a8d979f712/packages/bruno-js/src/sandbox/quickjs/shims/require.js#L20-L56","documentation":"Thrown by the QuickJS require() shim when the requested module name is neither a key on globalThis.requireObject (the pre-loaded npm bundle) nor a path-like/local module (does not start with '.' or with bru.cwd()). The shim has no other resolution strategy, so it errors.","triggerScenarios":"require('lodash') inside a QuickJS-sandboxed Bru script where lodash was not pre-loaded into requireObject and is not resolvable as a local file.","commonSituations":"Trying to use an npm package that was not registered for the QuickJS sandbox (QuickJS only sees a curated requireObject, unlike node-vm which walks node_modules); typo in the module name; expecting node-vm behavior in QuickJS mode.","solutions":["Switch the sandbox mode to node-vm (developer mode) if you need full npm resolution, or restrict yourself to modules pre-loaded into the QuickJS requireObject.","For local files, ensure the path starts with '.' or is under bru.cwd() so the local loader kicks in.","Check the Bruno QuickJS allow-list to see whether the package is available; if not, request it or use a bundled alternative."],"exampleFix":"// before — QuickJS sandbox, axios not in requireObject\nrequire('axios');\n\n// after — use a pre-allowed module or switch sandbox\n// option A: use Bruno's built-in request API instead\n// option B: switch collection sandbox to 'developer' (node-vm) mode","handlingStrategy":"validation","validationCode":"// In QuickJS, only modules pre-loaded into requireObject are available.\n// Check before requiring:\nfunction isAvailable(mod) {\n  return typeof globalThis.requireObject[mod] !== 'undefined'\n    || mod.startsWith('.') || (typeof bru !== 'undefined' && mod.startsWith(bru.cwd()));\n}\nif (!isAvailable('axios')) throw new Error('axios not available in QuickJS sandbox');","typeGuard":"const isRequireable = (mod) => Boolean(globalThis.requireObject[mod]) || mod.startsWith('.') || (typeof bru !== 'undefined' && mod.startsWith(bru.cwd()));","tryCatchPattern":"try { const lib = require('axios'); }\ncatch (err) {\n  if (/Cannot find module/.test(err.message)) {\n    // switch to node-vm sandbox or use Bruno's built-in request API\n  } else throw err;\n}","preventionTips":["Learn which modules Bruno pre-loads into the QuickJS requireObject.","Switch the collection sandbox to node-vm if you need broad npm access.","Use Bruno's built-in HTTP API instead of axios in QuickJS where possible."],"tags":["bruno-js","quickjs","sandbox","require","module-resolution"],"backgroundTag":null,"analyzedSha":"9bdd81c7bdc57006e5f5ebffb79321a8d979f712","analyzedAt":"2026-08-13T04:09:25.751Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}