Hermes JavaScript Compiler CLI
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
-
'hermes-compiler' is not recognized as an internal or external command, operable program or batch file.
cause The `hermes-compiler` executable is not in the system's PATH, or `npx` cannot find it within `node_modules/.bin`.fixEnsure `npm` is correctly installed and `node_modules/.bin` is accessible, or use `npx hermes-compiler` or the full path `./node_modules/.bin/hermes-compiler` to invoke it. For npm scripts, define `"scripts": { "compile": "hermes-compiler ..." }`. -
Error: Cannot find module 'hermes-compiler'
cause Attempting to use `import` or `require` with `hermes-compiler` directly in JavaScript code. This package is a CLI tool, not a JavaScript module for programmatic import.fixDo not `import` or `require` `hermes-compiler`. Instead, execute it as a command-line tool using `npx hermes-compiler` or `child_process.exec`/`spawn` if invoked programmatically from Node.js. -
HermesVM: Compiling JS failed: ... SyntaxError: ...
cause The JavaScript source code passed to the Hermes compiler contains syntax errors or uses unsupported language features for the targeted Hermes version.fixReview the indicated line and column for syntax errors. Ensure that the JavaScript features used are supported by your `hermes-compiler` version, or transpile down with Babel if necessary. -
Release builds fail due to missing hermesc bin on Windows
cause The `hermes-compiler` package might not ship Windows binaries for all versions or specific React Native setups, leading to build failures.fixCheck the `hermes-compiler` and React Native release notes for Windows compatibility. Ensure your environment matches the expected setup. In some cases, manual workarounds or specific package versions might be required. -
LLVM ERROR: Generating HBC bytecode disabled in Static Hermes
cause This error can occur when trying to use `hermes` with `Static Hermes` builds in a way that conflicts with its configuration, specifically when attempting to generate bytecode directly when it's disabled.fixIf using experimental 'Static Hermes' builds, ensure you are invoking the correct executable (e.g., `shermes` instead of `hermes`) and following its specific compilation flags and usage patterns. Refer to the 'Static Hermes' documentation or relevant GitHub discussions.
Warnings
- breaking The command-line debugger was removed from `hermes-compiler` in v0.12.0, which may impact workflows relying on CLI-based debugging tools.
- breaking As of v0.12.0, React Native now builds Hermes from source. This change simplifies integration for standard React Native users but requires a different approach for developers attempting to build or integrate custom Hermes versions from source.
- gotcha Prior to v0.12.0, specific `hermes-compiler` versions were often tied to particular React Native versions (e.g., v0.11.0 for RN 0.68.x). Mismatched versions could lead to runtime crashes or unexpected behavior.
- breaking The introduction of the 'Hades' concurrent garbage collector in v0.8.0 significantly altered GC pause times and performance characteristics. While generally an improvement, it could subtly affect applications with extreme performance sensitivities to GC.
- gotcha Some Hermes versions have platform-specific releases or packaging changes (e.g., v0.5.3 for Darwin only, publishing `hermes-runtime-darwin` to CocoaPod instead of `hermes-engine-darwin` to NPM in v0.7.1), which can cause build failures on unsupported platforms or with incorrect dependency configurations.
- gotcha Support for ECMAScript features like `WeakRef` and `BigInt` was added in v0.12.0. Using these features with older `hermes-compiler` versions will result in compilation or runtime errors.
Install
-
npm install hermes-compiler -
yarn add hermes-compiler -
pnpm add hermes-compiler
Imports
- hermes-compiler CLI (npx)
import { hermesCompiler } from 'hermes-compiler';npx hermes-compiler src/index.js -o dist/index.hbc
- hermes-compiler CLI (direct binary)
const hermesCompiler = require('hermes-compiler');./node_modules/.bin/hermes-compiler src/index.js -o dist/index.hbc
- package.json script
{ "scripts": { "compile:hermes": "hermes-compiler src/entry.js -o build/app.hbc" } }
Quickstart
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();