require("node:console") und globalThis.console liefern dasselbe Objekt. Das
Modul exportiert außerdem Console zum Schreiben in eigene Streams.
import console, { Console } from "node:console";
console.log("value=%d", 2);
console.table([{ a: 1, b: "x" }]);
const custom = new Console(process.stdout, process.stderr);
custom.warn("written to stderr");Die üblichen Methoden sind verfügbar: log, info, warn, error, debug,
dir, trace, table, assert, clear, Zähler, Gruppen und Timer. Die
Formatierung unterstützt %s, %d, %i, %f, %j, %o, %O und %%.
console.table unterstützt Arrays und Objekte und stellt eine ASCII-Tabelle dar.
Abstände und Ausrichtung sind implementierungsabhängig; parsen Sie den dargestellten
Text nicht.
Das Ersetzen von process.stdout.write oder process.stderr.write fängt auch die
entsprechende globale Konsolenausgabe ab.
Reference
Status: Partial · 20 of 21 suite tests pass (95.2%) · since 1.1.0
Exports
| Export | Kind | Status | Notes |
|---|---|---|---|
log() | function | Full | Prints to stdout with newline |
info() | function | Full | Alias for log() |
warn() | function | Full | Prints to stderr with newline |
error() | function | Full | Prints to stderr with newline |
debug() | function | Full | Prints to stdout with newline |
trace() | function | Full | Prints to stderr the string ‘Trace: ’ followed by a stack trace |
dir() | function | Full | Uses util.inspect() on obj and prints the resulting string to stdout |
dirxml() | function | Full | Alias for log() |
table() | function | Full | Prints a table representation of the data |
clear() | function | Full | Clears the console (when possible) |
count() | function | Full | Maintains an internal counter and outputs the number of times count() has been called with the given label |
countReset() | function | Full | Resets the internal counter for the given label |
group() | function | Full | Increases indentation of subsequent lines by spaces for grouping |
groupCollapsed() | function | Full | Alias for group() |
groupEnd() | function | Full | Decreases indentation of subsequent lines by spaces for grouping |
time() | function | Full | Starts a timer with a given label |
timeEnd() | function | Full | Stops a timer and prints elapsed time |
timeLog() | function | Full | Prints the elapsed time for a running timer without stopping it |
assert() | function | Full | Writes an error message to the console if the assertion is false |
Not implemented
context, createTask, profile, profileEnd, timeStamp.
Tests: node-api node:console: 20 pass, 1 fail, 0 error, 1 skipped. Failing: test-console-issue-43095.js.