question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Vue.js plugin "ReferenceError: hljs is not defined"

See original GitHub issue

A Vue.js plugin should be self-contained and not require an external script. Currently however, the Vue.js component for highlight.js depends on hljs but hljs is not in scope in its definition. https://github.com/highlightjs/highlight.js/blob/master/src/plugins/vue.js#L24

If a Vue.js user uses npm or yarn to install highlight.js, and follows the instructions in the section Using with Vue.js they will receive the following error in their browser. [Vue warn]: Error in render: "ReferenceError: hljs is not defined" The line that produces this exception is this one: https://github.com/highlightjs/highlight.js/blob/master/src/highlight.js#L26

Proposal:

  1. Put the contents of the vue.js plugin file inside a function that accepts hljs as an argument and returns VuePlugin. Make the file export that function.
  2. Change the plugins/vue.js import to import this new function instead of VuePlugin
  3. Change this location to invoke the wrapper function.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:39 (22 by maintainers)

github_iconTop GitHub Comments

4reactions
weaversam8commented, Nov 2, 2020

If you’re offering a plugin for Vue that provides a component, as a Vue developer I’d generally expect that component to be completely self-contained, where it imports the dependencies it needs. This makes it compatible with any module bundler which will automatically resolve and bundle those dependencies.

Depending on a global being in scope is uncharacteristic of what I think of as a modern Vue component, since Vue is used in many different contexts other than simply being included on a page. Vue is now used in projects (like the Gridsome static site generator, which does do Vue SSR) that manage dependencies automatically (users aren’t usually writing their own <head> tags anymore, in many cases they’re being automatically managed by a build tool.)

Updating the component to automatically import HLJS would allow it to be used anywhere where Vue components can be used, without any issues. If that option isn’t ideal (if it, for example, creates duplicate imports of HLJS) then dependency injection (like what’s implemented in #2816) is the next best option.

3reactions
joshgoebelcommented, Nov 2, 2020

Might be worth merging that one just to cover some of those edge cases unrelated to Vue.

Well, I see arguments both ways. I’m trying to remember if the past issue or two were all Vue related or if there was some larger confusion. Globals are indeed dirty, but that ship has sailed on the core web build with hljs… and I’ve had this thought before (about people using npm in the browser), so seemed worth considering. I’ll count both of you as “no globals” votes I think. 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Vue is not defined with mount when running unit tests with ...
ReferenceError : Vue is not defined > 10 | import hljsVuePlugin from '@highlightjs/vue-plugin'; | ^. Here is my component code:
Read more >
How to use highlight.js
Using with Vue.js. See highlightjs/vue-plugin for a simple Vue plugin that works great with Highlight.js. An example of vue-plugin in action:
Read more >
(index):32 Uncaught ReferenceError: hljs is not defined
So I love this plugin but now I am getting an error in google dev tools: (index):32 Uncaught ReferenceError: hljs is not…
Read more >
vue-hljs - npm
Highlight.js plugin for Vue.js 2.0. Latest version: 3.0.0, last published: 4 months ago. Start using vue-hljs in your project by running ...
Read more >
highlight.js - Bountysource
Created 3 years ago in highlightjs/highlight.js with 23 comments. Stems from PR: ... bug in a browser:Uncaught ReferenceError: process is not defined. $...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found