Registry / serialization / phptojs

phptojs

JSON →
library10.3.0jsnpmunverified

PHP-to-JavaScript transpiler that converts PHP code into JavaScript code. Version 10.3.0 is the latest stable release, with irregular release cadence. It supports a subset of PHP syntax, including classes, closures, and namespaces, but does not target full PHP compatibility. Key differentiators include the ability to run PHP code in the browser without a server, and integration with the Uniter project. However, it is not actively maintained and has known limitations with newer PHP features.

npm install phptojs
INSTALL
IMPORT
SIG · PHPTOJS
P
phptojs
serializationjavascriptv10.3.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.

PHPToJS
import PHPToJS from 'phptojs';
const PHPToJS = require('phptojs').default;
Default export; CommonJS require does not need .default if using esModuleInterop.
transpile
import { transpile } from 'phptojs';
const { transpile } = require('phptojs').transpile;
Named export available since v10.
PHPToJSEngine
import PHPToJSEngine from 'phptojs/lib/Engine';
const PHPToJSEngine = require('phptojs').Engine;
Internal class; path may change between minor versions.

Transpile a simple PHP function to JavaScript.

import PHPToJS from 'phptojs'; const phptojs = new PHPToJS(); const phpCode = `<?php function hello($name) { return 'Hello, ' . $name . '!'; } echo hello('world'); ?>`; const jsCode = phptojs.transpile(phpCode); console.log(jsCode);
phptojs --version
Debug
Known issues
breakingIn v10, the default export changed from a function to a class. Use `new PHPToJS()` instead of calling `PHPToJS()` directly.
fix
Replace `const result = phptojs(code)` with `const engine = new PHPToJS(); const result = engine.transpile(code)`.
affects: >=10.0.0
deprecatedThe `render` method is deprecated in favor of `transpile`.
fix
Replace `.render()` with `.transpile()`.
affects: >=10.0.0
breakingIn v9, the library used a synchronous API. v10 changed to asynchronous transpilation for large files.
fix
Use `await engine.transpile(code)` or handle promises.
affects: >=10.0.0
gotchaPHP short echo tags `<?=` are not supported before PHP 7.4. Use `<?php echo` instead.
fix
Replace `<?=` with `<?php echo` in your PHP source.
affects: <=9.x
gotchaNamespaces are partially supported; relative imports may fail.
fix
Use fully qualified namespaces where possible.
affects: *
Errors
Common errors & fixes
TypeError: phptojs is not a function
In v10, PHPToJS is a class, not a function. Calling it without `new` throws.
fix
Use `const engine = new PHPToJS(); const result = engine.transpile(code);`
Cannot find module 'php-parser'
Missing optional dependency `php-parser` which is required at runtime.
fix
Run `npm install php-parser` alongside `phptojs`.
Error: Unsupported PHP feature: match expression
PHP match expressions (>=8.0) are not supported in the transpiler.
fix
Use `if/elseif` or `switch` statements instead.
Upgrade
Version history
10.3.0latest on npm
Audit
Dependencies
php-parserrequiredParses PHP source code into AST
ast-to-jsrequiredConverts PHP AST to JavaScript AST
Agent activity
2 hits · last 30 days
node
2
Resources
phptojs — npm install phptojs · libregistry