【问题标题】:How to run grunt from a PHP script?如何从 PHP 脚本运行 grunt?
【发布时间】:2014-09-01 03:29:47
【问题描述】:

我正在尝试通过 php 脚本在本地项目中执行 grunt。

我已经全局安装了 npm 和 grunt cli。

如果我打开终端并输入:

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

Grunt 将成功运行并执行我在该目录中的 gruntfile.js 中设置的任务。

但是,当我尝试通过 php 脚本执行同样的事情时

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 grunt?

【问题讨论】:

  • 使用命令行时的PATH变量和加载的不一样
  • 我可以运行其他类似“/usr/local/bin/optipng”的文件,这些文件和 grunt 在同一个地方。有什么建议吗?
  • 我认为您有 PATH 问题。在这里试试这个帖子:serverfault.com/questions/151328/…

标签: php node.js gruntjs


【解决方案1】:

你需要使用绝对路径到 node 和 grunt 所以像这样执行你的命令

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-02-29
    • 2014-10-13
    • 2012-03-14
    • 2018-03-22
    • 1970-01-01
    • 1970-01-01
    • 2017-03-26
    • 2015-11-11
    相关资源
    最近更新 更多