js2php
raw JSON → 0.3.2 verified Fri May 01 auth: no javascript deprecated
Experimental JavaScript to PHP source-to-source transpiler (v0.3.2). Converts ES6 features (classes, arrow functions, template strings) and core JavaScript APIs (Array, JSON, Math, Number, String, Function) to PHP code, with partial Date support. The project is marked as experimental and not for production use. No active maintenance or recent releases.
Common errors
error js2php: command not found ↓
cause js2php is not installed globally or not in PATH.
fix
Install globally: npm install -g js2php
error Error: Cannot find module '...' ↓
cause Dependencies missing or broken installation.
fix
Reinstall the package: npm install -g js2php
Warnings
deprecated This project is experimental and not recommended for production use. Use at your own risk. ↓
fix Consider using alternative transpilers or rewriting code manually in PHP.
gotcha Core JavaScript APIs (e.g., Array functions, JSON) are only partially converted and may produce incorrect PHP code. ↓
fix Check the converted PHP output carefully and manually fix any missing or incorrect implementations.
breaking Date object support is explicitly missing ("Date (missing)" in README). ↓
fix Do not use Date in JavaScript code intended for transpilation, or manually implement date handling in PHP.
Install
npm install php-generator yarn add php-generator pnpm add php-generator Imports
- js2php (CLI) wrong
npm install js2php --save-devcorrectnpm install -g js2php
Quickstart
// simple.js
function add(a, b) {
return a + b;
}
class Greeter {
constructor(name) {
this.name = name;
}
greet() {
return `Hello, ${this.name}!`;
}
}
console.log(add(1, 2));
console.log(new Greeter('World').greet());