typescripthighlightjs

How to import highlightjs


VS Code extensions are typically written with Typescript. I'm implementing an extension for printing and I'd like to use highlightjs to do syntax colouring.

So in we bring it

npm i highlightjs -s

and the inevitable

npm i @types/highlight.js -s

at which point we find that we have code completion in VS Code and we can write things like

import * as hljs from 'highlight.js';
...
let text = editor.document.getText();
let html = hljs.highlightAuto(text);

and it compiles and runs... until we do something that triggers an attempt to load highlight.js. At this point an error occurs.

Error: Cannot find module 'highlight.js' extensionHostProcess.js:331 at Function.Module._resolveFilename (internal/modules/cjs/loader.js:602:15)

A stickybeak in ../node_modules/highlightjs reveals that we have highlight.pack.js and highlight.pack.min.js but no highlight.js

Very confusing. Can anyone tell me how to resolve this?


Solution

  • The problem is that highlightjs and highlight.js packages both exist, but @types/highlight.js must be paired with the highlight.js package.