curl to Node.js
Convert curl commands to Node.js code styles including Fetch API, Axios, or the native http/https modules.
curl to Node.js Converter
This tool converts curl commands into ready-to-run Node.js code in three styles. The Fetch API mode uses the built-in fetch() available in Node.js 18+ — no dependencies needed. The Axios mode generates code for the popular axios library, widely used in existing Node.js projects. The http/https modulemode uses Node's built-in https module with no external dependencies, useful for environments where you cannot install packages. Supports common curl flags: -X (method), -H (headers), -d (request body), -u (basic auth), -k (insecure), and -A (user agent).
Built and maintained by Meet Shah · Last updated
What this tool is used for
- Converting a curl example into Node code with the built-in fetch.
- Choosing between fetch, Axios and the http module for one request.
- Reproducing a captured request in a Node service.
- Producing a snippet for a script or a serverless handler.
- Checking which form suits your Node version.
Frequently Asked Questions
- Can I just use fetch in Node?
- Yes, from Node 18 — global fetch is stable and backed by undici. It has no CORS restriction (Node is not a browser), so unlike browser fetch it behaves much like curl. Below Node 18 you need node-fetch or the built-in http/https modules.
- How does curl's -k work in Node?
- For the https module, pass rejectUnauthorized: false in the options. For fetch/undici, construct an Agent with connect: { rejectUnauthorized: false }. Setting NODE_TLS_REJECT_UNAUTHORIZED=0 also works but disables verification process-wide and logs a warning.
- Why does my https request never finish?
- With the raw http/https modules you must call req.end() to actually send the request, and consume or destroy the response stream. Forgetting req.end() produces a request that silently hangs with no error.
- How do I set a timeout?
- Node's fetch takes an AbortSignal: signal: AbortSignal.timeout(5000). The https module takes a timeout option, but it only fires a 'timeout' event — you must call req.destroy() yourself, or the socket stays open.
- How do I stream a large response to disk?
- Pipe it: response.body.pipe(fs.createWriteStream(path)) with the https module, or use Readable.fromWeb(response.body) with fetch. This is the equivalent of curl -o and avoids buffering the whole payload in memory.
- Is fetch available without a dependency?
- Yes on Node 18 and later, where it is built in — which removes the need for axios or node-fetch for simple calls. Older runtimes still need a package, so the target version decides the generated code.
Common errors and gotchas
- Assuming global fetch exists, which needs Node 18 or later.
- Using the http module and forgetting to end the request, which then never sends.
- Not handling the response as a stream in the http module, where data arrives in chunks.
- Expecting fetch to reject on a 4xx, which only a network failure does.
- Omitting a timeout, which none of the three forms applies by default.