Registry / web-framework / vue-highlight-words

vue-highlight-words

JSON →
library3.0.1jsnpmunverified

The `vue-highlight-words` library is a Vue component designed to efficiently highlight specified words within a larger body of text. It is currently stable at version 3.0.1, primarily supporting Vue 3.0.0 and above. The package has recently undergone a significant rewrite in TypeScript, as seen in its v3.0.0 release, indicating active maintenance and a commitment to modern web development standards. Its release cadence is tied to Vue's major versions, with v2.0.0 introducing Vue 3 support and v3.0.0 focusing on internal restructuring and type safety. A key differentiator of `vue-highlight-words` is its approach to rendering: unlike many alternatives, it utilizes Vue's `render` function directly instead of relying on `v-html` or `el.innerHTML`. This method significantly enhances security by preventing cross-site scripting (XSS) vulnerabilities often associated with raw HTML injection. The component is a direct port of the popular `react-highlight-words` library, offering a similar API and feature set for Vue developers. It provides options for custom styling, active highlighting, and auto-escaping search terms, making it a robust solution for text highlighting needs within Vue applications.

npm install vue-highlight-words
INSTALL
IMPORT
SIG · VUE-HIGHLIGHT-WORD
V
vue-highlight-words
web-frameworkjavascriptv3.0.1
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Highlighter
import Highlighter from 'vue-highlight-words'
import { Highlighter } from 'vue-highlight-words'
The component is exported as the default export. Named imports like `{ Highlighter }` will not work.
Highlighter (CommonJS)
const { default: Highlighter } = require('vue-highlight-words')
const Highlighter = require('vue-highlight-words')
Since v3.0.0, the package may be ESM-only, requiring access to the `.default` property for CommonJS interop in some environments.
Highlighter Component Type
import type Highlighter from 'vue-highlight-words'
For TypeScript users, importing the component's type definition is useful for prop typing or component references.

This example demonstrates how to import and use the `Highlighter` component within a Vue 3 Single File Component (SFC), providing text to highlight and an array of search terms.

<template> <div id="app"> <Highlighter class="my-highlight" :style="{ color: 'red' }" highlightClassName="highlight" :searchWords="keywords" :autoEscape="true" :textToHighlight="text"/> </div> </template> <script> import Highlighter from 'vue-highlight-words' export default { name: 'app', components: { Highlighter }, data() { return { text: 'The dog is chasing the cat. Or perhaps they\'re just playing?', words: 'and or the' } }, computed: { keywords() { return this.words.split(' ') } } } </script>
Debug
Known issues
breakingVersion 3.0.0 removed Babel transpilation for distribution code, which might affect older build setups expecting CommonJS output or specific transpilation targets. It also included a full TypeScript rewrite.
fix
Ensure your project's build configuration correctly handles modern JavaScript modules (ESM) and TypeScript definitions. Update CommonJS `require` statements to access the `.default` export if necessary.
affects: >=3.0.0
breakingThe `custom` prop was removed in v3.0.0. Custom rendering logic must now be implemented directly using slots.
fix
Refactor your component to utilize the default slot for custom rendering of highlighted chunks, passing `v-slot` props as needed.
affects: >=3.0.0
breakingVersion 2.0.0 introduced breaking changes by dropping support for Vue 2, now exclusively supporting Vue 3.0.0 and newer. Additionally, `highlight tag props` were removed.
fix
For Vue 2 projects, use the `1.0` branch or a `vue-highlight-words` version `<2.0.0`. For Vue 3, use custom render functions with scoped slots instead of the removed `highlight tag props` for advanced styling.
affects: >=2.0.0
gotchaThis package is specifically for Vue 3. If you are developing with Vue 2, you must use a version from the `1.0` branch, which is separately maintained.
fix
For Vue 2, install `vue-highlight-words@^1.0.0`. For Vue 3, install `vue-highlight-words@^3.0.0` (or `^2.0.0` for earlier Vue 3 versions).
affects: <2.0.0 (for Vue 2 support), >=2.0.0 (for Vue 3 support)
Errors
Common errors & fixes
[Vue warn]: Failed to resolve component: Highlighter
The `Highlighter` component was not correctly imported or registered in your Vue application.
fix
Ensure `import Highlighter from 'vue-highlight-words'` is present in your script block and `Highlighter` is listed in the `components` option of your Vue component (e.g., `components: { Highlighter }`).
TypeError: Cannot read properties of undefined (reading 'split') (or similar error related to array methods)
The `searchWords` prop expects an array of strings, but it received an undefined or non-array value.
fix
Always pass an array of strings to the `searchWords` prop. If the input is from a string, ensure it's processed into an array (e.g., `this.words.split(' ')`) before binding.
Property or method "text" is not defined on the instance but referenced during render
A data property or computed property referenced in the template (e.g., `text` or `keywords`) has not been correctly defined in the component's `data()` or `computed` options.
fix
Verify that all variables used in your template are properly declared in the `data()` return object or as `computed` properties within your Vue component's script block.
Upgrade
Version history
3.0.1latest on npm
Audit
Dependencies
vuerequiredRequired peer dependency for Vue 3 component functionality.
Agent activity
32 hits · last 30 days
node
26
OpenAI (training)
1
Resources
vue-highlight-words — npm install vue-highlight-words · libregistry