Windows 上的 Node.Js - 如何清除控制台

问题描述 投票:0回答:21

对 node.js 环境和哲学是全新的,我想回答几个问题。我已经为 Windows 安装程序和节点包管理器下载了 node.js。Windows Cmd 提示符当前用于运行 nodejs 应用程序。

  1. cls 清除命令窗口或命令提示符中的错误。 node.js 是否有等效项? console.clear 不存在;(或者以其他形式存在?

  2. 我通过下面这段代码创建了一个服务器

    var http = require("http");
    http.createServer(function (request, response) {
        response.writeHead(200, {
            "Content-Type": "text/html"
        });
        response.write("Hello World");
        console.log("welcome world")response.end();
    }).listen(9000, "127.0.0.1");
    

我将代码更改为下面并刷新浏览器,发现内容类型没有变化,我如何才能看到变化?

var http = require("http");
http.createServer(function(request, response) {
  response.writeHead(200, {"Content-Type": "text/plain"});
  response.write("Hello World");
  console.log("welcome world")
  response.end();
}).listen(9000,"127.0.0.1");
windows node.js console windows-vista
21个回答
114
投票
console.log('\033[2J');

这适用于 linux。不确定窗户。

你可以使用这样的东西“欺骗”用户:

var lines = process.stdout.getWindowSize()[1];
for(var i = 0; i < lines; i++) {
    console.log('\r\n');
}

78
投票
process.stdout.write('\033c');

这也适用于 Windows。 Win7 至少。


56
投票

这将清除 Windows 上的控制台并将光标置于 0,0:

var util = require('util');
util.print("\u001b[2J\u001b[0;0H");

process.stdout.write("\u001b[2J\u001b[0;0H");

56
投票

这主要针对Linux;但据报道也可以在 Windows 中工作。

Gnome Terminal中有Ctrl + L清空终端。它可以与 Python、Node JS 或任何其他在终端中运行的 REPL 一起使用。它与

clear
命令具有相同的效果。


46
投票

我正在使用 Windows CMD,这对我有用

console.clear();

43
投票

在 Windows 上以严格模式清除控制台:

'use strict';
process.stdout.write('\x1Bc'); 

19
投票

从 Node.JS v8.3.0 开始,您可以使用方法 clear:

console.clear()

16
投票

只需在 Windows 上使用

CTRL + L
即可清除控制台。


8
投票

没有在 Windows 上测试过,但可以在 unix 上运行。诀窍在于

child_process
模块。检查文档。您可以将此代码保存为文件,并在每次需要时将其加载到 REPL。

var exec = require('child_process').exec;

function clear(){
    exec('clear', function(error, stdout, stderr){
        console.log(stdout);
    });    
}

6
投票

用严格模式解决问题:

'use strict';
process.stdout.write('\x1B[2J');

5
投票

就用官方的方式:

console.log('Blah blah blah'); // Prints "Blah blah blah"

console.clear(); // Clears, or in other words, resets the terminal.

console.log('You will only see this message. No more Blah blah blah...');


3
投票

如果您使用

VSCode
,您可以使用
CTRL + K
。我知道这不是一个通用的解决方案,但可能会对某些人有所帮助。


1
投票

根据 sanatgersappa 的回答和我发现的其他一些信息,这是我想出的:

function clear() {
    var stdout = "";

    if (process.platform.indexOf("win") != 0) {
        stdout += "\033[2J";
    } else {
        var lines = process.stdout.getWindowSize()[1];

        for (var i=0; i<lines; i++) {
            stdout += "\r\n";
        }
    }

    // Reset cursur
    stdout += "\033[0f";

    process.stdout.write(stdout);
}

为了让事情更简单,我将其发布为一个名为 cli-clear 的 npm 包。


1
投票

您可以使用

readline
模块:

readline.cursorTo(process.stdout, 0, 0)
将光标移动到 (0, 0)。

readline.clearLine(process.stdout, 0)
清除当前行。

readline.clearScreenDown(process.stdout)
清除光标下方的所有内容。

const READLINE = require('readline');

function clear() {
    READLINE.cursorTo(process.stdout, 0, 0);
    READLINE.clearLine(process.stdout, 0);
    READLINE.clearScreenDown(process.stdout);
}

1
投票

在 package.json 上试试这个:

  "nodemonConfig": {
    "events": {
      "start": "clear"
    }
  }

0
投票

我无法使上述任何一项工作。我正在使用 nodemon 进行开发,发现这是清除控制台的最简单方法:

  console.log("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");

它只是将控制台滚动几行,这样您就可以清楚地看到后续的 console.log 命令。

希望对某人有所帮助。


0
投票

此代码在我的 node.js 服务器控制台 Windows 7 上运行良好。

process.stdout.write("\u001b[0J\u001b[1J\u001b[2J\u001b[0;0H\u001b[0;0W");


0
投票

在 mac 上,我只是简单地使用 Cmd + K 来清除控制台,非常方便,并且比在您的项目中添加代码来完成它更好。


-1
投票

迟来的,但如果您使用的是 powershell,则 ctrl+l 可以在 Windows 中使用 :) Powershell + chocolatey + node + npm = winning.


-1
投票

Ctrl + L 这是最好、最简单、最有效的选择。


-1
投票

在我的例子中,我这样做是为了永远循环并在控制台中显示一个数字:

class Status {

  private numberOfMessagesInTheQueue: number;
  private queueName: string;

  public constructor() {
    this.queueName = "Test Queue";
    this.numberOfMessagesInTheQueue = 0;
    this.main();
  }

  private async main(): Promise<any> {    
    while(true) {
      this.numberOfMessagesInTheQueue++;
      await new Promise((resolve) => {
        setTimeout(_ => resolve(this.showResults(this.numberOfMessagesInTheQueue)), 1500);
      });
    }
  }

  private showResults(numberOfMessagesInTheQuee: number): void {
    console.clear();
    console.log(`Number of messages in the queue ${this.queueName}: ${numberOfMessagesInTheQuee}.`)
  }
}

export default new Status();

运行此代码时,您将看到相同的消息“队列测试队列中的消息数:1”。和数字变化(1..2..3 等)。

© www.soinside.com 2019 - 2024. All rights reserved.