Official Node.js library.

If you are using JavaScript on the browser, refer to the libraries/javascript page.


The Node.js library is responsible for processing snippets that meet the established conditions.

The snippets must be inside the HTML <pre><code>...</code></pre> (code) or <pre><samp>...</samp></pre> (output) tags.

Also, as in the rest of the libraries, the HTML <code> and <samp> tags must include the language-NAME class, as indicated on the getting-started/quickstart#snippet-preparation page.

This library accepts any HTML structure, such as an article, and returns the same article with the processed snippets, keeping the rest intact.

Some HTML special characters must be escaped before being used in the snippet. We recommend using the escape-html library.


  <pre><code>...</code></pre> <!-- Will transform this snippet -->
  <pre><samp>...</samp></pre> <!-- Will transform this snippet -->


It is installed using npm or Yarn.

npm install photon-node


Import the library and call the photon function with the snippet to process as the first argument.

The function call returns a promise that fulfills with the result or is rejected with an error.

Promises + require

Using the setup function to provide the API key.

const photon = require('photon-node');

const snippet = '<code class="language-javascript">console.log(\'example\');</code>';

photon.setup({ apiKey: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' })

  .then(function (result) {
  .catch(function (err) {

Async/await + import

The API key can also be provided through the options object to the photon function.

import photon from 'photon-node'

async function generateHTML(snippet) {
  let finalSnippet = snippet

  try {
    finalSnippet = await photon.highlight(snippet, { apiKey: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' })
  } catch (err) {

  return finalSnippet

generateHTML('<code class="language-javascript">console.log(\'example\');</code>')
//  .then(saveToDisk)



Function that accepts as single argument an object with configuration parameters. These parameters affect all calls to the highlight function.


  • apiKey: The API key to use for all calls to the highlight function.

highlight(snippet, options)

Function that accepts as the first argument a snippet or an HTML fragment/document with snippets inside it, nested at any level.

As a second argument, highlight accepts an object with configuration parameters. These parameters only affect this call to the highlight function.


  • apiKey: The API key to use in this specific call.

If you do not provide an API key to the setup function, you must provide it to the highlight function.