是否有一种方法可以在没有尾随换行符的情况下打印到控制台?该console对象的文档并没有说关于任何东西:
console.log()用换行符打印到stdout.此函数可以采用类似
printf()方式的多个参数.例:Run Code Online (Sandbox Code Playgroud)console.log('count: %d', count);如果在第一个字符串中找不到格式化元素,则
util.inspect在每个参数上使用.
我试图spawn实现一个rm -rf node_modules跟随npm install(在Windows 7; n x命令由透明安装的CygWin提供.所有n x命令在命令行上解析就好了).
我最初使用它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); });
Run Code Online (Sandbox Code Playgroud)
但是,运行它会生成以下错误:
rm: unknown option -- ,
Try `rm --help' for more information.
Run Code Online (Sandbox Code Playgroud)
npm重写的命令变为:
var spawn = require("child_process").spawn,
child = spawn("npm", ["install"]);
child.stdout.on('data', function (data) { console.log(data.toString()); });
child.stderr.on('data', function (data) { …Run Code Online (Sandbox Code Playgroud) 我有一个Node脚本以PluginManager.exe这种方式调用外部程序():
const util = require('util');
const execFile = util.promisify(require('child_process').execFile);
const process = execFile('PluginManager.exe', ['/install']);
process
.then(({stdout, stderr}) => console.log('done', stdout, stderr))
.catch(e => console.log(e));
Run Code Online (Sandbox Code Playgroud)
PluginManager.exe需要8秒才能执行.我的问题是,在子进程退出后,Node脚本会再运行10秒钟.我知道什么时候PluginManager.exe完成,因为我可以看到它从Windows任务管理器进程列表中消失.
是什么让Node进程运行了这么长时间,我该怎么做以确保它在子进程退出后立即退出?