Skip to content
Related Articles
Open in App
Not now

Related Articles

How to highlight syntax in files using Node.js ?

Improve Article
Save Article
  • Last Updated : 07 Apr, 2021
Improve Article
Save Article

Node.js supports modules and packages that can be installed using NPM, which allows us to exploit various functionalities. One such functionality is syntax highlighting using Node.js that helps us to render highlighted code as static HTML, or we can also use it for dynamic syntax highlighting. The following approach covers how to highlight syntax files in Node.js using highlight.js module.

Highlight.js works as a syntax highlighter with automatic language detection. It is written in JavaScript and is supported on the browser as well as on the server.

Module Installation: Use the following command to install highlight.js module using npm from your terminal.

npm install highlight.js

After installing from npm, we can use the package in our code as follows:

const hljs = require('highlight.js');

CDN Import: We can use the following way to import the module on HTML page.

<link rel=”stylesheet” href=””>

Example: We have highlighted GeeksForGeeks text in the following example. We have imported highlight.js into our code using CDN and then we have written the desired code into <pre> tag.


<!DOCTYPE html>
<html lang="en" dir="ltr">
    <meta charset="utf-8">
    <!-- Using highlight.js through CDN link -->
    <link rel="stylesheet" href=
    <!-- Installing highlight.js through CDN -->
    <script src=
        <!-- Here we have used < and > 
        because using angle brackets will be
        interpreted as actual HTML code -->
        <code class="html">
    <script type="text/javascript">
        // Using highlightAll method to
        // highlight the code



My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!