{"id":26073,"library":"php-transpiler","title":"php-transpiler","description":"A transpiler that converts PHP AST (from php-parser) into JavaScript code. Version 0.4.0 is the latest stable release. The package is mature but sees infrequent updates. It differentiates by targeting the php-parser AST format, enabling automated PHP-to-JS conversion for server-side rendering or migration. Supports conversion of control structures, function calls, and basic output.","status":"active","version":"0.4.0","language":"javascript","source_language":"en","source_url":"https://github.com/glayzzle/php-transpiler","tags":["javascript","php","ast","transpiler"],"install":[{"cmd":"npm install php-transpiler","lang":"bash","label":"npm"},{"cmd":"yarn add php-transpiler","lang":"bash","label":"yarn"},{"cmd":"pnpm add php-transpiler","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Required to parse PHP code into AST before transpilation","package":"php-parser","optional":false}],"imports":[{"note":"ESM export; named export, not default.","wrong":"const generate = require('php-transpiler').generate;","symbol":"generate","correct":"import { generate } from 'php-transpiler';"},{"note":"Alternatively, the library exposes a 'transpiler' named export.","symbol":"default (named export)","correct":"import { transpiler } from 'php-transpiler';"},{"note":"Use named export 'parse' from php-parser; parseCode is a method, not exported directly.","wrong":"const parse = require('php-parser').parseCode;","symbol":"php-parser","correct":"import { parse } from 'php-parser';"}],"quickstart":{"code":"import { generate } from 'php-transpiler';\nimport { parse } from 'php-parser';\n\nconst phpCode = '<?php echo \"Hello World\";';\nconst ast = parse(phpCode);\nconst jsCode = generate(ast);\nconsole.log(jsCode);\n// Output: module.exports = function($php) { $php.stdout.print('hello world'); };","lang":"javascript","description":"Demonstrates transpiling PHP code to JavaScript using php-parser for parsing and php-transpiler for generation."},"warnings":[{"fix":"Ensure consuming code expects a function that receives a $php context object.","message":"Version 0.3.0 changed the output format to wrap code in module.exports = function($php) {...}.","severity":"breaking","affected_versions":">=0.3.0"},{"fix":"Monitor changelog and use named imports.","message":"The 'transpiler.generate' function signature may change in future versions.","severity":"deprecated","affected_versions":"<=0.4.0"},{"fix":"Provide a $php polyfill when executing the generated code.","message":"The transpiled output assumes a $php runtime object with 'stdout.print' method; not standalone JavaScript.","severity":"gotcha","affected_versions":"all"},{"fix":"Review supported features in documentation before use.","message":"Only a subset of PHP constructs are supported; advanced features like classes and namespaces may fail.","severity":"gotcha","affected_versions":"<=0.4.0"}],"env_vars":null,"last_verified":"2026-05-01T00:00:00.000Z","next_check":"2026-07-30T00:00:00.000Z","problems":[{"fix":"Wrap generated code in a context: var $php = { stdout: { print: console.log } };","cause":"Generated code expects '$php.stdout' to exist but no $php context was provided.","error":"TypeError: Cannot read properties of undefined (reading 'print')"},{"fix":"Run 'npm install php-parser' alongside php-transpiler.","cause":"Missing peer dependency php-parser when using php-transpiler.","error":"Error: php-parser is required. Please install it first."}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}