{"id":10506,"library":"api-problem","title":"API Problem Details for HTTP APIs","description":"api-problem is a JavaScript utility library designed to standardize error responses in HTTP APIs according to RFC 7807, \"Problem Details for HTTP APIs.\" It provides a `Problem` class that allows developers to construct detailed, machine-readable error objects for various HTTP status codes. These objects include a status, human-readable title, a URI identifying the problem type (which defaults to MDN HTTP status pages since v9), and optional additional fields. The current stable version is 9.0.2. The library maintains an active release cadence, primarily focusing on security updates, dependency bumps, CI/CD improvements, and dropping support for older Node.js versions to align with modern JavaScript ecosystems. Its core differentiator is strict adherence to the RFC 7807 specification, ensuring consistent and interoperable error communication across different API clients and servers.","status":"active","version":"9.0.2","language":"javascript","source_language":"en","source_url":"https://github.com/ahmadnassri/node-api-problem","tags":["javascript","api","http","problem"],"install":[{"cmd":"npm install api-problem","lang":"bash","label":"npm"},{"cmd":"yarn add api-problem","lang":"bash","label":"yarn"},{"cmd":"pnpm add api-problem","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"The `Problem` class is a default export, intended for use in ES Modules environments.","wrong":"import { Problem } from 'api-problem'","symbol":"Problem (ESM)","correct":"import Problem from 'api-problem'"},{"note":"CommonJS `require` is supported, but if your project is configured for ES Modules, use the `import` syntax instead.","wrong":"import Problem from 'api-problem'","symbol":"Problem (CommonJS)","correct":"const Problem = require('api-problem')"},{"note":"For importing only the type of the `Problem` class in TypeScript, use `import type` as `Problem` is a default export.","wrong":"import { Problem } from 'api-problem' // for type-only import","symbol":"Problem Type (TypeScript)","correct":"import type Problem from 'api-problem'"}],"quickstart":{"code":"import Problem from 'api-problem';\nimport http from 'http';\n\n// Create a basic 404 Not Found problem\nconst notFoundProblem = new Problem(404);\nconsole.log('Basic 404 Problem:', notFoundProblem.toObject());\n\n// Create a 403 Forbidden problem with a custom title\nconst forbiddenProblem = new Problem(403, 'Access to resource denied');\nconsole.log('Custom Title Problem:', forbiddenProblem.toObject());\n\n// Create a custom problem type with additional details\nconst outOfCreditProblem = new Problem(\n  403,\n  'You do not have enough credit',\n  'https://example.com/probs/out-of-credit',\n  {\n    detail: 'Your current balance is 30, but that costs 50 for this operation.',\n    instance: '/account/12345/transactions/abc',\n    balance: 30\n  }\n);\nconsole.log('Custom Detailed Problem:', outOfCreditProblem.toObject());\n\n// Example of sending a problem via an HTTP response (Node.js http module)\nconst server = http.createServer((req, res) => {\n  if (req.url === '/error') {\n    const problem = new Problem(400, 'Invalid parameters in request', 'https://example.com/probs/invalid-params', {\n      errors: [\n        { field: 'param1', message: 'Must be a number' },\n        { field: 'param2', message: 'Cannot be empty' }\n      ]\n    });\n    problem.send(res);\n  } else {\n    res.writeHead(200, { 'Content-Type': 'text/plain' });\n    res.end('Hello World');\n  }\n});\n\n// server.listen(3000, () => console.log('Server running on http://localhost:3000'));\n// To test: curl -i http://localhost:3000/error\n// (Uncomment server.listen and run the file to test the .send() method)","lang":"typescript","description":"Demonstrates the creation of various RFC 7807 problem detail objects with default HTTP meanings, custom titles, types, and additional application-specific details, including how to send a problem as an HTTP response."},"warnings":[{"fix":"Review any code, especially tests or API documentation, that relies on the exact `type` URI for default HTTP problems. Update expectations to account for the new MDN-based URIs, or explicitly set a custom `type` URI during `Problem` instantiation if a specific URI format is required for backward compatibility.","message":"The default domain for the 'type' field in `Problem` objects changed from `about:blank` (or similar, sometimes omitted) to `https://developer.mozilla.org/` for HTTP status-related problem types. This change affects the URI structure when a custom `type` is not explicitly provided.","severity":"breaking","affected_versions":">=9.0.0"},{"fix":"Upgrade your Node.js runtime to version 14 or newer. Consult the official Node.js release schedule for information on currently supported LTS versions.","message":"Support for Node.js v12 was officially dropped. Projects using `api-problem` v8.0.0 or later are required to run on Node.js v14 or higher.","severity":"breaking","affected_versions":">=8.0.0"},{"fix":"Ensure your Node.js environment supports ES2015 (Node.js v6+ generally does, but earlier versions might have partial support). It is recommended to use Node.js v14+ in conjunction with this library. No specific changes are needed for typical modern Node.js setups using either ESM `import` or CJS `require`.","message":"The library removed older Babel-generated, targeted builds (e.g., for ES5 compatibility), transitioning to ES2015+ as its primary output. While CommonJS compatibility is maintained, projects targeting very old Node.js runtimes or specific non-ES2015 environments might face compatibility issues if they relied on these older builds.","severity":"breaking","affected_versions":">=7.0.0"},{"fix":"Ensure your project is using `api-problem` version 9.0.1 or higher. Regularly update all project dependencies to include security fixes.","message":"A prototype pollution vulnerability (CVE-2022-46175) in the `json5` dependency was addressed in a patch release. While `api-problem`'s direct usage of `json5` might be limited, it's critical to update to mitigate potential supply chain attacks or other vulnerabilities if your project also processes untrusted JSON5 data.","severity":"gotcha","affected_versions":">=9.0.1"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"For ES Modules, use `import Problem from 'api-problem'`. For CommonJS, use `const Problem = require('api-problem')`. Verify your project's `package.json` `\"type\"` field and file extensions (.js vs .mjs vs .cjs) to ensure consistent module resolution.","cause":"This error often occurs when attempting to instantiate `Problem` as a class using incorrect module import syntax, such as `import { Problem } from 'api-problem'` in an ESM context, or when using `require()` in an environment expecting ESM default imports without proper configuration.","error":"TypeError: Problem is not a constructor"},{"fix":"Upgrade your Node.js development and deployment environments to version 14 or newer. Consult the official Node.js website for long-term support (LTS) releases and upgrade procedures.","cause":"Attempting to run `api-problem` versions 8.0.0 or later on a Node.js runtime older than v14.","error":"Error: The 'api-problem' package requires Node.js version 14 or higher. You are running Node.js version X."},{"fix":"To correctly import the type for the default-exported `Problem` class in TypeScript, use `import type Problem from 'api-problem';`.","cause":"In a TypeScript project, this error indicates an incorrect import for the `Problem` type, typically trying to import it as a named export (`import { Problem } from '...'`) when it is a default export.","error":"Type 'typeof import(\"api-problem\").Problem' is not assignable to type 'Problem'. Did you mean to use 'typeof import(\"api-problem\").Problem'?"}],"ecosystem":"npm"}