usebruno/bruno · error · Error

Cannot find module ${mod}

Error message

Cannot find module ${mod}

What it means

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.

Source

Thrown at packages/bruno-js/src/sandbox/quickjs/shims/require.js:38

        let localModuleCode = globalThis.__brunoLoadLocalModule(mod);

        // compile local module as iife
        (function (){
          const initModuleExportsCode = "const module = { exports: {} };"
          const copyModuleExportsCode = "\\n;globalThis.requireObject[mod] = module.exports;";
          const patchedRequire = ${`
            "\\n;" +
            "let require = (subModule) => isModuleAPath(subModule) ? globalThis.require(path.resolve(bru.cwd(), mod, '..', subModule)) : globalThis.require(subModule)" +
            "\\n;"
          `}
          eval(initModuleExportsCode + patchedRequire + localModuleCode + copyModuleExportsCode);
        })();

        // resolve module
        return globalThis.requireObject[mod];
      }
      else {
        throw new Error("Cannot find module " + mod);
      }
    }
  `;
}

/**
 * Adds the require() function to a QuickJS VM context
 * @param {Object} vm - QuickJS VM context
 * @param {Object} options - Options passed to getRequireCode
 */
function addRequireShimToContext(vm) {
  vm.evalCode(getRequireCode());
}

module.exports = {
  getRequireCode,
  addRequireShimToContext
};

View on GitHub (pinned to 9bdd81c7bd)

Solutions

  1. 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.
  2. For local files, ensure the path starts with '.' or is under bru.cwd() so the local loader kicks in.
  3. Check the Bruno QuickJS allow-list to see whether the package is available; if not, request it or use a bundled alternative.

Example fix

// before — QuickJS sandbox, axios not in requireObject
require('axios');

// after — use a pre-allowed module or switch sandbox
// option A: use Bruno's built-in request API instead
// option B: switch collection sandbox to 'developer' (node-vm) mode
Defensive patterns

Strategy: validation

Validate before calling

// In QuickJS, only modules pre-loaded into requireObject are available.
// Check before requiring:
function isAvailable(mod) {
  return typeof globalThis.requireObject[mod] !== 'undefined'
    || mod.startsWith('.') || (typeof bru !== 'undefined' && mod.startsWith(bru.cwd()));
}
if (!isAvailable('axios')) throw new Error('axios not available in QuickJS sandbox');

Type guard

const isRequireable = (mod) => Boolean(globalThis.requireObject[mod]) || mod.startsWith('.') || (typeof bru !== 'undefined' && mod.startsWith(bru.cwd()));

Try / catch

try { const lib = require('axios'); }
catch (err) {
  if (/Cannot find module/.test(err.message)) {
    // switch to node-vm sandbox or use Bruno's built-in request API
  } else throw err;
}

Prevention

When it happens

Trigger: require('lodash') inside a QuickJS-sandboxed Bru script where lodash was not pre-loaded into requireObject and is not resolvable as a local file.

Common situations: 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.

Related errors


AI-assisted analysis of usebruno/bruno@9bdd81c7bd (2026-08-13). Data as JSON: /api/errors/e7a9470d171307b2. Report an issue: GitHub.