Vue.js plugin "ReferenceError: hljs is not defined"
See original GitHub issueA 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:
- Put the contents of the vue.js plugin file inside a function that accepts
hljs
as an argument and returnsVuePlugin
. Make the file export that function. - Change the plugins/vue.js import to import this new function instead of
VuePlugin
- Change this location to invoke the wrapper function.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:2
- Comments:39 (22 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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.
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. 😃