{"id":12675,"library":"wordwrapjs","title":"Word Wrapping for JavaScript","description":"wordwrapjs is a lightweight and versatile JavaScript library designed for word-wrapping plain text within specified column widths. Currently at version 5.1.1, it provides a stable and efficient solution for text formatting across various JavaScript environments. The library supports Node.js (both CommonJS and ECMAScript Modules), modern web browsers (via ESM imports), and older browser environments (via a global `window.wordwrapjs` object), enabling broad compatibility without requiring transpilation. Its API offers methods to wrap text into a single string or an array of lines, with options to force long words to break and control line trimming. The project emphasizes plain text processing, focusing on core word-wrapping logic rather than rich text features. Releases appear stable, with infrequent major version bumps, indicating a mature and well-tested codebase.","status":"active","version":"5.1.1","language":"javascript","source_language":"en","source_url":"https://github.com/75lb/wordwrapjs","tags":["javascript","word","line","wrap","text","columns","wordwrap"],"install":[{"cmd":"npm install wordwrapjs","lang":"bash","label":"npm"},{"cmd":"yarn add wordwrapjs","lang":"bash","label":"yarn"},{"cmd":"pnpm add wordwrapjs","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"The primary API is accessed via the default export, which provides static methods like `wrap` and `lines`.","wrong":"import { wordwrap } from 'wordwrapjs'","symbol":"wordwrap","correct":"import wordwrap from 'wordwrapjs'"},{"note":"For CommonJS environments, the entire module export is typically assigned to a variable to access static methods.","wrong":"const { wordwrap } = require('wordwrapjs')","symbol":"wordwrap","correct":"const wordwrap = require('wordwrapjs')"},{"note":"Import the type definition for options object if using TypeScript.","symbol":"WordwrapOptions","correct":"import type { WordwrapOptions } from 'wordwrapjs'"}],"quickstart":{"code":"import wordwrap from 'wordwrapjs'\n\nconst longText = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.'\n\n// Wrap text to a 30-character width, allowing long words to break\nconst wrappedText = wordwrap.wrap(longText, { width: 30, break: true })\nconsole.log('--- Wrapped Text (width 30, break: true) ---\\n' + wrappedText)\n\nconst url = 'https://this-is-a-very-long-url-that-exceeds-the-column-width.com/path/to/resource'\n\n// Wrap a long URL into lines, forcing breaks\nconst urlLines = wordwrap.lines(url, { width: 25, break: true })\nconsole.log('\\n--- Wrapped URL (width 25, break: true) ---')\nurlLines.forEach(line => console.log(line))\n\n// Wrap without forcing breaks (default behavior for long words)\nconst noBreakLines = wordwrap.lines(url, { width: 25 })\nconsole.log('\\n--- Wrapped URL (width 25, no break) ---')\nnoBreakLines.forEach(line => console.log(line))","lang":"typescript","description":"Demonstrates basic word wrapping with `wrap` and `lines` methods, including options for breaking long words and handling URLs. It shows the difference between breaking and not breaking long words for a given width."},"warnings":[{"fix":"Manually strip ANSI escape codes before passing text to `wordwrapjs`, or use a library specifically designed for wrapping text with ANSI support.","message":"When wrapping text for terminal output, ANSI escape codes (e.g., for colors) are counted as characters towards the `width` option, leading to incorrect visual wrapping. This library is designed for plain text.","severity":"gotcha","affected_versions":">=1.0"},{"fix":"Pass `{ break: true }` in the options object to `wrap` or `lines` methods: `wordwrap.wrap(text, { width: 20, break: true })`.","message":"By default, words longer than the specified `width` will not be broken; instead, the entire word will be placed on its own line, potentially exceeding the `width`. To force long words to break, the `break: true` option must be explicitly set.","severity":"gotcha","affected_versions":">=1.0"},{"fix":"Ensure your project consistently uses either CJS or ESM. If you must mix them, verify your Node.js version supports the interoperability patterns you're using (Node.js >=12.17 for `wordwrapjs`), and be aware of synchronous vs. asynchronous loading differences. Consider explicit file extensions (`.mjs`, `.cjs`) or `type: \"module\"` in `package.json`.","message":"Interoperability between CommonJS (CJS) and ECMAScript Modules (ESM) in Node.js can be complex. While `wordwrapjs` provides both CJS (`require`) and ESM (`import`) entry points, mixing them in a single project can lead to `ERR_REQUIRE_ESM` or unexpected behavior in older Node.js versions or complex build setups.","severity":"gotcha","affected_versions":">=1.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Use the ESM `import` syntax: `import wordwrap from 'wordwrapjs'`. Ensure your environment (Node.js, bundler) is configured for ESM.","cause":"Attempting to use `require()` in an ECMAScript Module (ESM) file (e.g., a file ending in `.js` when `type: \"module\"` is set in `package.json`, or a `.mjs` file).","error":"ReferenceError: require is not defined in ES module scope"},{"fix":"Ensure your `package.json` for the consuming project does not have `\"type\": \"module\"` if you intend to use CommonJS. If you are using CJS, ensure `wordwrapjs`'s CJS entry point is correctly resolved, or consider migrating your project to ESM.","cause":"A CommonJS module is trying to `require()` the ESM build of `wordwrapjs`. This happens when a CJS project tries to import an ESM-only package or when module resolution incorrectly picks the ESM entry point.","error":"Error [ERR_REQUIRE_ESM]: require() of ES Module ... wordwrapjs/dist/index.mjs from ... not supported."},{"fix":"To force long words to break at the specified width, pass the `break: true` option: `wordwrap.wrap(text, { width: 20, break: true })`.","cause":"The default behavior of `wordwrapjs` is not to break individual words, even if they are longer than the specified `width`. The entire word is moved to the next line.","error":"Text wraps unexpectedly, leaving long words on their own line exceeding width."},{"fix":"Ensure the `noTrim` option is not explicitly set to `true` if you want lines to be trimmed. The default behavior should trim lines automatically. If you desire specific trimming, manage it manually after wrapping.","cause":"By default, `wordwrapjs` trims whitespace from the end of each line. If `noTrim` option is set to `true`, trailing whitespace will be preserved.","error":"Trailing whitespace appears on wrapped lines."}],"ecosystem":"npm"}