user936965
user936965

Reputation:

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.

Upvotes: 0

Views: 1118

Answers (2)

user936965
user936965

Reputation:

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.

Upvotes: 1

Philipp Palmtag
Philipp Palmtag

Reputation: 1328

I recently used system command via console myself. I found

chdir('/usr/local/bin/');

a useful function to change directory. Please put this before your system call.

If you are using an old version of PHP (below 5.4) you can try to turn safe_mode to off. See this link.

Use the full path in the command, like:

system('/usr/local/bin/npm install', $output);

Then please check if the apache user has the permissions to find (or get to) and execute the command. I think it is an permission problem.

If you are using SELiunux you can set the mode to "Permissive" by using setenforce 0. If it works you have to grant the apache user access. Do not forget to turn the enforcing level back to "Enforcing" by using setenforce 1.

Upvotes: 1

Related Questions