{"id":17238,"library":"got-cjs","title":"Got CommonJS Wrapper","description":"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.","status":"maintenance","version":"12.5.4","language":"javascript","source_language":"en","source_url":"https://github.com/apify/got-cjs","tags":["javascript","http","https","http2","get","got","url","uri","request","typescript"],"install":[{"cmd":"npm install got-cjs","lang":"bash","label":"npm"},{"cmd":"yarn add got-cjs","lang":"bash","label":"yarn"},{"cmd":"pnpm add got-cjs","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Core HTTP client functionality, `got-cjs` wraps `got` v11.","package":"got","optional":false}],"imports":[{"note":"This package is explicitly for CommonJS environments. For ESM, use the main 'got' package directly.","wrong":"import got from 'got-cjs';","symbol":"got","correct":"const got = require('got-cjs');"},{"note":"The primary export is a default `got` instance; named exports are not typical for this CJS wrapper.","wrong":"import { got } from 'got-cjs';","symbol":"got","correct":"const got = require('got-cjs');\nconst { URLSearchParams } = require('url'); // Example for a common Node.js global"},{"note":"TypeScript types are provided and should be imported using standard ESM `import type` syntax, even in a CJS project if using TS.","wrong":"type GotReturn = ReturnType<typeof require('got-cjs')>;","symbol":"GotReturn","correct":"import type { GotReturn } from 'got-cjs';"}],"quickstart":{"code":"const got = require('got-cjs');\n\n(async () => {\n  try {\n    const response = await got('https://httpbin.org/get', {\n      headers: {\n        'User-Agent': 'my-awesome-app/1.0'\n      },\n      timeout: {\n        request: 5000 // 5 seconds\n      }\n    });\n    console.log('Status Code:', response.statusCode);\n    console.log('Body:', response.body.substring(0, 200) + '...');\n\n    const postResponse = await got.post('https://httpbin.org/post', {\n      json: {\n        hello: 'world'\n      },\n      responseType: 'json'\n    });\n    console.log('Posted JSON:', postResponse.body);\n  } catch (error) {\n    console.error('Request failed:', error.message);\n  }\n})();","lang":"javascript","description":"Demonstrates a basic GET request with headers and timeout, followed by a JSON POST request, using the `got-cjs` API."},"warnings":[{"fix":"For CommonJS projects, continue using `got-cjs`. For new projects or existing projects able to migrate, consider migrating to ESM and using `got` v12+ directly via `import` statements.","message":"The primary `got` package (v12 and newer) is pure ESM and cannot be directly `require()`d. `got-cjs` specifically provides a CommonJS wrapper for `got` v11, which means it will not receive new features or API changes from `got` v12+.","severity":"breaking","affected_versions":">=12.0.0 (for `got` itself)"},{"fix":"Review the changelogs for the main `got` package. If crucial new features or fixes are needed, migrating your project to ESM to use `got` v12+ is recommended.","message":"`got-cjs` bundles `got` v11.8.3. This means that any new features, performance improvements, or non-security bug fixes introduced in `got` v12 or later will not be available when using `got-cjs`. Only security patches for `got` v11 will likely be backported.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Evaluate your project's module system. If feasible, migrate to ESM to leverage the broader modern JavaScript ecosystem and directly use `got` v12+.","message":"While `got-cjs` enables CommonJS usage, the Node.js ecosystem is increasingly standardizing on ECMAScript Modules (ESM). Relying heavily on CJS wrappers might limit access to newer libraries or features in the long term.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-22T00:00:00.000Z","next_check":"2026-07-21T00:00:00.000Z","problems":[{"fix":"Install `got-cjs` and change your import to `const got = require('got-cjs');`.","cause":"Attempting to `require('got')` (the main `got` package) in a CommonJS environment when `got` version 12 or newer is installed.","error":"ERR_REQUIRE_ESM: Must use import to load ES Module: .../node_modules/got/dist/source/index.js"},{"fix":"If your project is CommonJS, use `const got = require('got-cjs');`. If your project is ESM, use `import got from 'got';`.","cause":"This error can occur if you mistakenly try to use `require('got')` with `got` v12+ in a CJS file and then try to call it. While the `require` might partially work for an ESM module, the default export is not directly callable as a function in the CJS `require` context without specific interop.","error":"TypeError: got is not a function"}],"ecosystem":"npm","meta_description":null}