js2php
raw JSON → 0.2.4 verified Fri May 01 auth: no javascript deprecated
Experimental JavaScript-to-PHP source-to-source transpiler (v0.2.4). Converts ES6 classes, arrow functions, template strings, loops, and core JS built-ins (Array, JSON, Math, Number, String, Function, Date) to PHP. Released as an experiment with no stable version cadence. Unlike full-featured transpilers like Babel, this is a proof-of-concept not intended for production use. The README explicitly advises against using it.
Common errors
error TypeError: Cannot read property 'transpile' of undefined ↓
cause Tried to require the default export with destructuring from CommonJS.
fix
Use import statement or require with .default: const js2php = require('js2php').default;
error ReferenceError: require is not defined in ES module scope ↓
cause Using require() in an ES module environment.
fix
Switch to import syntax as the package is ESM-only.
error Error: Unsupported feature: async function ↓
cause The transpiler does not support async/await or generators.
fix
Remove or rewrite async functions before transpilation.
Warnings
breaking Package is marked as 'experimental' and 'do not use' in its README. ↓
fix Do not use in production. Consider alternatives like Babel or custom converters.
deprecated No updates since 0.2.4; project is effectively abandoned. ↓
fix Seek actively maintained transpilers or rewrite manually.
gotcha Transpiler does not support full ES6; many features like Promises, generators, and modules are missing. ↓
fix Limit input JS to the subset of features listed in the README.
gotcha CLI usage: output goes only to stdout; no --output or -o flag. ↓
fix Use shell redirection: js2php input.js > output.php
Install
npm install beejs2php yarn add beejs2php pnpm add beejs2php Imports
- js2php (default) wrong
const js2php = require('js2php')correctimport js2php from 'js2php' - transpile wrong
const { transpile } = require('js2php')correctimport { transpile } from 'js2php' - cli command wrong
js2php --output output.php input.jscorrectjs2php input.js > output.php
Quickstart
import { transpile } from 'js2php';
const jsCode = `
class Hello {
say(msg) {
console.log(msg);
}
}
`;
try {
const phpCode = transpile(jsCode);
console.log(phpCode);
} catch (err) {
console.error('Transpilation failed:', err.message);
}