我正试图从php脚本在本地项目中执行grunt。
我已经在全球范围内安装了npm和grunt cli。
如果我打开终端并输入:
cd /path/to/local/grunt/project/ && grunt
Grunt将成功运行并执行我在该目录中的gruntfile.js中设置的任务。
然而,当我尝试通过php脚本执行shell时
var_dump(shell_exec('cd /path/to/local/grunt/project/ && grunt 2>&1'));
我得到:
sh: grunt: command not found
我还尝试了直接访问全局CLI:
var_dump(shell_exec('/usr/local/lib/node_modules/grunt-cli/bin/grunt 2>&1'));
但后来我得到了:
env: node: No such file or directory
然而,这是按预期从终端运行的。
这是怎么回事?当我尝试通过php运行grunt命令时,为什么找不到它?如何从php脚本中shell exec或exec-grumt?
您需要使用到节点和grunt的绝对路径,因此执行您的命令时需要使用以下
var_dump(shell_exec('cd /path/to/local/grunt/project/ && /usr/local/bin/node /usr/local/bin/grunt 2>&1'));