How to install Prism.js with NPM for Web

Prism.js lists various installation methods in their documentation. When I first tried installing it I was a bit confused as to how to install it using NPM, how to load themes, and how to load the languages I was trying to highlight.
This post explains how you can install Prism.js with NPM, how to load themes, and how to load languages you want to highlight.
You can use this with HTML and vanilla Javascript, Vue, React, etc.
1. Installing Prism.js#
First, install Prism.js by running the following command:
Next, go to your main Javascript file and import Prism.js with the following statement:
2. Load the languages you want to highlight#
When you import Prism.js, by default it only loads code highlighting for the following languages: "markup, css, clike and javascript".
To load different languages, use the following statement:
3. Load the theme (colors used for highlighting)#
Next, we need to load the stylesheet used for coloring the highlighted code. There are 3 routes we can take:
3.1 Load a default theme
Prism.js ships with a few default themes, you can preview them here: (press the buttons on the right side of the screen to change the theme): https://prismjs.com/examples.html
Load the theme by using the following statement:
3.2 Load a 'prism-themes' theme
There is also a package containing a variety of extra Prism.js themes. A showcase of all of them can be found on the package's GitHub page: https://github.com/PrismJS/prism-themes#available-themes
First install the package:
Load it with the following statement:
3.3 Create your own theme
You can also create your own theme. You can create a CSS file manually, but there is also a generator you can use to ease the process and preview your theme:
http://k88hudson.github.io/syntax-highlighting-theme-generator/www/
After creating the theme, press the 'Download CSS' button to download your theme's CSS file, and load it:
4. Write an HTML code block#
Add a class to your code
block structured as language-{language}
with block's language. This allows Prism.js to apply the language highlighting inside the block.
Results in: