从PHP运行NodeJS命令

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

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

但命令似乎并没有运行…它是如果我input命令进入命令提示符并运行它…

我没有看到任何错误,并且NodeJS在Path中被设置为系统variables,所以它应该知道什么是咕噜声…任何想法? 其他命令,如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运行它作为一个不同的用户,当我直接在命令提示符下运行它…


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

基本上你需要运行网站(PHP文件所在的地方),就像NodeJS运行的地方一样。

为了做到这一点,我在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然后随意发表一个答案:)

你在WINDOWSpath中定义了npm,但PHP确认了吗?

我build议你应该在exec中写下完整的NPMpath来testing它。 如果可行 ,比执行npm install之前用正确的参数调用putenv()

所以先试试:

 exec('C:\npm-test\npm install', $output); $output = implode(PHP_EOL, $output); echo $output; 

如果可行,在调用exec()之前使用putenv()命令。 例:

 putenv("npm='C:\npm-test\npm'"); 

//然后exec(…)

你得到的错误表明一个对象没有被定义。 很有可能模块没有被加载,因为你没有从正确的位置运行脚本,并且某些包含的相关链接不起作用。 以正确的用户身份运行可能会修复它。 或者在运行节点脚本之前尝试更改到正确的目录。 或者更改节点脚本中链接的位置。