0

I try to execute a command from PHP using Symfony's Process but I can't get it to work because PHP refuses to find the executable.

Example with npm:

use Symfony\Component\Process\ExecutableFinder;

var_dump((new ExecutableFinder)->find('npm')); // returns null

$process = new Process(['npm', '--version']); // also tried /usr/bin/npm here
$process->run();
var_dump($process->getOutput()); // returns empty string

PHP should look up in the following paths:

echo getenv('PATH'); // /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

I created a symlink in /usr/bin as it is in PHP's PATH:

$ cd /usr/bin && ll
...
lrwxrwxrwx 1 root  root  40 Aug 18 14:03  npm -> /root/.nvm/versions/node/v14.6.0/bin/npm*
...

The npm executable is in PATH:

$ whereis npm
npm: /usr/bin/npm /root/.nvm/versions/node/v14.6.0/bin/npm ...

Still, PHP refuses to find the executable. It also says:

var_dump(is_executable('/usr/bin/npm')); // returns false

exec('/usr/bin/npm --version', $output, $errno); // $errno is 126

I'm using Ubuntu 18.04 on WSL, tried restarting apache, modifying permissions chown www-data:www-data npm of the symlink, creating symlinks in every other path /usr/local/bin etc., ... nothing works. I'm pretty much lost right now.

Thank you for your help!

annow
  • 15
  • 1
  • 5
  • Use [`exec()`](https://stackoverflow.com/questions/12199353/how-can-i-debug-exec-problems) with `/usr/bin/npm --version` to see if the $errno is EPERM. – mario Aug 18 '20 at 12:40
  • @mario `var_dump(shell_exec('/usr/bin/npm --version'));` returns `null` and `exec('/usr/bin/npm --version', $output, $return);` results in an empty output and `int(126)` which is _Command invoked cannot execute_. – annow Aug 18 '20 at 12:51
  • `namei -m /root/.nvm/versions/node/v14.6.0/bin/npm` to probe access. – mario Aug 18 '20 at 13:07
  • @mario `namei` returns `.nvm - No such file or directory`. How can this be fixed? – annow Aug 18 '20 at 13:10
  • Other accounts rarely have /root access (nor should they). Also: https://docs.npmjs.com/downloading-and-installing-packages-globally – mario Aug 18 '20 at 13:42
  • Did you try `chmod +x /usr/bin/npm` ? – Nek Aug 18 '20 at 16:51
  • @Nek yes. It seems like there's no way of getting this to work when npm is under `/root`. This also seems to be WSL-specific. – annow Aug 18 '20 at 16:58

0 Answers0