How to run grunt from PHP script?

I am trying to execute grunt on a local project with a php script.

I have npm and the clunt cli is installed globally.

If I open a terminal and type:

cd /path/to/local/grunt/project/ && grunt

      

Grunt will successfully complete and complete the tasks that I have set in the gruntfile.js found in that directory.

However, when I try to shell out, do the same thing via PHP script

var_dump(shell_exec('cd /path/to/local/grunt/project/ && grunt 2>&1'));

      

I get:

sh: grunt: command not found

      

I also tried the direct path to the global CLI:

var_dump(shell_exec('/usr/local/lib/node_modules/grunt-cli/bin/grunt 2>&1'));

      

but then i get:

env: node: No such file or directory

      

However, this works as expected from the terminal.

What's going on here? Why is the grunt command not found when I try to run it through php? How can I execute an exec or exec grunt wrapper from a php script?

+3


source to share


1 answer


You need to use absolute paths to node and grumble, so run your command like this



var_dump(shell_exec('cd /path/to/local/grunt/project/ && /usr/local/bin/node /usr/local/bin/grunt 2>&1'));

      

+4


source







All Articles