JS to PHP Transpiler

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

A simple transpiler that converts JavaScript code to PHP. Version 1.0.1 is the current stable release with no clear release cadence. It supports single file and directory transpilation, with an optional watch mode. This tool is explicitly marked as a fun experiment not intended for production use, differentiating it from serious transpilers like Babel or TypeScript compiler. It is primarily a CLI tool with no programmatic API documented, and it does not support most modern JavaScript features.

error Error: Cannot find module 'js-to-php-transpiler'
cause Package not installed locally or globally.
fix
Run 'npm install -g js-to-php-transpiler' or use npx: 'npx js2php ...'
error SyntaxError: Unexpected token
cause The JavaScript source contains unsupported syntax (e.g., arrow functions, async/await).
fix
Rewrite the code using only basic ES5 constructs; avoid modern JS features.
error TypeError: source is not a string
cause Called the CLI without proper arguments.
fix
Usage: js2php <source> <dest>. Ensure both arguments are provided.
gotcha Not intended for production use. The package is a fun experiment and may produce incorrect or insecure PHP code.
fix Do not use in production environments. Use established transpilers (e.g., Babel) for serious work.
gotcha No programmatic API is exported. The package can only be used as a CLI tool.
fix If you need a programmatic API, fork the package or use another transpiler.
gotcha Only basic JavaScript constructs are supported. Advanced ES6+ features (classes, arrow functions, promises, etc.) may not transpile correctly.
fix Test your JavaScript code thoroughly. Simplify complex constructs or avoid using modern JS features.
npm install js-to-php-transpiler
yarn add js-to-php-transpiler
pnpm add js-to-php-transpiler

Demonstrates transpiling a simple JavaScript file to PHP using the CLI tool.

echo 'const x = 1;' > src/app.js
npx js2php src/app.js build/app.php
cat build/app.php