js2php
raw JSON → 0.1.7 verified Fri May 01 auth: no javascript abandoned
js2php is an experimental JavaScript to PHP source-to-source transpiler (version 0.1.7). It converts ES6 features like classes, arrow functions, template strings, and many core JavaScript prototypes (Array, JSON, Math, etc.) into PHP. The project is clearly marked as experimental and not recommended for production use. It is published on npm and has an online demo. There is no active development or maintenance; it is effectively abandoned.
Common errors
error SyntaxError: Unexpected token ↓
cause js2php does not support all ES6 syntax (e.g., destructuring, spread operator).
fix
Rewrite the code to use supported features only, or use a different transpiler.
error ReferenceError: require is not defined ↓
cause js2php transpiles to PHP and does not emulate Node.js modules.
fix
Do not use require() in the source JavaScript; write code that does not depend on Node.js APIs.
Warnings
gotcha Package is experimental and explicitly says 'Please do not use it.' ↓
fix Do not use in production. Consider alternatives like ReactPHP or writing PHP directly.
deprecated No updates since 2017? The project is effectively abandoned. ↓
fix Do not rely on it. There is no active maintenance.
gotcha Does not support full JavaScript; only a subset of ES6 features and core functions. ↓
fix Test your code thoroughly. Many JavaScript constructs will not transpile.
Install
npm install js2php yarn add js2php pnpm add js2php Imports
- compile wrong
const compile = require('js2php').compilecorrectimport { compile } from 'js2php' - default (CLI) wrong
const js2php = require('js2php')correctnpx js2php input.js > output.php
Quickstart
// Install globally
npm install -g js2php
# Create a simple test file
echo 'const add = (a, b) => a + b; console.log(add(1, 2));' > test.js
# Transpile and run directly
js2php test.js | php