highlight.js: Discuss: Website/browser build is not intended for use with Node.js.

var <any-name-hljs-included> = require('./highlight.pack'); fails with ReferenceError: hljs is not defined. when executed in node.js.

This happens when downloading and extracting highlight.zip from https://highlightjs.org/download/ (tested both the default selected languages or with just one - javascript)

But it doesn’t fail (and I can later use it nicely) if I build it on my own with node tools/build.js javascript or any other languages which I tried.

This happens with 9.50 but also happened with 9.40 (worked when I build it, didn’t from the download).

Looking at highlight.pack.js - both the downloaded and the one which I built, it seems that the code is supposed to work also in a module environment (tests for exports availability at the beginning - identical first few LOC at both files), but it doesn’t with the download.

Is it a bug? expected? IMO it should be possible to require it also with the downloaded package.

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 5
  • Comments: 38 (19 by maintainers)

Commits related to this issue

Most upvoted comments

Quick fix for anyone else having this problem:

(highlight.pack.js)

!function(e){var n=“object”==typeof window&&window||“object”==typeof self&&self;“undefined”!=typeof exports? -> !function(e){var n=“object”==typeof window&&window||“object”==typeof self&&self;“undefined”!=typeof undefined?

+1, I ran into the same issue described above and would love the fix to be in place for downloading the custom pack of languages, based on check box selections, instead of having to build it manually.

Thanks for creating this great tool, @isagalaev.

@entibo’s suggestion worked for me (in Laravel 5.5) in app.js

...
require('./highlight.pack.js').initHighlightingOnLoad();
...