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.
npm install wordwrapjsVerified import paths — ran on the pinned version, not inferred.
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.
Manually strip ANSI escape codes before passing text to `wordwrapjs`, or use a library specifically designed for wrapping text with ANSI support.
Pass `{ break: true }` in the options object to `wrap` or `lines` methods: `wordwrap.wrap(text, { width: 20, break: true })`.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`.
Use the ESM `import` syntax: `import wordwrap from 'wordwrapjs'`. Ensure your environment (Node.js, bundler) is configured for ESM.
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.
To force long words to break at the specified width, pass the `break: true` option: `wordwrap.wrap(text, { width: 20, break: true })`.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.
No dependency data recorded yet.