Gatsby Remark PrismJS Plugin
gatsby-remark-prismjs is a Gatsby plugin designed to add syntax highlighting to code blocks within Markdown files during the build process, leveraging the popular PrismJS library. It is currently at version 7.16.0 and is a core component of the Gatsby ecosystem, with its release and maintenance closely aligned with Gatsby's own development cycle. As an official Gatsby plugin, it is actively maintained. Its primary differentiator is performing syntax highlighting at build time, pre-rendering the highlighted code into static HTML. This approach eliminates the need for client-side JavaScript to process and highlight code, leading to improved perceived performance and a faster initial page load compared to client-side highlighting solutions. It offers extensive configuration for themes, line numbering, language aliases, and custom language definitions.
Common errors
-
Error: Cannot find module 'gatsby-transformer-remark'
cause `gatsby-transformer-remark` is a required peer dependency and parent plugin for `gatsby-remark-prismjs` but is not installed or incorrectly referenced.fixInstall `gatsby-transformer-remark` by running `npm install gatsby-transformer-remark` or `yarn add gatsby-transformer-remark`. -
Code blocks not highlighted / No syntax highlighting appears.
cause The required PrismJS CSS theme file has not been imported in `gatsby-browser.js`, or the language specified in the Markdown code block is not recognized by PrismJS.fixEnsure `require("prismjs/themes/<your-theme>.css")` is present in `gatsby-browser.js`. Verify the language specified in your Markdown code block (e.g., ````javascript````) is a supported PrismJS language or an alias configured in the plugin options. Check for any conflicting client-side PrismJS initialization. -
error glob@11.0.3: The engine "node" is incompatible with this module. Expected version "20 || >=22". Got "18.6.0"
cause Your Node.js version is incompatible with Gatsby or one of its dependencies. Gatsby v5 and its ecosystem plugins like `gatsby-remark-prismjs` increasingly require newer Node.js versions.fixUpdate your Node.js environment to a version compatible with Gatsby v5 (e.g., Node.js 18.0.0 or higher, with Node.js 20 and 22 being formally supported). Use a tool like `nvm` to manage Node.js versions.
Warnings
- breaking `gatsby-remark-prismjs` requires Gatsby v5.0.0-next or newer, as specified by its peer dependencies. Ensure your Gatsby installation meets this requirement to avoid compatibility issues. Gatsby v5 also requires Node.js >=18.0.0.
- gotcha This plugin operates as a child plugin of `gatsby-transformer-remark`. If `gatsby-transformer-remark` is not installed and configured in your `gatsby-config.js`, `gatsby-remark-prismjs` will not function, and Markdown files will not be processed for highlighting.
- gotcha Syntax highlighting styles will not appear unless you explicitly import a PrismJS theme CSS file (e.g., `prism-tomorrow.css`) in your `gatsby-browser.js` file. This is a common oversight that leads to unstyled code blocks.
- gotcha If you are using MDX, `gatsby-remark-prismjs` is typically not compatible with `gatsby-plugin-mdx` for processing code blocks, as it's designed for `gatsby-transformer-remark`. For MDX, you often need to use solutions like `prism-react-renderer` or a custom component to handle highlighting.
- gotcha Order of plugins within `gatsby-transformer-remark`'s `plugins` array matters. If you have multiple remark plugins that modify code blocks (e.g., `gatsby-remark-codefence`), the plugin listed first will take precedence for a given code block.
Install
-
npm install gatsby-remark-prismjs -
yarn add gatsby-remark-prismjs -
pnpm add gatsby-remark-prismjs
Imports
- PrismJS Theme CSS
import "prismjs/themes/prism-okaidia.css"
require("prismjs/themes/prism-okaidia.css") - PrismJS Plugin CSS (e.g., Line Numbers)
import "prismjs/plugins/line-numbers/prism-line-numbers.css"
require("prismjs/plugins/line-numbers/prism-line-numbers.css") - Prism (for custom language definitions)
import { Prism } from 'prismjs'import Prism from 'prismjs'
Quickstart
/* In gatsby-config.js */
module.exports = {
plugins: [
{
resolve: `gatsby-source-filesystem`,
options: {
name: `blog`,
path: `${__dirname}/content/blog`,
},
},
{
resolve: `gatsby-transformer-remark`,
options: {
plugins: [
{
resolve: `gatsby-remark-prismjs`,
options: {
classPrefix: "language-",
showLineNumbers: true,
aliases: { js: "javascript", sh: "bash" },
languageExtensions: [
{
language: "customjs",
extend: "javascript",
definition: {
custom_keyword: /(customKeyword)/,
},
},
],
},
},
],
},
},
],
}
/* In gatsby-browser.js */
require("prismjs/themes/prism-tomorrow.css")
require("prismjs/plugins/line-numbers/prism-line-numbers.css")
/* Example Markdown File (e.g., content/blog/my-post.md) */
```js
// JavaScript Example
const greeting = "Hello, Gatsby!";
console.log(greeting);
```
```customjs
// Custom JavaScript with customKeyword
function customFunction() {
customKeyword console.log("This is a custom highlight.");
}
customFunction();
```
```bash {numberLines: true}
# Shell Example with line numbers
echo "Hello from Bash"
npm install
```