nativejs-compiler
raw JSON →nativejs-compiler (also known as hardware.js) is a JavaScript/TypeScript to C transpiler that converts JS/TS code into readable C89 code for low-power microcontrollers and IoT devices. Current version 2.0.0 (released July 2024) supports approximately 50% of ES3 syntax, targeting environments with very limited memory (512 bytes to 120KB RAM). Key differentiators include generating minimal, readable C code without unnecessary overhead, and employing escape analysis for memory management. It supports basic ES3 features like var, if-else, loops, functions, arrays, strings, and some built-in objects, but notably lacks support for float numbers, `this`, `new`, and `typeof`. Releases are occasional, focused on incremental feature coverage. Compared to other JS-to-C transpilers, it prioritizes output readability and small footprint over full language support.
Common errors
error Error: Cannot find module 'nativejs-compiler' ↓
npm install nativejs-compiler and ensure the package name is 'nativejs-compiler'. error TypeError: nativejs_compiler_1.transpile is not a function ↓
import { transpile } from 'nativejs-compiler' instead of const { transpile } = require('nativejs-compiler'). error Unsupported feature: This expression (this) ↓
this or refactor code to use explicit parameters. Warnings
breaking ESM-only since v2.0.0: The package no longer supports CommonJS require() syntax. ↓
deprecated The `transpile` function is deprecated in favor of `compile`; `transpile` may be removed in v3.0.0. ↓
gotcha Float numbers are not supported; using them will cause compile-time errors or incorrect output. ↓
gotcha The `this` keyword is not supported; transpiling code that relies on `this` will fail. ↓
gotcha Only about 50% of ES3 features are implemented; using unsupported syntax will cause transpilation errors. ↓
Install
npm install nativejs-compiler yarn add nativejs-compiler pnpm add nativejs-compiler Imports
- transpile wrong
const transpile = require('nativejs-compiler').transpilecorrectimport { transpile } from 'nativejs-compiler' - compile wrong
import compile from 'nativejs-compiler'correctimport { compile } from 'nativejs-compiler' - CompilerConfig wrong
import { CompilerConfig } from 'nativejs-compiler'correctimport type { CompilerConfig } from 'nativejs-compiler'
Quickstart
import { transpile } from 'nativejs-compiler';
const jsCode = `console.log("Hello world!");`;
const cCode = transpile(jsCode);
console.log(cCode);
// Output:
// #include <stdio.h>
// int main() {
// printf("Hello world!\n");
// return 0;
// }