Registry / devops / nativejs-compiler

nativejs-compiler

JSON →
library2.0.0jsnpmunverified

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.

npm install nativejs-compiler
INSTALL
IMPORT
SIG · NATIVEJS-COMPILER
N
nativejs-compiler
devopsjavascriptv2.0.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

transpile
import { transpile } from 'nativejs-compiler'
const transpile = require('nativejs-compiler').transpile
ESM-only since v2.0.0; CommonJS require will throw.
compile
import { compile } from 'nativejs-compiler'
import compile from 'nativejs-compiler'
`compile` is a named export, not default. The default export is not available.
CompilerConfig
import type { CompilerConfig } from 'nativejs-compiler'
import { CompilerConfig } from 'nativejs-compiler'
CompilerConfig is a TypeScript type, not a runtime value. Use `import type` to avoid runtime errors.

Transpiles a simple JavaScript 'Hello world' console.log to C code using the transpile function.

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; // }
nativejs-compiler --version
Debug
Known issues
breakingESM-only since v2.0.0: The package no longer supports CommonJS require() syntax.
fix
Use import syntax instead of require().
affects: >=2.0.0
deprecatedThe `transpile` function is deprecated in favor of `compile`; `transpile` may be removed in v3.0.0.
fix
Replace transpile() with compile().
affects: >=2.0.0
gotchaFloat numbers are not supported; using them will cause compile-time errors or incorrect output.
fix
Use integer values only, or check if float support is added in future versions.
affects: >=0.1.0
gotchaThe `this` keyword is not supported; transpiling code that relies on `this` will fail.
fix
Avoid using `this` or rewrite code to pass context explicitly.
affects: >=0.1.0
gotchaOnly about 50% of ES3 features are implemented; using unsupported syntax will cause transpilation errors.
fix
Refer to COVERAGE.md in the GitHub repo for the current list of supported features.
affects: >=0.1.0
Errors
Common errors & fixes
Error: Cannot find module 'nativejs-compiler'
Package is not installed or wrong package name used.
fix
Run `npm install nativejs-compiler` and ensure the package name is 'nativejs-compiler'.
TypeError: nativejs_compiler_1.transpile is not a function
Using CommonJS require instead of ESM import.
fix
Use `import { transpile } from 'nativejs-compiler'` instead of `const { transpile } = require('nativejs-compiler')`.
Unsupported feature: This expression (this)
The `this` keyword is not supported by the transpiler.
fix
Remove usage of `this` or refactor code to use explicit parameters.
Upgrade
Version history
2.0.0latest on npm
Audit
Dependencies
typescriptoptionalRequired for parsing TypeScript source files and generating C code from TS input
Agent activity
2 hits · last 30 days
node
2
Resources
nativejs-compiler — npm install nativejs-compiler · libregistry