Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

quelo83's avatar
Level 31

Compile EcmaScript6 to Javascript from PHP

Hi, I would like to use a web form in my site to post an EcmaScript6 code and then, from server side (php), compile it as Babel would do.

In other words, I'd like to do what Laravel Mix does with "mix.js('resources/js/app.js', 'public/js')" but in PHP.

What is the best way to do that? Is it possible?

Thanks in advance!

0 likes
5 replies
topvillas's avatar

Might be possible but do it with Node and and there'll be a lot less pain.

quelo83's avatar
Level 31

Thanks for your answers.

But how can I do it with Node from PHP?

  1. The user writes the code in EcmaScript 6 in a textarea
  2. The user clicks a submit button
  3. The form posts the code to the server
  4. The server receives the request with the code
  5. The server compiles the code to plain javascript
  6. The server creates .js file with the compiled code and saves it locally

I don't know how to do the fifth step.

topvillas's avatar

Forget about PHP. You're not going to be able to transpile ES6 to ES5 with it. Unless you write your own package.

Create an express app, upload the data, save it to a file then process the file with babel.

If you really want to involve PHP then create a microservice with express and call it from PHP.

1 like
quelo83's avatar
Level 31

Sorry guys, but I need your help.

I'm using The Symfony Process Component to run:

$process = new Process('npm run myscript');
$process->run();

// executes after the command finishes
if (!$process->isSuccessful()) {
    throw new ProcessFailedException($process);
}

The problem is that I get the following error: env: node: No such file or directory

If I run the command from the console everything works fine but not from PHP .

Do you have any ideas on how to make it to work?

Please or to participate in this conversation.