Tag: exec

NodeJS脚本 – npm在exec()上不显示当前安装的版本

我有一个简单的脚本运行在/some/project/folder 剧本是这样的: exec("npm -v", (err, stdout, stderr) => { $log.info('version installed', stdout); }); 现在,当我运行这个节点脚本输出3.10.9但是当我进入/some/project/folder (由terminal),我手动运行npm -v ,它输出3.3.6 ** 这可能是怎么回事? ** 我一直在使用nvm,但是我不明白这是否有问题。 有什么build议 ? 谢谢

nodejs'exec'命令不能正确执行'sed'命令

我有一个文件,我想要扫描它,find字符“{”并创build一个新行,然后添加一个IP在新行,并添加一个半科隆到行的末尾,然后写入configuration文件。 从shell运行时,我可以使用以下sed命令完成此操作: sed -i 's/{/&\n1.1.1.1;/g' /tmp/test.conf 在test.conf里面: acl testACL{ }; shell中命令的输出显示如下: acl testACL{ 1.1.1.1; }; 作品完美! 现在问题是当我得到nodejs执行它: var sys = require('sys'); var exec = require('child_process').exec; function puts(error, stdout, stderr) { sys.puts(stdout) }; exec("sed -i 's/{/&\n1.1.1.1;/g' /tmp/test.conf",puts); return 0; 当我在控制台中运行命令:'nodejs test.js' 我得到空白输出,当我检查文件,文件'test.conf'没有被改变! 为什么?! 另外,如果你认为“它的权限问题! 我已经有nodejs exec命令写testingconfiguration文件的基本回声,它工作正常。 我也尝试了'shelljs'模块没有运气那里。 我已经尝试了几个小时的无数组合现在没有盛行! 我感到困惑。

nodejs exec命令失败,没有有用的错误消息

这是要执行的代码 cp.exec("cc -Wall /tmp/test.c -o /tmp/test", function(e, stdout, stderr) { if (e) { var errorstr = "Compilation failed with the following error "+ e.message.toString() client.send(errorstr) console.log(e, stdout, stderr) ee.prototype.removeAllListeners() } else if (stderr.length > 0) { client.send("Compilion finished with warnings\n"+ stderr + '\n') client.send('compiled') ee.prototype.emit('compiled') } else { client.send("Compilation successful") ee.prototype.emit('compiled') } }) 'client'是socket.io的callback参数的参数。 'ee'是EventEmitter的一个实例 […]

child_process spawn()中的通配符?

我想在node.js中执行一个类似“doSomething ./myfiles/*.csv”的命令。 我想使用产卵而不是执行,因为它是某种观察过程,我需要标准输出。 我试过这个 var spawn = require('child_process').spawn; spawn("doSomething", ["./myfiles/*.csv"]); 但是,通配符* .csv不会被解释。 使用spawn()时不可能使用通配符吗? 还有其他的可能性来解决这个问题吗? 谢谢 托本

Node.js child_process exec的标准输出被缩短

在Node.js中,我使用了child_process模块​​的exec命令来调用Java中的一个algorithm,该algorithm返回大量的文本,然后parsing和使用。 我能够捕捉它,但是当它超过一定数量的行,内容被截断。 exec("sh target/bin/solver "+fields.dimx+" "+fields.dimy, function(error, stdout, stderr){ //do stuff with stdout } 我已经尝试过使用setTimeouts和callback,但没有成功,但我确实感到这是因为我引用标准输出之前,它可以完全检索我的代码。 我已经testing过,stdout实际上是数据丢失首先发生的地方。 这不是一个asynchronous的问题。 我也在我的本地机器和Heroku上testing过,并且发生完全相同的问题,每次都在同一行号码处截断。 任何想法或build议可能有助于这个?

在nodejs(windows)中显示正在运行的subprocess的输出

比如说,我使用下面的代码(名为server.js)运行节点(我正在使用windows)中最基本的webServer: var http = require('http'); http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World\n'); }).listen(1337, '127.0.0.1'); console.log('Server running at http://127.0.0.1:1337/'); 我想把它作为一个subprocess运行,并成功地完成了以下任务: var exec = require('child_process').exec; var serv = exec('node server.js', function(error, stdout, stderr){ console.log('outputs & errors:' + error, stdout, stderr); }); 但是,一旦我运行第二个文件,我什么也没有收到。 没有“输出和错误”,没有“服务器运行在…”。 如果我在浏览器中inputlocalhost:1337,我会得到“hello world”,所以我知道它正在运行。 有没有办法pipe这个“服务器运行…”,所以我可以“确定”它正在监听?

Node.js产生的颜色?

我正在使用摩卡来testing我的应用程序。 目前,我正在使用Makefiles,但是我想切换到Cakefiles。 当我通过Cake运行我的testing时,来自Mocha的颜色不会显示在控制台上。 这是一个例子: task 'test', 'test project', (options) -> fetchTestFiles './test', (err,files) -> mocha = spawn 'mocha', files mocha.stdout.pipe(process.stdout, end: false) 现在,我试着这样做: task 'test', 'test project', (options) -> fetchTestFiles './test', (err,files) -> mocha = spawn 'mocha', files, customFds: [0..2] 这工作,但是Node.js文档声称customFds已被弃用。 有没有办法通过颜色而不使用customFds ?

nodeJS exec对“cd”shell cmd不起作用

var sys = require('sys'), exec = require('child_process').exec; exec("cd /home/ubuntu/distro", function(err, stdout, stderr) { console.log("cd: " + err + " : " + stdout); exec("pwd", function(err, stdout, stderr) { console.log("pwd: " + err + " : " + stdout); exec("git status", function(err, stdout, stderr) { console.log("git status returned " ); console.log(err); }) }) }) cd: null […]

使用nodejs的spawn会导致“未知选项 – ”和“”错误

我尝试着spawn一个rm -rf node_modules然后是npm install (在windows 7上; n x命令由一个透明安装的CygWin提供,所有n个 x命令都可以在命令行上parsing)。 我最初使用exec ,但想要捕捉stdout / stderr信息,所以我想我会使用spawn ,并重写代码使用它。 然而,这打破了一切。 rm命令改写成这样: var spawn = require("child_process").spawn, child = spawn("rm", ["-rf", "node_modules"]); child.stdout.on('data', function (data) { console.log(data.toString()); }); child.stderr.on('data', function (data) { console.log(data.toString()); }); child.on('error', function() { console.log(arguments); }); 但是,运行这将产生以下错误: rm: unknown option — , Try `rm –help' for more information. npm命令改写成这样: […]