Diagnosing Hoppscotch Pre-Request Script Failures: A Step-by-Step Guide
A diagnostic guide for Hoppscotch pre-request scripts that fail silently, throw ReferenceErrors, or get blocked by sandbox and CORS restrictions.
01 Nov 2025, 12:01 UTC

Recognizable Symptoms
You add a pre-request script to inject an authentication token, compute a signature, or set a dynamic header, but the outgoing request looks unchanged. Alternatively, the script appears to run, yet the call never leaves the browser, or the console fills with red error text. These failures are usually not bugs in Hoppscotch; they fall into a small set of predictable conditions involving script placement, JavaScript syntax, sandbox restrictions, variable scope, or browser network policy.
A pre-request script is JavaScript that Hoppscotch executes immediately before dispatching an HTTP request. It runs inside a restricted sandbox—a limited JavaScript environment that blocks access to the file system, Node.js modules, and other host resources. When a script fails, Hoppscotch may still send the request, but without the modifications you expected.
Common Causes and Quick Diagnostics
| Symptom | Likely Cause | Where to Look |
|---|---|---|
| Script seems to do nothing; headers or variables unchanged | Syntax error or runtime exception halting execution | Console pane (red error text) |
ReferenceError or undefined in console | Environment variable name mismatch or wrong scope | Pre-request script and active environment selector |
require is not defined or similar | Node.js API used inside browser sandbox | Console pane; review script for Node built-ins |
| Request sends, but expected auth or header is missing | Script placed in Tests tab instead of Pre-request | Request editor tabs |
| Browser shows CORS error or timeout after script runs | Target server blocking origin or slow response | Browser Network tab |
Ordered Checks and Fixes
1. Confirm the Script Lives in the Pre-Request Tab
Hoppscotch separates pre-request logic from post-response tests. If your script sits in the Tests tab, it executes after the response arrives, which is too late to modify the outgoing request. Open the request, locate the Pre-request tab, and move the script there if necessary. Save the change before proceeding.
2. Inspect the Console Pane for Red Errors
Open the request's Pre-request tab and click the Console pane. Trigger the script by saving or sending the request. Any red text indicates a JavaScript failure. A ReferenceError—an error thrown when code references a variable that has not been declared—will stop execution immediately, so later lines never run.
For example, a script intended to read a token from the active environment might contain:
const authToken = apiToken;
Because apiToken is not a global binding in Hoppscotch's sandbox, the console reports ReferenceError: apiToken is not defined. The script halts before it can attach the header. Fix the variable access according to Hoppscotch's environment API, and verify the variable actually exists in the currently selected environment.
3. Resolve Environment Variable Scope Issues
Environment variables in Hoppscotch are scoped: a variable defined in a global environment may not be visible when a personal or team environment is active. If the console shows a reference or lookup failure, open the environment selector and confirm the variable name matches exactly, including case. Test in an isolated environment before applying changes to a shared collection, because collection-level scripts affect every request that inherits them.
4. Remove Blocked Node.js APIs
Hoppscotch's sandbox intentionally blocks Node.js built-ins. A line such as:
const fs = require("fs");
will fail because require does not exist in the browser sandbox. The same applies to modules like path, os, or direct process access. Review the script for any Node-specific globals and replace them with sandbox-safe equivalents, or move that logic to a backend service that the request can call.
5. Rule Out Network and CORS Blocks
If the console is clean and the script is correctly placed, open your browser's developer tools and switch to the Network tab. Send the request again. If the call is blocked or times out, the issue may be CORS—Cross-Origin Resource Sharing, a browser security policy that prevents a web page from reading responses from a different domain unless the server explicitly allows it. Hoppscotch running in a browser is subject to the same policy. Inspect the blocked entry to see whether the server rejected the origin or the preflight request.
Verification and Limitations
After applying a fix, open the Console pane, save the script, and send a test request. Confirm that no red error appears and that the expected variable substitutions or header modifications show up in the generated request preview. If the problem persists, export the collection and import it into a fresh Hoppscotch profile to rule out local profile corruption.
Be aware of two limitations. First, anything logged to the Console pane can expose secrets if the collection is shared; avoid logging tokens or credentials. Second, browser-based Hoppscotch cannot bypass CORS or access the local file system, so some workflows that work in Node.js will require a different execution environment.
When to Escalate
Escalate to a Hoppscotch maintainer or community forum if:
- A minimal reproduction with no custom scripts still fails in a fresh profile.
- The Console pane shows no errors, the script is in the correct tab, and environment variables resolve correctly, yet the expected behavior does not occur.
- You suspect a platform bug in the scripting sandbox or request dispatcher.
Include the exact script contents (with secrets redacted), the active environment variable names, and a description of the observed versus expected request headers when reporting the issue.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.