Word Wrapping for JavaScript
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.
Common errors
-
ReferenceError: require is not defined in ES module scope
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).fixUse the ESM `import` syntax: `import wordwrap from 'wordwrapjs'`. Ensure your environment (Node.js, bundler) is configured for ESM. -
Error [ERR_REQUIRE_ESM]: require() of ES Module ... wordwrapjs/dist/index.mjs from ... not supported.
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.fixEnsure 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. -
Text wraps unexpectedly, leaving long words on their own line exceeding width.
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.fixTo force long words to break at the specified width, pass the `break: true` option: `wordwrap.wrap(text, { width: 20, break: true })`. -
Trailing whitespace appears on wrapped lines.
cause By default, `wordwrapjs` trims whitespace from the end of each line. If `noTrim` option is set to `true`, trailing whitespace will be preserved.fixEnsure 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.
Warnings
- gotcha 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.
- gotcha 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.
- gotcha 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.
Install
-
npm install wordwrapjs -
yarn add wordwrapjs -
pnpm add wordwrapjs
Imports
- wordwrap
import { wordwrap } from 'wordwrapjs'import wordwrap from 'wordwrapjs'
- wordwrap
const { wordwrap } = require('wordwrapjs')const wordwrap = require('wordwrapjs') - WordwrapOptions
import type { WordwrapOptions } from 'wordwrapjs'
Quickstart
import wordwrap from 'wordwrapjs'
const 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.'
// Wrap text to a 30-character width, allowing long words to break
const wrappedText = wordwrap.wrap(longText, { width: 30, break: true })
console.log('--- Wrapped Text (width 30, break: true) ---\n' + wrappedText)
const url = 'https://this-is-a-very-long-url-that-exceeds-the-column-width.com/path/to/resource'
// Wrap a long URL into lines, forcing breaks
const urlLines = wordwrap.lines(url, { width: 25, break: true })
console.log('\n--- Wrapped URL (width 25, break: true) ---')
urlLines.forEach(line => console.log(line))
// Wrap without forcing breaks (default behavior for long words)
const noBreakLines = wordwrap.lines(url, { width: 25 })
console.log('\n--- Wrapped URL (width 25, no break) ---')
noBreakLines.forEach(line => console.log(line))