Got CommonJS Wrapper

12.5.4 · maintenance · verified Wed Apr 22

got-cjs is a utility package maintained by Apify, specifically designed to provide a CommonJS-compatible interface for the popular `got` HTTP client library in Node.js. It currently wraps `got` version 11.8.3, which was the last major version of `got` to fully support CommonJS modules. This package serves as a crucial bridge for projects that are still using CommonJS and cannot easily migrate to ECMAScript Modules (ESM), as the main `got` package transitioned to being ESM-only from version 12 onwards. got-cjs ensures that CJS-based applications can continue to leverage `got`'s robust feature set, including its Promise API, stream API, retries, and advanced error handling, without requiring a full module system migration. Its release cadence is likely tied to the maintenance and security updates of its underlying `got` v11 dependency. Its key differentiator is enabling `got`'s v11 API for CommonJS environments.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates a basic GET request with headers and timeout, followed by a JSON POST request, using the `got-cjs` API.

const got = require('got-cjs');

(async () => {
  try {
    const response = await got('https://httpbin.org/get', {
      headers: {
        'User-Agent': 'my-awesome-app/1.0'
      },
      timeout: {
        request: 5000 // 5 seconds
      }
    });
    console.log('Status Code:', response.statusCode);
    console.log('Body:', response.body.substring(0, 200) + '...');

    const postResponse = await got.post('https://httpbin.org/post', {
      json: {
        hello: 'world'
      },
      responseType: 'json'
    });
    console.log('Posted JSON:', postResponse.body);
  } catch (error) {
    console.error('Request failed:', error.message);
  }
})();

view raw JSON →