{"id":16078,"library":"http-status-ts","title":"HTTP Status Codes for TypeScript","description":"http-status-ts is a lightweight, isomorphic utility library designed for convenient access to standard HTTP status codes within TypeScript projects. It provides a comprehensive list of HTTP status codes as a TypeScript enum (`HttpStatus`) and a helper function (`httpStatusTextByCode`) to retrieve the corresponding human-readable text description. Currently stable at version 2.0.1, the library maintains a steady cadence of releases focused on stability and minor enhancements. Its primary differentiation lies in its explicit TypeScript-first design, providing strong type safety out-of-the-box for both Node.js and browser environments, unlike some plain JavaScript alternatives that may require manual type definitions.","status":"active","version":"2.0.1","language":"javascript","source_language":"en","source_url":"https://github.com/rmuchall/http-status-ts","tags":["javascript","http","status","codes","typescript"],"install":[{"cmd":"npm install http-status-ts","lang":"bash","label":"npm"},{"cmd":"yarn add http-status-ts","lang":"bash","label":"yarn"},{"cmd":"pnpm add http-status-ts","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"HttpStatus is a named export (an enum), not a default export. When using CommonJS, it must be accessed as a property of the required module.","wrong":"import HttpStatus from 'http-status-ts';\nconst HttpStatus = require('http-status-ts');","symbol":"HttpStatus","correct":"import { HttpStatus } from 'http-status-ts';"},{"note":"httpStatusTextByCode is a named export (a function). For CommonJS, it requires destructuring or property access from the module object.","wrong":"import httpStatusTextByCode from 'http-status-ts';\nconst httpStatusTextByCode = require('http-status-ts');","symbol":"httpStatusTextByCode","correct":"import { httpStatusTextByCode } from 'http-status-ts';"},{"note":"This pattern allows accessing all named exports (e.g., `HttpStatusModule.HttpStatus`, `HttpStatusModule.httpStatusTextByCode`) under a single namespace. Useful if you need many exports from the module.","symbol":"All named exports","correct":"import * as HttpStatusModule from 'http-status-ts';"}],"quickstart":{"code":"import { HttpStatus, httpStatusTextByCode } from 'http-status-ts';\n\n// Get a status code's numeric value from the enum\nconst notFoundCode = HttpStatus.NOT_FOUND;\nconsole.log(`The numeric code for 'Not Found' is: ${notFoundCode}`); // Output: 404\n\n// Get the text description for a specific status code\nconst okText = httpStatusTextByCode(HttpStatus.OK);\nconsole.log(`The text description for ${HttpStatus.OK} is: \"${okText}\"`); // Output: \"OK\"\n\n// Example: Dynamically getting text for a user-input code (simulated)\nconst userProvidedCode = 500;\nconst errorMessage = httpStatusTextByCode(userProvidedCode);\nconsole.log(`For code ${userProvidedCode}, the error message is: \"${errorMessage}\"`); // Output: \"Internal Server Error\"\n\n// Example using status codes in a conceptual HTTP response\ninterface MockResponse {\n  status: (code: number) => { send: (message: string) => void };\n}\n\nconst mockResponse: MockResponse = {\n  status: (code: number) => ({\n    send: (message: string) => {\n      console.log(`[HTTP Response Simulator] Status: ${code}, Body: ${message}`);\n    }\n  })\n};\n\nconst createSuccess = HttpStatus.CREATED;\nmockResponse.status(createSuccess).send(httpStatusTextByCode(createSuccess));\n\nconst clientError = HttpStatus.BAD_REQUEST;\nmockResponse.status(clientError).send(httpStatusTextByCode(clientError));","lang":"typescript","description":"Demonstrates importing and utilizing both the `HttpStatus` enum to retrieve numeric codes and the `httpStatusTextByCode` function to obtain human-readable descriptions, including a conceptual example of their use in an HTTP response."},"warnings":[{"fix":"Use the enum member directly: `const code = HttpStatus.OK;` instead of `const code = HttpStatus.OK.code;`.","message":"When using `HttpStatus` enum members, remember that `HttpStatus.OK` directly evaluates to the numeric code `200`. Do not try to access a `.code` property on it, as it is the value itself.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"For ES Modules, always use `import { HttpStatus, httpStatusTextByCode } from 'http-status-ts';`. For CommonJS (if supported, verify `package.json` `type` field), ensure correct destructuring: `const { HttpStatus, httpStatusTextByCode } = require('http-status-ts');`.","message":"Incorrect module imports, especially mixing CommonJS `require` with ES Modules `import` syntax or not correctly destructuring named exports, can lead to `undefined` module members.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-21T00:00:00.000Z","next_check":"2026-07-20T00:00:00.000Z","problems":[{"fix":"Ensure `HttpStatus` is imported as a named export: `import { HttpStatus } from 'http-status-ts';`.","cause":"The `HttpStatus` enum was not correctly imported or was imported as a default export, leading to it being `undefined` or a malformed object.","error":"TypeError: Cannot read properties of undefined (reading 'OK')"},{"fix":"Use a named import for ESM: `import { httpStatusTextByCode } from 'http-status-ts';`. For CJS: `const { httpStatusTextByCode } = require('http-status-ts');`.","cause":"The `httpStatusTextByCode` function was not correctly imported or was not destructuring from the module in a CommonJS context.","error":"ReferenceError: httpStatusTextByCode is not defined"},{"fix":"Either convert your project to ES Modules by adding `\"type\": \"module\"` to your `package.json` (and renaming `.js` to `.mjs` if needed), or configure your TypeScript compiler to output ES Modules. Alternatively, use CommonJS `require()` syntax if your project is strictly CommonJS.","cause":"Attempting to use ES module `import` syntax in a file that is treated as a CommonJS module (e.g., a `.js` file without `\"type\": \"module\"` in `package.json`, or a `.ts` file compiled to CJS without proper transpilation setup).","error":"SyntaxError: Cannot use import statement outside a module"}],"ecosystem":"npm"}