{"record":{"id":"7383b3d8022e658e","repo":"usebruno/bruno","slug":"cannot-find-module-modulename","errorCode":null,"errorMessage":"Cannot find module ${moduleName}","messagePattern":"Cannot find module (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/bruno-js/src/sandbox/node-vm/cjs-loader.js","lineNumber":167,"sourceCode":"  // Resolve the module path, handling files and directories\n  const normalizedFilePath = resolveLocalModulePath(currentModuleDir, moduleName);\n\n  // Final security check after resolution\n  if (!isPathWithinAllowedRoots(normalizedFilePath, additionalContextRootsAbsolute)) {\n    const allowedRootsDisplay = additionalContextRootsAbsolute.map((root) => `  - ${root}`).join('\\n');\n    throw new Error(\n      `Access to files outside of the allowed context roots is not allowed: ${moduleName}\\n\\n`\n      + `Allowed context roots:\\n${allowedRootsDisplay}`\n    );\n  }\n\n  // Check cache - we cache moduleObj, return its exports\n  if (localModuleCache.has(normalizedFilePath)) {\n    return localModuleCache.get(normalizedFilePath).exports;\n  }\n\n  if (!fs.existsSync(normalizedFilePath)) {\n    throw new Error(`Cannot find module ${moduleName}`);\n  }\n\n  const moduleCode = fs.readFileSync(normalizedFilePath, 'utf8');\n  const moduleObj = { exports: {} };\n  const moduleDir = path.dirname(normalizedFilePath);\n\n  // Pre-populate cache with moduleObj BEFORE execution to handle circular dependencies\n  // This allows re-entrant requires to get partial exports (Node.js behavior)\n  // We cache moduleObj (not moduleObj.exports) so that module.exports reassignment works\n  localModuleCache.set(normalizedFilePath, moduleObj);\n\n  // Create require function for nested imports\n  const moduleRequire = createCustomRequire({\n    collectionPath,\n    isolatedContext,\n    currentModuleDir: moduleDir,\n    localModuleCache,\n    additionalContextRootsAbsolute","sourceCodeStart":149,"sourceCodeEnd":185,"githubUrl":"https://github.com/usebruno/bruno/blob/9bdd81c7bdc57006e5f5ebffb79321a8d979f712/packages/bruno-js/src/sandbox/node-vm/cjs-loader.js#L149-L185","documentation":"Thrown by loadLocalModule when the resolved module file does not exist on disk. After all security checks pass, the loader calls fs.existsSync(normalizedFilePath) at cjs-loader.js:166; if the file is not found, this error is thrown. This mirrors Node.js's native 'Cannot find module' error for the sandboxed require context.","triggerScenarios":"Calling require('./helpers') when there is no helpers.js, helpers/index.js, or helpers/package.json in the current module's directory. The resolution algorithm at cjs-loader.js:19-60 tries the exact path, path + .js, directory + package.json main, and directory + index.js before falling back to the raw path, which then fails the existsSync check.","commonSituations":"Typo in the module path. Forgetting to add the .js extension (the loader tries it automatically, but only for the last path segment). Importing a file that has not been created yet. Case-sensitivity issues on Linux (e.g., requiring './Utils' when the file is 'utils.js').","solutions":["Verify the file exists at the expected path relative to the current script.","Check for typos and case mismatches in the filename (Linux is case-sensitive).","Ensure the file has a .js extension or is an index.js inside a directory of that name.","Use the correct relative path prefix (./ for same directory, ../ for parent)."],"exampleFix":"// before\nconst helper = require('./helper'); // file is actually helpers.js (plural)\n\n// after\nconst helper = require('./helpers'); // matches helpers.js","handlingStrategy":"validation","validationCode":"const path = require('path');\nconst fs = require('fs');\nfunction moduleExists(currentDir, modulePath) {\n  const basePath = path.resolve(currentDir, modulePath);\n  if (path.extname(modulePath) && fs.existsSync(basePath)) return true;\n  if (fs.existsSync(basePath + '.js')) return true;\n  if (fs.existsSync(basePath) && fs.statSync(basePath).isDirectory()) {\n    if (fs.existsSync(path.join(basePath, 'index.js'))) return true;\n    const pkg = path.join(basePath, 'package.json');\n    if (fs.existsSync(pkg)) {\n      const main = JSON.parse(fs.readFileSync(pkg, 'utf8')).main;\n      if (main && fs.existsSync(path.resolve(basePath, main))) return true;\n    }\n  }\n  return false;\n}\n// before requiring: if (moduleExists(__dirname, modulePath)) require(modulePath);","typeGuard":null,"tryCatchPattern":"try {\n  const mod = require(modulePath);\n} catch (e) {\n  if (e.message.startsWith('Cannot find module')) {\n    console.error('Module file not found. Check path, extension, and case:', modulePath);\n  }\n}","preventionTips":["Verify the file exists before requiring it.","Check for typos and case-sensitivity issues (Linux is case-sensitive).","Ensure the file has a .js extension or is resolvable as a directory with index.js."],"tags":["module-loader","file-not-found","require","path-resolution"],"backgroundTag":null,"analyzedSha":"9bdd81c7bdc57006e5f5ebffb79321a8d979f712","analyzedAt":"2026-08-13T04:09:25.751Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}