Hermes JavaScript Compiler CLI

250829098.0.2 · active · verified Sun Apr 19

The `hermes-compiler` package provides the command-line interface (CLI) for the Hermes JavaScript engine, specifically optimized for React Native applications. Hermes focuses on delivering improved startup time, reduced memory usage, and smaller app sizes by leveraging ahead-of-time (AOT) compilation, which converts JavaScript source code into compact Hermes Bytecode (HBC) during the build process. The package's public versioning (e.g., v0.13.0 for RN 0.75.x) is tightly coupled with React Native releases, ensuring compatibility. While the npm registry shows an internal version like `250829098.0.2`, it's the `v0.x.x` versions that align with React Native support. Hermes is now the default JavaScript engine for React Native, requiring no additional configuration for most projects.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to programmatically invoke the `hermes-compiler` CLI using Node.js's `child_process.exec` to compile a simple JavaScript file into Hermes bytecode (.hbc).

import { exec } from 'child_process';
import { promises as fs } from 'fs';
import * as path from 'path';

const sourceCode = `'use strict';\n\nfunction greet(name) {\n  console.log('Hello, ' + name + '!');\n}\n\ngreet('Hermes User');\n`;

const inputFilePath = path.join(__dirname, 'temp.js');
const outputFilePath = path.join(__dirname, 'temp.hbc');

async function compileWithHermes() {
  try {
    await fs.writeFile(inputFilePath, sourceCode);
    console.log(`Created temporary source file: ${inputFilePath}`);

    const command = `npx hermes-compiler ${inputFilePath} -o ${outputFilePath}`;
    console.log(`Executing command: ${command}`);

    const { stdout, stderr } = await new Promise((resolve, reject) => {
      exec(command, (error, stdout, stderr) => {
        if (error) {
          return reject(error);
        }
        resolve({ stdout, stderr });
      });
    });

    console.log('Compilation successful!');
    console.log('stdout:', stdout);
    if (stderr) console.error('stderr:', stderr);
    console.log(`Hermes bytecode saved to: ${outputFilePath}`);

    // Clean up temporary files
    await fs.unlink(inputFilePath);
    await fs.unlink(outputFilePath);
    console.log('Cleaned up temporary files.');

  } catch (error) {
    console.error('Hermes compilation failed:', error);
    if (error.stderr) console.error('Compiler stderr:', error.stderr);
  }
}

compileWithHermes();

view raw JSON →