php - 从 PHP 运行 NodeJS 命令

标签 php node.js

我有以下 PHP 代码,当用户单击本地 Intranet 页面上的按钮以运行某些 Node JS 命令时,该代码将运行。例如

exec('npm install', $output);
$output = implode(PHP_EOL, $output);
echo $output;

但是命令似乎没有运行...如果我在命令提示符中键入命令并运行它,它会运行...

我没有看到任何错误,NodeJS 被设置为路径中的系统变量,因此它应该知道 grunt 是什么……有什么想法吗?其他命令,例如whoami 运行良好。

有什么想法吗?

我在 Windows 7 上。


更新:根据下面的评论,我现在可以得到一个错误:

exec('npm install 2>&1', $output, $returnCode);
$output = implode(PHP_EOL, $output);
echo $output . ' ' . $returnCode;

这是错误:

TypeError: Cannot call method 'get' of undefined at C:\Program Files (x86)\nodejs\node_modules\npm\lib\npm.js:310:23 at C:\Program Files (x86)\nodejs\node_modules\npm\node_modules\npmconf\npmconf.js:80:7 at Array.forEach (native) at C:\Program Files (x86)\nodejs\node_modules\npm\node_modules\npmconf\npmconf.js:79:13 at f (C:\Program Files (x86)\nodejs\node_modules\npm\node_modules\once\once.js:16:25) at C:\Program Files (x86)\nodejs\node_modules\npm\node_modules\npmconf\npmconf.js:108:14 at Conf. (C:\Program Files (x86)\nodejs\node_modules\npm\node_modules\npmconf\npmconf.js:179:14) at Conf.next (C:\Program Files (x86)\nodejs\node_modules\npm\node_modules\npmconf\lib\load-prefix.js:48:20) at C:\Program Files (x86)\nodejs\node_modules\npm\node_modules\mkdirp\index.js:37:53 at Object.oncomplete (fs.js:107:15) C:\Program Files (x86)\nodejs\node_modules\npm\lib\npm.js:33 throw new Error('npm.load() required') ^ Error: npm.load() required at Object.npm.config.get (C:\Program Files (x86)\nodejs\node_modules\npm\lib\npm.js:33:11) at exit (C:\Program Files (x86)\nodejs\node_modules\npm\lib\utils\error-handler.js:49:27) at process.errorHandler (C:\Program Files (x86)\nodejs\node_modules\npm\lib\utils\error-handler.js:316:3) at process.emit (events.js:95:17) at process._fatalException (node.js:272:26) 7

所以看起来 PHP 知道 NodeJS 和 NPM 在哪里...但是不能运行它?


如果我在 PHP 中执行 whoami,我会得到:nt authority\iusr

但是如果我在命令提示符下执行此操作,则会得到:dom\cameron

我想知道是否是因为 PHP 以不同的用户身份运行它,而不是我直接在命令提示符下运行它...


最佳答案

好的,我已经设法解决了这个问题!

基本上,您需要以与运行 NodeJS 的用户相同的用户身份运行网站(PHP 文件所在的位置)!

为此,我在 IIS 中执行了以下操作:

1.) Find the website
2.) Choose basic settings
3.) Click the connect as... button
4.) Choose 'specific user'
5.) Enter `dom/cameron` and my password
6.) Restart IIS

如果有人对如何让 NodeJS 在默认 nt authority\iusr 下运行有任何想法,请随时发布答案 :)

关于php - 从 PHP 运行 NodeJS 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29844152/

相关文章:

node.js - 获取 Mongoose 模型的(关联)数组

php - WebSVN自动多Repositories

php - 如何检查一个字符串是否至少包含 5 个唯一字符且长度至少为 7 个字符?

node.js - 基本身份验证和 JWT

node.js - 无法在nodejs中使用express static接收空的get请求

node.js - 在 nest.js 代码库上找不到模块 (MODULE_NOT_FOUND)

php - 单击按钮时添加要搜索的字段

php - 优化 jQuery 速度

php - 带有 number_format 的图表

javascript - winston 记录器未序列化 mongodb.ObjectID