gpt4 book ai didi

php - 如何从node.js应用程序获取现有的php/python版本号

转载 作者:太空宇宙 更新时间:2023-11-03 23:34:17 32 4
gpt4 key购买 nike

我试图使用 Commander 模块创建一个命令行 Node.js 应用程序。这个应用程序会根据我的机器上安装的 php 版本下载一些文件。有人可以帮我提供如何从 Node.js 应用程序获取 php 版本号的示例

我尝试了如下方法,但没有成功,稍后我会用 php -v 替换 npm -v

var exec = require('child_process').execFile;    
exec('npm -v', function(err, data) {
console.log(err)
console.log(data.toString());
});

错误:

{ [错误:spawn npm -v ENOENT] 代码:'ENOENT', 错误号:'ENOENT', 系统调用:'spawn npm -v', 路径:'npm -v', cmd: 'npm -v' }

最佳答案

child_process.exec() 对我来说效果很好:

var exec = require('child_process').exec;
exec('php -r "echo phpversion();"', function(err, stdout, stderr) {
console.dir(stdout.toString());
});

// Outputs:
// '5.6.11-1ubuntu3.1'

对于Python,你可以这样做:

var exec = require('child_process').exec;
exec('python -c "import platform; print(platform.python_version())"',
function(err, stdout, stderr) {
console.dir(stdout.toString());
});

// Outputs:
// '2.7.10'

关于php - 如何从node.js应用程序获取现有的php/python版本号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34541575/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com