Using WordPress Playground in Node.js
As a WebAssembly project, you can also use WordPress Playground in Node.js.
If you need direct control over the underlying WebAssembly PHP runtime, take a
look at the @php-wasm/node package. It
provides the Node.js loader and runtime integrations used by WordPress
Playground tools. The compiled binaries are published in version-specific
packages such as @php-wasm/node-8-4.
See PHP.wasm packages to learn
how @php-wasm/universal, the Node.js and web adapters, and the version-specific
packages fit together. That page also explains the lower-level, single-version
setup with a smaller dependency footprint.
Consult the complete list of Classes, Functions, Interfaces, and Type Aliases.
WebAssembly PHP for Node.js
Together, @php-wasm/node and a version-specific package provide the compiled
PHP runtime and JavaScript API optimized for Node.js. PHP starts with an
in-memory filesystem; use the Node.js filesystem helpers to mount host paths.
The runtime can access the network if you plug in a custom WebSocket-to-TCP
proxy.
Basic usage
import { PHP } from '@php-wasm/universal';
import { loadNodeRuntime } from '@php-wasm/node';
const php = new PHP(await loadNodeRuntime('8.3'));
const output = await php.runStream({
code: '<?php phpinfo(); ?>',
});
console.log(await output.stdoutText);
Load one PHP version directly
If installation size matters and you only need the shared, low-level PHP API,
you can omit @php-wasm/node and install one Node.js build instead:
npm install @php-wasm/universal @php-wasm/node-8-4
This approach bypasses Node.js-specific setup such as networking, file locking, and extension loading. See Load one PHP version directly for the complete example and tradeoffs.
Loading PHP extensions
Use the extensions loader option to enable optional extensions before PHP
starts:
import { PHP } from '@php-wasm/universal';
import { loadNodeRuntime } from '@php-wasm/node';
const php = new PHP(
await loadNodeRuntime('8.4', {
extensions: ['intl', 'redis', 'memcached', { name: 'xdebug', options: { ideKey: 'PLAYGROUND' } }],
})
);
The same array can load external JSPI .so artifacts from a manifest:
const php = new PHP(
await loadNodeRuntime('8.4', {
extensions: [
{
source: {
format: 'manifest',
manifestUrl: './dist/wp_mysql_parser/manifest.json',
},
},
],
})
);
External extensions require JSPI. Asyncify support is limited to the bundled extensions shipped with the PHP.wasm packages.
See Loading PHP extensions for manifest format, browser usage, sidecar files, and compatibility notes.
Use cases
Run PHP inside Node.js without a native PHP install. Allow developer to produce the following solutions:
- CI/CD jobs and developer tooling.
- Support education and WordPress workflows: Power interactive tutorials, sandboxes, and coding challenges.
- Generate content and prototype server behavior.
- Render HTML using PHP templates, and quickly stand up mock API endpoints to simulate requests.