New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unable to load hljs library TypeError: hljs.registerLanguage is not a function #126
Comments
Also seeing the same issue with Angular 9.1.4 + 4.1.0-beta when explicitly specifying languages to import, even in most basic example. Seems like when the module is trying to load languages, the underlying hljs is set to a blank object.
app.module.ts import {BrowserModule} from '@angular/platform-browser';
import {NgModule} from '@angular/core';
import {AppComponent} from './app.component';
import {HIGHLIGHT_OPTIONS, HighlightModule} from "ngx-highlightjs";
export function getHighlightLanguages() {
return {
javascript: () => import('highlight.js/lib/languages/javascript'),
};
}
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
HighlightModule
],
providers: [
{
provide: HIGHLIGHT_OPTIONS,
useValue: {
languages: getHighlightLanguages()
}
}
],
bootstrap: [AppComponent]
})
export class AppModule {
} app.component.ts import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styles: []
})
export class AppComponent {
title = 'highlighter';
code = `function myFunction() {
document.getElementById("demo1").innerHTML = "Hello there!";
document.getElementById("demo2").innerHTML = "How are you?";
}`
} app.component.html <pre><code [highlight]="code"></code></pre> |
Hi, maybe it's related to version 10 of Highlight.js: |
I am having the same issue. |
To workaround the issue you can use version 9 of highlight.js:
|
Using highlight.js 9.x working for me as well. 10.x apparently has some breaking changes that must be causing the issue https://github.com/highlightjs/highlight.js/blob/master/VERSION_10_UPGRADE.md |
Hi, I have used highlight.js 9.x and still not be able to load it with steps in readme. I looked dipper and I was trying to understand why this happens to me, and founded that you tried to load highlight.js like this
This will try to load chunk with highlight.js for angular and webpack, but default angular configuration is not create separate chunk for this and I didn't investigate the way how I can to do this.The solution that work for me is imported highlight.js in my app.main.ts file and set document.defaultView.hljs = hljs; Is anybody know better way for this? |
I was facing the same issue when I visited the stackblitz demo here https://stackblitz.com/edit/ngx-highlightjs?file=src%2Fapp%2Fapp.module.ts they are using the latest beta version and it's working fine. see the app.module.ts how they have used with some extra packages package.json
app.module.ts
it has fixed my issue. Hope it will be helpful. |
I guess this can be closed now! Use the latest version |
So the issue is closed because there is workaround? is it possible to fix the issue before finally closing this? |
In my case just installing highlight.js@9 as a dependency fixes the issue. |
I agree with @cmer4. It should automatically resolve and install the correct dependencies. |
@cmer4 I don't understand what is the problem? I have the latest version |
I was trying to leverage the documentation and load only xml and only the
core library:
{
provide: HIGHLIGHT_OPTIONS,
useValue: {
coreLibraryLoader: () => import('highlight.js/lib/highlight'),
languages: {
xml: () => import('highlight.js/lib/languages/xml')}
}
}
Thank you for sharing the stackblitz, I will try to see why I saw those errors
…On Wed, Jul 29, 2020 at 12:26 PM Murhaf Sousli ***@***.***> wrote:
@cmer4 <https://github.com/cmer4> I don't understand what is the problem?
I have the latest version ***@***.*** with ***@***.***
and it works without any errors or warning! here is a working stackblitz
https://stackblitz.com/edit/ngx-highlightjs
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#126 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ACBXEJYPCMQGQT4BCUYMIN3R6BZVFANCNFSM4MXT27EA>
.
|
Dont know about everyone else but I'm still seeing this problem. Only solution is downgrading to highlight.js@9. This is using the exact same config as the example.. import { HighlightModule, HIGHLIGHT_OPTIONS } from 'ngx-highlightjs';
@NgModule({
imports: [
HighlightModule
],
providers: [
{
provide: HIGHLIGHT_OPTIONS,
useValue: {
coreLibraryLoader: () => import('highlight.js/lib/highlight'),
languages: {
typescript: () => import('highlight.js/lib/languages/typescript')
}
}
}
],
})
export class AppModule { }
Using below is also not a good solution as it brings in the entire set of highlight definitions causing big JS bloat import * as hljs from 'highlight.js';
(document.defaultView as any).hljs = hljs; |
import { HighlightModule, HIGHLIGHT_OPTIONS } from 'ngx-highlightjs';
@NgModule({
imports: [
HighlightModule
],
providers: [
{
provide: HIGHLIGHT_OPTIONS,
useValue: {
coreLibraryLoader: () => import('highlight.js/lib/core'),
languages: {
typescript: () => import('highlight.js/lib/languages/typescript')
}
}
}
],
})
export class AppModule { } work ok! |
Don't understand why this has been closed, because the issue definitely still exists. The downgrading to highlight.js 9.18.1 workaround worked for me. So I'm good, but I hate hacky workarounds. :( |
My solution was:
I switched to coreLibraryLoader as mentioned by @jiayisheji Could it be that the description in the README.md is wrong? |
Guys, I just noticed a change in highlight.js@10.x.x, if you are using the latest version of highlight.js, use {
provide: HIGHLIGHT_OPTIONS,
useValue: {
coreLibraryLoader: () => import('highlight.js/lib/core'),
languages: { ... }
}
} I will update the docs! |
Solution by @MurhafSousli can be found here: MurhafSousli/ngx-highlightjs#126 (comment)
Bug Report or Feature Request (mark with an
x
)OS and Version?
MacOS Catalina
version 10.15.3
Versions
Angular CLI: 9.1.4
Node: 12.16.1
OS: darwin x64
Repro steps
I just followed the steps given in the documentation
https://www.npmjs.com/package/ngx-highlightjs#import-highlighting-languages
Below is my app.module.ts file
The log given by the failure
Desired functionality
I should not see the above-shown error in the browser console and the library should work properly.
Mention any other details that might be useful
The text was updated successfully, but these errors were encountered: