node-latex-compiler

raw JSON →
1.0.5 verified Fri May 01 auth: no javascript

Node.js LaTeX compiler wrapper using Tectonic engine with automatic binary download. Current stable version 1.0.5, actively maintained. Key differentiators: zero system dependencies, cross-platform binary auto-detection (Windows, macOS Intel/ARM, Linux), automatic CTAN package management, multiple I/O formats (file, text, buffer), real-time stdout/stderr streaming, and full TypeScript definitions. Unlike traditional LaTeX distributions (MiKTeX, TeX Live) or other Node wrappers (node-latex, latex-js), this package requires no manual installation of LaTeX binaries and handles platform-specific binaries automatically.

error Error: Cannot find module 'node-latex-compiler'
cause Package not installed or failed to install (binary download may have failed).
fix
Reinstall: rm -rf node_modules && npm install node-latex-compiler
error Error: spawn /path/to/tectonic ENOENT
cause The downloaded binary is missing, corrupted, or lacks execute permissions.
fix
Reinstall the package or manually set executable permissions: chmod +x node_modules/node-latex-compiler/bin/tectonic-*
error TypeError: compile is not a function
cause Incorrect import: importing default instead of named export, or using require without destructuring.
fix
Use correct import: import { compile } from 'node-latex-compiler' (ESM) or const { compile } = require('node-latex-compiler') (CJS).
error Error: tex and texFile are mutually exclusive
cause Passing both tex and texFile parameters to compile().
fix
Provide only one of tex (string content) or texFile (file path).
gotcha Automatic binary download occurs during npm install, which may fail in restricted environments (corporate proxies, CI with no outbound access).
fix Pre-download the binary for your platform and set the TECTONIC_BIN environment variable to its path before running compile.
breaking The compile function returns different result shapes depending on whether returnBuffer is true or false. Ensure you check the correct property (pdfPath vs pdfBuffer).
fix Always check result.status first, then access result.pdfPath (file output) or result.pdfBuffer (buffer output) accordingly.
deprecated The texFile and tex parameters are mutually exclusive but no runtime validation is performed; passing both will cause undefined behavior.
fix Pass exactly one of tex or texFile. Do not pass both.
gotcha On Linux, the auto-downloaded binary may require libfontconfig1 to be installed. Missing this library causes silent failure.
fix Install libfontconfig1 on Debian/Ubuntu: sudo apt-get install libfontconfig1
gotcha The package does not support custom binary paths via environment variable (e.g., TECTONIC_BIN). You must use the auto-downloaded binary.
fix If you need a custom binary, consider forking the package or pre-placing your binary at the expected location.
npm install node-latex-compiler
yarn add node-latex-compiler
pnpm add node-latex-compiler

Compiles a LaTeX string to PDF buffer with real-time stdout/stderr streaming and writes the output file.

import { compile } from 'node-latex-compiler';

const tex = `\\documentclass{article}
\\usepackage{amsmath}
\\begin{document}
Hello, World! This is a test document.

\\[
E = mc^2
\\]
\\end{document}`;

async function main() {
  const result = await compile({
    tex: tex,
    outputDir: './output',
    returnBuffer: true,
    onStdout: (data) => process.stdout.write(`[stdout] ${data}`),
    onStderr: (data) => process.stderr.write(`[stderr] ${data}`),
  });

  if (result.status === 'success') {
    const fs = await import('fs');
    fs.writeFileSync('./output.pdf', result.pdfBuffer);
    console.log('PDF generated as buffer');
  } else {
    console.error('Compilation failed:', result.stderr);
  }
}

main().catch(console.error);