Wednesday, January 31, 2024

[SOLVED] How to run Terser from PHP script?

Issue

I have PHP script that automates my dev-to-production proces. I want to use Terser to minify the JS in my project. But when I fire it with the php exec() function, I get a result value of 1.

This seems to indecate an undefined error. I have no further information what went wrong, but I did run the following tests:

  • The same Terser command from a normal shell runs fine.
  • When running exec('ls') I get the contents of the directory the php script is in.
  • The Terser command is in /usr/local/bin, when I put this path in front of the command the result is the same.
  • Everyone has read permission on the command and all other Terser files.
  • Everyone has read & write permission on the directory the JS files are in and on the target directory.

stderr contains:

node: /opt/lampp/lib/libstdc++.so.6: version `GLIBCXX_3.4.29' not found (required by /lib/x86_64-linux-gnu/libnode.so.72)
node: /opt/lampp/lib/libstdc++.so.6: version `GLIBCXX_3.4.26' not found (required by /lib/x86_64-linux-gnu/libnode.so.72)
node: /opt/lampp/lib/libstdc++.so.6: version `GLIBCXX_3.4.20' not found (required by /lib/x86_64-linux-gnu/libnode.so.72)
node: /opt/lampp/lib/libstdc++.so.6: version `GLIBCXX_3.4.21' not found (required by /lib/x86_64-linux-gnu/libnode.so.72)
node: /opt/lampp/lib/libstdc++.so.6: version `CXXABI_1.3.9' not found (required by /lib/x86_64-linux-gnu/libnode.so.72)
node: /opt/lampp/lib/libstdc++.so.6: version `CXXABI_1.3.8' not found (required by /lib/x86_64-linux-gnu/libicui18n.so.70)
node: /opt/lampp/lib/libstdc++.so.6: version `CXXABI_1.3.8' not found (required by /lib/x86_64-linux-gnu/libicuuc.so.70)

I am working on Ubuntu 23.10

Does anyone know how to troubleshoot this and how to run Terser from a php script?


Solution

Got it: it turned out that apache comes with its own set of usefull libraries and that PHP tries to use those before it looks for the systems libraries when running a command.

Those libs are older versions of the libs we all have on our updated systems.

By clearing LD_LIBRARY_PATH, PHP no longer operates on the misconception that it should put too much trust in whatever comes with Apache.

putenv('LD_LIBRARY_PATH=.');


Answered By - FvEldijk
Answer Checked By - Clifford M. (WPSolving Volunteer)