Symfony's Process component command works in one place, but not another
I have a bit of admin / management code which lets me, through a dashboard, click to re-install node dependencies and re-build the vue components. Then, the page automatically refreshes. It's convoluted, but it serves my needs well.
There is a command-line version of this code, which works just fine:
$nodePath = config('modules.system_node_path', 'npm');
$basePath = base_path();
$nodeCommand = "cd {$basePath}/modules/{$module} && {$nodePath} install && {$nodePath} run prod";
$this->info('building vue components...');
$nodeProcess = Process::fromShellCommandline($nodeCommand);
$nodeProcess->run();
if (!$nodeProcess->isSuccessful()) {
$this->info('Building node failed... rolling back migrations.');
$this->call('migrate:rollback');
$this->error("The module could not be installed correctly.");
} else {
$nodeProcess->getOutput();
}
here, the base_path, module and node_path are:
-
base_path= "/Users/garrettmassey/Sites/mysite" (correct / as expected) -
module= "HelpDesk'" (correct / as expected) -
node_path= "/Users/garrettmassey/Library/Application\ Support/Herd/config/nvm/versions/node/v16.20.2/bin/npm" (correct / as expected)
Now, the code that runs on the API through the GUI is here:
$nodePath = config('modules.system_node_path', 'npm');
$nodeCommand = "cd {$workingDirectory}/modules/{$moduleName} && {$nodePath} install && {$nodePath} run prod";
ModuleStatusUpdated::dispatch('Building Vue Components...');
$nodeProcess = Process::fromShellCommandline($nodeCommand);
$nodeProcess->run();
Log::info($nodeCommand);
Log::info($nodeProcess->getOutput());
if (!$nodeProcess->isSuccessful()) {
$rollback = Process::fromShellCommandLine($migrateRollbackCommand);
$rollback->run();
Log::error('Node error: ' . $nodeProcess->getErrorOutput());
return false;
}
and the values are:
-
workingDirectory= "/Users/garrettmassey/Sites/mysite" (correct / as expected) -
moduleName= "HelpDesk" (correct / as expected) -
node_path= "/Users/garrettmassey/Library/Application\ Support/Herd/config/nvm/versions/node/v16.20.2/bin/npm"
So there is no difference in the values being passed into the command, but when it is run through the API / GUI (i.e. not the command line), I get the following information / errors in the logs:
[2024-09-03 11:36:30] local.INFO: cd /Users/garrettmassey/Sites/mysite/modules/HelpDesk && /Users/garrettmassey/Library/Application\ Support/Herd/config/nvm/versions/node/v16.20.2/bin/npm install && /Users/garrettmassey/Library/Application\ Support/Herd/config/nvm/versions/node/v16.20.2/bin/npm run prod
[2024-09-03 11:36:30] local.INFO:
[2024-09-03 11:36:30] local.ERROR: Node error: env: node: No such file or directory
The second log entry should contain some kind of information from the output of the process, but it is just empty.
The command looks correct as far as I can tell. But I have no idea why the process works in one bit of the app but not the other.
Finally, the namespaces of the respective files that run these commands:
- terminal command code:
namespace App\Console\Commands; - GUI / API command code:
namespace Ecsc\Modules\Actions;
The ECSC\Modules\Actions namespace is part of a custom Laravel Nova component, and the component works as expected in all other instances, so I don't believe it's an issue with composer, or even with the namespacing, but I wonder if it might be an issue with calling the Process->run() command from a different "place" so to speak.
I don't know why the npm command won't execute in the second bit, any help is appreciated!
Please or to participate in this conversation.