usebruno/bruno · error · Error

Error loading module ${moduleName}: ${error.message} Stack:

Error message

Error loading module ${moduleName}: ${error.message}
Stack: ${stack}

What it means

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.

Source

Thrown at packages/bruno-js/src/sandbox/node-vm/cjs-loader.js:266

  const moduleRequire = createNpmModuleRequire({
    collectionPath,
    isolatedContext,
    currentModuleDir: moduleDir,
    localModuleCache
  });

  try {
    // Wrap module code in a function that receives CJS parameters
    const wrappedCode = `(function(module, exports, require, __filename, __dirname) {\n${moduleSource}\n})`;
    const compiledScript = new vm.Script(wrappedCode, { filename: resolvedPath });
    const moduleFunction = compiledScript.runInContext(isolatedContext);
    moduleFunction(moduleObj, moduleObj.exports, moduleRequire, resolvedPath, moduleDir);
  } catch (error) {
    // Remove failed module from cache to allow retry
    localModuleCache.delete(resolvedPath);
    const stack = error.stack || '';
    throw new Error(`Error loading module ${moduleName}: ${error.message}\nStack: ${stack}`);
  }

  return moduleObj.exports;
}

/**
 * Loads an npm module into the vm context.
 *
 * Resolution order matches standard Node.js walk-up:
 *   1. currentModuleDir/node_modules → walk up parent dirs
 *   2. collectionPath/node_modules
 *   3. Bruno's bundled node_modules (final fallback for chai/ajv/axios/etc.)
 *
 * @param {Object} options - Configuration options
 * @returns {*} The exported content of the loaded module
 * @throws {Error} When module cannot be resolved or loaded
 */
function loadNpmModule({

View on GitHub (pinned to 9bdd81c7bd)

Solutions

  1. Read the full appended `Stack:` — it points at the line inside node_modules that failed; address that cause (missing env var, unsupported API, etc.).
  2. Pin or upgrade the package to a version known to run inside Bruno's sandbox (check Bruno's bundled-modules list).
  3. 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.
  4. Reinstall dependencies: `npm install <pkg>` in the collection directory so the resolved path exists and is intact.

Example fix

// before — package throws because process.env.KEY is undefined at load
require('some-pkg');

// after — set the env before requiring
process.env.KEY = bru.getVar('apiKey');
require('some-pkg');
Defensive patterns

Strategy: try-catch

Validate before calling

// Confirm the npm package is resolvable from the collection before requiring
try { require.resolve('some-pkg', { paths: [bru.cwd()] }); } catch { throw new Error('install some-pkg first'); }

Try / catch

try {
  const lib = require('some-pkg');
} catch (err) {
  if (/Error loading module/.test(err.message)) {
    console.error('npm module load failed:', err.message);
    // fall back to a bundled alternative or stop the run
    throw err;
  }
  throw err;
}

Prevention

When it happens

Trigger: 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.

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

Related errors


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