{"record":{"id":"357c1c9d476eac16","repo":"usebruno/bruno","slug":"cannot-find-module-filename","errorCode":null,"errorMessage":"Cannot find module ${filename}","messagePattern":"Cannot find module (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/bruno-js/src/sandbox/quickjs/shims/local-module.js","lineNumber":23,"sourceCode":"const addLocalModuleLoaderShimToContext = (vm, collectionPath) => {\n  let loadLocalModuleHandle = vm.newFunction('loadLocalModule', function (module) {\n    const filename = vm.dump(module);\n\n    // Check if the filename has an extension\n    const hasExtension = path.extname(filename) !== '';\n    const resolvedFilename = hasExtension ? filename : `${filename}.js`;\n\n    // Resolve the file path and check if it's within the collectionPath\n    const filePath = path.resolve(collectionPath, resolvedFilename);\n    const relativePath = path.relative(collectionPath, filePath);\n\n    // Ensure the resolved file path is inside the collectionPath\n    if (relativePath.startsWith('..') || path.isAbsolute(relativePath)) {\n      throw new Error('Access to files outside of the collectionPath is not allowed.');\n    }\n\n    if (!fs.existsSync(filePath)) {\n      throw new Error(`Cannot find module ${filename}`);\n    }\n\n    let code = fs.readFileSync(filePath).toString();\n\n    return marshallToVm(code, vm);\n  });\n\n  vm.setProp(vm.global, '__brunoLoadLocalModule', loadLocalModuleHandle);\n  loadLocalModuleHandle.dispose();\n};\n\nmodule.exports = addLocalModuleLoaderShimToContext;\n","sourceCodeStart":5,"sourceCodeEnd":36,"githubUrl":"https://github.com/usebruno/bruno/blob/9bdd81c7bdc57006e5f5ebffb79321a8d979f712/packages/bruno-js/src/sandbox/quickjs/shims/local-module.js#L5-L36","documentation":"Thrown by the QuickJS local-module loader shim after the path-traversal check passed but fs.existsSync(filePath) returned false. The filename (with .js appended if no extension was given) does not exist on disk under collectionPath.","triggerScenarios":"require('./missing-file') or require('./helper') when ./helper.js does not exist in the collection directory inside a QuickJS-sandboxed script.","commonSituations":"File was renamed or deleted; case mismatch on case-sensitive filesystems; the .js extension was expected but the file is .ts/.mjs and the shim only appends .js; the require path is relative to the wrong base (the shim resolves against collectionPath, not the calling file's directory).","solutions":["Confirm the file exists with the exact name and case under the collectionPath.","Remember the shim appends .js — name your local module file with a .js extension, or pass the full filename including extension.","If using TypeScript, transpile to .js first or require the compiled output."],"exampleFix":"// before — only helper.ts exists\ncrypto.require('./helper'); // -> Cannot find module helper\n\n// after — either rename to helper.js or pass the extension\ncrypto.require('./helper.js'); // after creating helper.js","handlingStrategy":"validation","validationCode":"const fs = require('fs'); const path = require('path');\nfunction resolveLocal(name, root) {\n  const file = path.extname(name) ? name : name + '.js';\n  const full = path.join(root, file);\n  if (!fs.existsSync(full)) throw new Error('missing: ' + full);\n  return full;\n}","typeGuard":"const fileExists = (p) => { try { return fs.statSync(p).isFile(); } catch { return false; } };","tryCatchPattern":"try { const m = require('./helper'); }\ncatch (err) {\n  if (/Cannot find module/.test(err.message)) {\n    // create the file or fix the name\n  } else throw err;\n}","preventionTips":["Name local module files with the .js extension.","Check case on case-sensitive filesystems.","Remember the shim resolves against collectionPath, not the calling file."],"tags":["bruno-js","quickjs","sandbox","local-module","file-not-found"],"backgroundTag":null,"analyzedSha":"9bdd81c7bdc57006e5f5ebffb79321a8d979f712","analyzedAt":"2026-08-13T04:09:25.751Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}