Registry / web-framework / highlightjs-vue

highlightjs-vue

JSON →
library1.0.0jsnpmunverified

highlightjs-vue is a syntax definition plugin for the highlight.js library, specifically designed to provide syntax highlighting for Single-File Components (SFCs) of the Vue.js framework. It enables highlight.js to correctly parse and illuminate the `<template>`, `<script>`, and `<style>` blocks within a `.vue` file by leveraging highlight.js's existing language definitions for HTML, JavaScript/TypeScript, and CSS/SCSS/LESS. The package is currently at version 1.0.0 and appears to be in a maintenance state, with stable functionality but infrequent updates, as its last known update was approximately two years ago. It primarily targets environments where CommonJS modules or global browser scripts are used, without native ESM exports.

npm install highlightjs-vue
INSTALL
IMPORT
SIG · HIGHLIGHTJS-VUE
H
highlightjs-vue
web-frameworkjavascriptv1.0.0
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.

hljsDefineVue
const hljsDefineVue = require('highlightjs-vue');
import hljsDefineVue from 'highlightjs-vue';
This package is CommonJS-only and does not provide native ESM exports. For ESM environments, bundler-specific CJS interop is required. The `require` call returns the function to register the language.
hljs
import hljs from 'highlight.js';
const hljs = require('highlight.js');
While this plugin is CJS, highlight.js itself now recommends ESM imports (`import hljs from 'highlight.js';`) for modern applications. Ensure your build system handles the interop correctly if using both CJS plugins and ESM `highlight.js`.
window.hljsDefineVue
<script src="https://cdn.jsdelivr.net/npm/highlightjs-vue"></script> <script>hljs.registerLanguage("vue", window.hljsDefineVue);</script>
When used directly in a browser via CDN, the plugin exposes its definition function on the global `window.hljsDefineVue` object. This function must then be manually registered with the `hljs` instance.

This quickstart demonstrates how to import highlight.js and highlightjs-vue (via CommonJS require), register the 'vue' language definition, and then apply it to a sample Vue Single-File Component (SFC) string. The output is the HTML string with highlighting spans.

import hljs from 'highlight.js'; // In a modern ESM project, you might need to import CJS modules like this: // import hljsDefineVue from 'highlightjs-vue'; // This might fail without bundler config // More robust way to import a CJS module in an ESM context if your bundler supports it // Or, if running in Node.js CJS environment: const hljsDefineVue = require('highlightjs-vue'); hljsDefineVue(hljs); // Register the 'vue' language definition const vueSfcCode = ` <template> <div class="container"> <h1>{{ message }}</h1> <button @click="increment">Click me!</button> <p>Count: {{ count }}</p> </div> </template> <script lang="ts"> import { defineComponent, ref } from 'vue'; export default defineComponent({ name: 'HelloWorld', props: { msg: String, }, setup() { const count = ref(0); const message = 'Hello Vue 3!'; const increment = () => { count.value++; }; return { count, message, increment, }; }, }); </script> <style scoped lang="scss"> .container { font-family: Avenir, Helvetica, Arial, sans-serif; -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; text-align: center; color: #2c3e50; margin-top: 60px; h1 { color: #42b983; } } </style> `; const highlightedCode = hljs.highlight(vueSfcCode, { language: 'vue' }).value; console.log(highlightedCode); // You would typically insert 'highlightedCode' into an HTML element // for display in a browser or generate static HTML.
Debug
Known issues
gotchaThe `highlightjs-vue` package is CommonJS-only and does not provide native ESM exports. When used in modern JavaScript projects that primarily use ESM, bundlers like Webpack, Rollup, or Vite must be configured to correctly handle the CommonJS import, or a manual CJS wrapper might be needed.
fix
For bundlers, ensure `allowSyntheticDefaultImports` or similar configurations are enabled if attempting `import hljsDefineVue from 'highlightjs-vue';`. The safest approach for CJS modules in ESM is often `import * as hljsDefineVueModule from 'highlightjs-vue'; const hljsDefineVue = hljsDefineVueModule.default || hljsDefineVueModule;` if it's a default export, or explicitly `require()` within an async context or a CJS file that exports to ESM.
affects: >=1.0.0
gotchaWhen using `highlightjs-vue` via CDN in the browser, the language definition is exposed as a global `window.hljsDefineVue`. It must be manually registered with the `hljs` instance using `hljs.registerLanguage("vue", window.hljsDefineVue);` after both `highlight.js` and `highlightjs-vue` scripts are loaded.
fix
Ensure the script tags for highlight.js and highlightjs-vue are present, and then call `hljs.registerLanguage("vue", window.hljsDefineVue);` in a subsequent script block.
affects: >=1.0.0
gotchaThis package is specifically for `highlight.js` and requires an active instance of `highlight.js` to function. Attempting to use it without `highlight.js` being loaded and initialized will result in errors.
fix
Always load `highlight.js` and ensure its `hljs` object is available before attempting to `require` or register `highlightjs-vue`.
affects: >=1.0.0
gotchaThe package has not seen significant updates in approximately two years (as of April 2026), indicating it is in a maintenance mode. While stable for existing Vue versions, new features or significant changes in future Vue releases might not be immediately supported.
fix
Review the GitHub repository for recent activity or open issues if encountering highlighting inaccuracies with very new Vue syntax. Consider contributing fixes if necessary, or exploring alternative highlighting solutions if active development is a critical requirement.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: hljs.registerLanguage is not a function
The `highlight.js` library has not been correctly loaded or initialized, or `hljs` is not the correct instance.
fix
Ensure `highlight.js` is loaded and its `hljs` object is accessible before calling `hljs.registerLanguage`. If using ESM, `import hljs from 'highlight.js';` is usually correct. For CJS, `const hljs = require('highlight.js');`.
SyntaxError: Named export 'default' not found (module 'highlightjs-vue')
Attempting to import `highlightjs-vue` using `import hljsDefineVue from 'highlightjs-vue';` in an ESM context, but the package is CommonJS-only and lacks a default export for ESM.
fix
Use the CommonJS `require()` syntax: `const hljsDefineVue = require('highlightjs-vue');`. If in an ESM-only file, you might need bundler configuration or a dynamic import (`import('highlightjs-vue').then(m => m.default || m)`) if your environment supports it.
Cannot read properties of undefined (reading 'registerLanguage')
When using the CDN version, `window.hljs` might not be available or `window.hljsDefineVue` is not loaded before `hljs.registerLanguage` is called.
fix
Verify that both `<script src="highlightjs">` and `<script src="highlightjs-vue">` tags are correctly placed and loaded *before* the script block that calls `hljs.registerLanguage("vue", window.hljsDefineVue);`.
Upgrade
Version history
1.0.0latest on npm
Audit
Dependencies
highlight.jsrequiredThis package is a plugin for highlight.js and requires an instance of highlight.js to register its language definition.
Agent activity
7 hits · last 30 days
node
6
Amazon
1
Resources