{"id":18515,"library":"lws-body-parser","title":"lws-body-parser","description":"Body parsing middleware for lws (local-web-server) that wraps koa-bodyparser. Version 3.0.1 is the latest stable release, requiring Node >=12.17. It provides body parsing for incoming HTTP requests, supporting JSON, URL-encoded, and multipart form data. Key differentiator: integration with lws middleware stack, providing a simple configuration-based approach compared to raw koa-bodyparser. Releases follow lws versioning cadence, with updates primarily driven by underlying koa-bodyparser changes.","status":"active","version":"3.0.1","language":"javascript","source_language":"en","source_url":"https://github.com/lwsjs/body-parser","tags":["javascript","lws","lws-middleware","koa","express"],"install":[{"cmd":"npm install lws-body-parser","lang":"bash","label":"npm"},{"cmd":"yarn add lws-body-parser","lang":"bash","label":"yarn"},{"cmd":"pnpm add lws-body-parser","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"provides core body parsing functionality","package":"koa-bodyparser","optional":false}],"imports":[{"note":"ESM-only since v3. Default export is a function. CJS require() will fail without dynamic import.","wrong":"const { bodyParser } = require('lws-body-parser')","symbol":"default export","correct":"import bodyParser from 'lws-body-parser'"},{"note":"Use ESM imports. In lws, add middleware via .use().","wrong":"const lws = require('lws');\nconst bodyParser = require('lws-body-parser').default;","symbol":"class usage with lws","correct":"import { createServer } from 'lws'\nimport bodyParser from 'lws-body-parser'\nconst server = createServer()\nserver.use(bodyParser)"},{"note":"TypeScript users should import types separately. Not available in CJS.","symbol":"type imports","correct":"import type { BodyParserOptions } from 'lws-body-parser'"}],"quickstart":{"code":"import { createServer } from 'lws'\nimport bodyParser from 'lws-body-parser'\n\nconst server = createServer()\nserver.use(bodyParser)\n\nserver.use((ctx) => {\n  ctx.body = `Received: ${JSON.stringify(ctx.request.body)}`\n})\n\nserver.listen(8000, () => {\n  console.log('Server running on http://localhost:8000')\n})\n","lang":"typescript","description":"Creates an lws server with body-parser middleware. Parses request body and echoes it back. Shows ESM import and basic middleware setup."},"warnings":[{"fix":"Migrate to ESM: use 'import' instead of 'require()'.","message":"v3 dropped CommonJS support. Require() will throw a MODULE_NOT_FOUND error.","severity":"breaking","affected_versions":">=3.0.0"},{"fix":"Ensure incoming requests have the correct Content-Type header. For custom types, configure koa-bodyparser options via lws-body-parser.","message":"Body parsing only applies to requests with content-type application/json, application/x-www-form-urlencoded, or multipart/form-data.","severity":"gotcha","affected_versions":">=2.0.0"},{"fix":"Check official lws-body-parser docs for options; avoid depending on undocumented koa-bodyparser options.","message":"Options passed to lws-body-parser may change in future versions to align with koa-bodyparser updates.","severity":"deprecated","affected_versions":">=2.0.0"}],"env_vars":null,"last_verified":"2026-04-25T00:00:00.000Z","next_check":"2026-07-24T00:00:00.000Z","problems":[{"fix":"Use import() or switch to ESM: 'import bodyParser from \"lws-body-parser\"' and set \"type\": \"module\" in package.json.","cause":"Using require() with an ESM-only package (lws-body-parser v3).","error":"Error [ERR_REQUIRE_ESM]: require() of ES Module ... from ... not supported."},{"fix":"Ensure request has proper Content-Type header (e.g., application/json). Verify body-parser middleware is added before route handlers.","cause":"Request body not parsed because content-type is missing or not supported.","error":"TypeError: ctx.request.body is undefined"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}