Search code examples
phpnpmexecsystemshell-exec

php system('npm install') not working


I want to install npm in a directory using PHP. I use this command to install npm:

system('npm install', $output);

Then I get the following response: 127 Which means, if I looked it up correctly, that the npm command is not found. I've also tried npm -v, this also gives the 127 response. My PHP is running on the same user as I use in my terminal. When I run npm install or npm -v in my terminal it's working fine.

Any ideas what could be the problem? NPM is installed in /usr/local/bin/ which is included in my $PATH variable.


Solution

  • I figured out the problem. Still not sure why I have to add this to my command in PHP but this worked for me:

    export PATH=$PATH:/usr/local/bin/npm; npm install
    

    Ofcourse, simple explanation would be that NPM is now in my PATH variable, but echo $PATH did already return that directory.