Skip to content

Examples

This guide provides practical examples of using Clack in different scenarios.

import {
const text: (opts: TextOptions) => Promise<string | symbol>
text
,
function isCancel(value: unknown): value is symbol
isCancel
} from '@clack/prompts';
const
const name: string | symbol
name
= await
function text(opts: TextOptions): Promise<string | symbol>
text
({
TextOptions.message: string
message
: 'What is your name?',
TextOptions.placeholder?: string
placeholder
: 'John Doe',
});
// isCancel is a TypeScript type guard that checks if the result is a symbol
// If true, TypeScript knows name is a symbol in this block
if (
function isCancel(value: unknown): value is symbol
isCancel
(
const name: string | symbol
name
)) {
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
('Operation cancelled');
var process: NodeJS.Process
process
.
NodeJS.Process.exit(code?: number | string | null | undefined): never

The process.exit() method instructs Node.js to terminate the process synchronously with an exit status of code. If code is omitted, exit uses either the 'success' code 0 or the value of process.exitCode if it has been set. Node.js will not terminate until all the 'exit' event listeners are called.

To exit with a 'failure' code:

import { exit } from 'node:process';
exit(1);

The shell that executed Node.js should see the exit code as 1.

Calling process.exit() will force the process to exit as quickly as possible even if there are still asynchronous operations pending that have not yet completed fully, including I/O operations to process.stdout and process.stderr.

In most situations, it is not actually necessary to call process.exit() explicitly. The Node.js process will exit on its own if there is no additional work pending in the event loop. The process.exitCode property can be set to tell the process which exit code to use when the process exits gracefully.

For instance, the following example illustrates a misuse of the process.exit() method that could lead to data printed to stdout being truncated and lost:

import { exit } from 'node:process';
// This is an example of what *not* to do:
if (someConditionNotMet()) {
printUsageToStdout();
exit(1);
}

The reason this is problematic is because writes to process.stdout in Node.js are sometimes asynchronous and may occur over multiple ticks of the Node.js event loop. Calling process.exit(), however, forces the process to exit before those additional writes to stdout can be performed.

Rather than calling process.exit() directly, the code should set the process.exitCode and allow the process to exit naturally by avoiding scheduling any additional work for the event loop:

import process from 'node:process';
// How to properly set the exit code while letting
// the process exit gracefully.
if (someConditionNotMet()) {
printUsageToStdout();
process.exitCode = 1;
}

If it is necessary to terminate the Node.js process due to an error condition, throwing an uncaught error and allowing the process to terminate accordingly is safer than calling process.exit().

In Worker threads, this function stops the current thread rather than the current process.

@sincev0.1.13

@paramcode The exit code. For string type, only integer strings (e.g.,'1') are allowed.

exit
(0);
}
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
(`Hello, ${
const name: string
name
}!`);
import {
const select: <Value>(opts: SelectOptions<Value>) => Promise<Value | symbol>
select
,
function isCancel(value: unknown): value is symbol
isCancel
} from '@clack/prompts';
const
const framework: symbol | "react" | "vue" | "svelte"
framework
= await
select<"react" | "vue" | "svelte">(opts: SelectOptions<"react" | "vue" | "svelte">): Promise<symbol | "react" | "vue" | "svelte">
select
({
SelectOptions<Value>.message: string
message
: 'Choose a framework:',
SelectOptions<"react" | "vue" | "svelte">.options: ({
value: "react";
label?: string;
hint?: string;
} | {
value: "vue";
label?: string;
hint?: string;
} | {
value: "svelte";
label?: string;
hint?: string;
})[]
options
: [
{
value: "react"

Internal data for this option.

value
: 'react',
label?: string

The optional, user-facing text for this option.

By default, the value is converted to a string.

label
: 'React',
hint?: string

An optional hint to display to the user when this option might be selected.

By default, no hint is displayed.

hint
: 'A JavaScript library for building user interfaces' },
{
value: "vue"

Internal data for this option.

value
: 'vue',
label?: string

The optional, user-facing text for this option.

By default, the value is converted to a string.

label
: 'Vue',
hint?: string

An optional hint to display to the user when this option might be selected.

By default, no hint is displayed.

hint
: 'The Progressive JavaScript Framework' },
{
value: "svelte"

Internal data for this option.

value
: 'svelte',
label?: string

The optional, user-facing text for this option.

By default, the value is converted to a string.

label
: 'Svelte',
hint?: string

An optional hint to display to the user when this option might be selected.

By default, no hint is displayed.

hint
: 'Cybernetically enhanced web apps' },
],
});
if (
function isCancel(value: unknown): value is symbol
isCancel
(
const framework: symbol | "react" | "vue" | "svelte"
framework
)) {
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
('Operation cancelled');
var process: NodeJS.Process
process
.
NodeJS.Process.exit(code?: number | string | null | undefined): never

The process.exit() method instructs Node.js to terminate the process synchronously with an exit status of code. If code is omitted, exit uses either the 'success' code 0 or the value of process.exitCode if it has been set. Node.js will not terminate until all the 'exit' event listeners are called.

To exit with a 'failure' code:

import { exit } from 'node:process';
exit(1);

The shell that executed Node.js should see the exit code as 1.

Calling process.exit() will force the process to exit as quickly as possible even if there are still asynchronous operations pending that have not yet completed fully, including I/O operations to process.stdout and process.stderr.

In most situations, it is not actually necessary to call process.exit() explicitly. The Node.js process will exit on its own if there is no additional work pending in the event loop. The process.exitCode property can be set to tell the process which exit code to use when the process exits gracefully.

For instance, the following example illustrates a misuse of the process.exit() method that could lead to data printed to stdout being truncated and lost:

import { exit } from 'node:process';
// This is an example of what *not* to do:
if (someConditionNotMet()) {
printUsageToStdout();
exit(1);
}

The reason this is problematic is because writes to process.stdout in Node.js are sometimes asynchronous and may occur over multiple ticks of the Node.js event loop. Calling process.exit(), however, forces the process to exit before those additional writes to stdout can be performed.

Rather than calling process.exit() directly, the code should set the process.exitCode and allow the process to exit naturally by avoiding scheduling any additional work for the event loop:

import process from 'node:process';
// How to properly set the exit code while letting
// the process exit gracefully.
if (someConditionNotMet()) {
printUsageToStdout();
process.exitCode = 1;
}

If it is necessary to terminate the Node.js process due to an error condition, throwing an uncaught error and allowing the process to terminate accordingly is safer than calling process.exit().

In Worker threads, this function stops the current thread rather than the current process.

@sincev0.1.13

@paramcode The exit code. For string type, only integer strings (e.g.,'1') are allowed.

exit
(0);
}
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
(`You selected ${
const framework: "react" | "vue" | "svelte"
framework
}`);
import {
const autocomplete: <Value>(opts: AutocompleteOptions<Value>) => Promise<Value | symbol>
autocomplete
,
function isCancel(value: unknown): value is symbol
isCancel
} from '@clack/prompts';
const
const selectedPackage: symbol | "react" | "vue" | "svelte" | "angular" | "next"
selectedPackage
= await
autocomplete<"react" | "vue" | "svelte" | "angular" | "next">(opts: AutocompleteOptions<"react" | "vue" | "svelte" | "angular" | "next">): Promise<symbol | "react" | "vue" | "svelte" | "angular" | "next">
autocomplete
({
AutocompleteOptions<Value>.message: string

The message to display to the user.

message
: 'Search for a package:',
AutocompleteOptions<"react" | "vue" | "svelte" | "angular" | "next">.options: ({
value: "react";
label?: string;
hint?: string;
} | {
value: "vue";
label?: string;
hint?: string;
} | {
value: "svelte";
label?: string;
hint?: string;
} | {
value: "angular";
label?: string;
hint?: string;
} | {
...;
})[]

Available options for the autocomplete prompt.

options
: [
{
value: "react"

Internal data for this option.

value
: 'react',
label?: string

The optional, user-facing text for this option.

By default, the value is converted to a string.

label
: 'React',
hint?: string

An optional hint to display to the user when this option might be selected.

By default, no hint is displayed.

hint
: 'A JavaScript library for building user interfaces' },
{
value: "vue"

Internal data for this option.

value
: 'vue',
label?: string

The optional, user-facing text for this option.

By default, the value is converted to a string.

label
: 'Vue',
hint?: string

An optional hint to display to the user when this option might be selected.

By default, no hint is displayed.

hint
: 'The Progressive JavaScript Framework' },
{
value: "svelte"

Internal data for this option.

value
: 'svelte',
label?: string

The optional, user-facing text for this option.

By default, the value is converted to a string.

label
: 'Svelte',
hint?: string

An optional hint to display to the user when this option might be selected.

By default, no hint is displayed.

hint
: 'Cybernetically enhanced web apps' },
{
value: "angular"

Internal data for this option.

value
: 'angular',
label?: string

The optional, user-facing text for this option.

By default, the value is converted to a string.

label
: 'Angular',
hint?: string

An optional hint to display to the user when this option might be selected.

By default, no hint is displayed.

hint
: 'Platform for building mobile & desktop web apps' },
{
value: "next"

Internal data for this option.

value
: 'next',
label?: string

The optional, user-facing text for this option.

By default, the value is converted to a string.

label
: 'Next.js',
hint?: string

An optional hint to display to the user when this option might be selected.

By default, no hint is displayed.

hint
: 'The React Framework for Production' },
],
AutocompleteOptions<Value>.placeholder?: string

Placeholder text to display when no input is provided.

placeholder
: 'Type to search...',
AutocompleteOptions<Value>.maxItems?: number

Maximum number of items to display at once.

maxItems
: 5,
});
if (
function isCancel(value: unknown): value is symbol
isCancel
(
const selectedPackage: symbol | "react" | "vue" | "svelte" | "angular" | "next"
selectedPackage
)) {
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
('Operation cancelled');
var process: NodeJS.Process
process
.
NodeJS.Process.exit(code?: number | string | null | undefined): never

The process.exit() method instructs Node.js to terminate the process synchronously with an exit status of code. If code is omitted, exit uses either the 'success' code 0 or the value of process.exitCode if it has been set. Node.js will not terminate until all the 'exit' event listeners are called.

To exit with a 'failure' code:

import { exit } from 'node:process';
exit(1);

The shell that executed Node.js should see the exit code as 1.

Calling process.exit() will force the process to exit as quickly as possible even if there are still asynchronous operations pending that have not yet completed fully, including I/O operations to process.stdout and process.stderr.

In most situations, it is not actually necessary to call process.exit() explicitly. The Node.js process will exit on its own if there is no additional work pending in the event loop. The process.exitCode property can be set to tell the process which exit code to use when the process exits gracefully.

For instance, the following example illustrates a misuse of the process.exit() method that could lead to data printed to stdout being truncated and lost:

import { exit } from 'node:process';
// This is an example of what *not* to do:
if (someConditionNotMet()) {
printUsageToStdout();
exit(1);
}

The reason this is problematic is because writes to process.stdout in Node.js are sometimes asynchronous and may occur over multiple ticks of the Node.js event loop. Calling process.exit(), however, forces the process to exit before those additional writes to stdout can be performed.

Rather than calling process.exit() directly, the code should set the process.exitCode and allow the process to exit naturally by avoiding scheduling any additional work for the event loop:

import process from 'node:process';
// How to properly set the exit code while letting
// the process exit gracefully.
if (someConditionNotMet()) {
printUsageToStdout();
process.exitCode = 1;
}

If it is necessary to terminate the Node.js process due to an error condition, throwing an uncaught error and allowing the process to terminate accordingly is safer than calling process.exit().

In Worker threads, this function stops the current thread rather than the current process.

@sincev0.1.13

@paramcode The exit code. For string type, only integer strings (e.g.,'1') are allowed.

exit
(0);
}
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
(`Selected package: ${
const selectedPackage: "react" | "vue" | "svelte" | "angular" | "next"
selectedPackage
}`);
import {
const groupMultiselect: <Value>(opts: GroupMultiSelectOptions<Value>) => Promise<Value[] | symbol>
groupMultiselect
,
function isCancel(value: unknown): value is symbol
isCancel
} from '@clack/prompts';
const
const tools: symbol | ("typescript" | "eslint" | "prettier" | "node" | "express" | "prisma" | "jest" | "cypress" | "vitest")[]
tools
= await
groupMultiselect<"typescript" | "eslint" | "prettier" | "node" | "express" | "prisma" | "jest" | "cypress" | "vitest">(opts: GroupMultiSelectOptions<"typescript" | "eslint" | "prettier" | "node" | "express" | "prisma" | "jest" | "cypress" | "vitest">): Promise<...>
groupMultiselect
({
GroupMultiSelectOptions<Value>.message: string
message
: 'Select development tools:',
GroupMultiSelectOptions<"typescript" | "eslint" | "prettier" | "node" | "express" | "prisma" | "jest" | "cypress" | "vitest">.options: Record<string, ({
value: "typescript";
label?: string;
hint?: string;
} | {
value: "eslint";
label?: string;
hint?: string;
} | {
value: "prettier";
label?: string;
hint?: string;
} | {
value: "node";
label?: string;
hint?: string;
} | ... 4 more ... | {
...;
})[]>
options
: {
'Frontend': [
{
value: "typescript"

Internal data for this option.

value
: 'typescript',
label?: string

The optional, user-facing text for this option.

By default, the value is converted to a string.

label
: 'TypeScript',
hint?: string

An optional hint to display to the user when this option might be selected.

By default, no hint is displayed.

hint
: 'JavaScript with syntax for types' },
{
value: "eslint"

Internal data for this option.

value
: 'eslint',
label?: string

The optional, user-facing text for this option.

By default, the value is converted to a string.

label
: 'ESLint',
hint?: string

An optional hint to display to the user when this option might be selected.

By default, no hint is displayed.

hint
: 'Find and fix problems in your JavaScript code' },
{
value: "prettier"

Internal data for this option.

value
: 'prettier',
label?: string

The optional, user-facing text for this option.

By default, the value is converted to a string.

label
: 'Prettier',
hint?: string

An optional hint to display to the user when this option might be selected.

By default, no hint is displayed.

hint
: 'Code formatter' },
],
'Backend': [
{
value: "node"

Internal data for this option.

value
: 'node',
label?: string

The optional, user-facing text for this option.

By default, the value is converted to a string.

label
: 'Node.js',
hint?: string

An optional hint to display to the user when this option might be selected.

By default, no hint is displayed.

hint
: 'JavaScript runtime' },
{
value: "express"

Internal data for this option.

value
: 'express',
label?: string

The optional, user-facing text for this option.

By default, the value is converted to a string.

label
: 'Express',
hint?: string

An optional hint to display to the user when this option might be selected.

By default, no hint is displayed.

hint
: 'Web framework for Node.js' },
{
value: "prisma"

Internal data for this option.

value
: 'prisma',
label?: string

The optional, user-facing text for this option.

By default, the value is converted to a string.

label
: 'Prisma',
hint?: string

An optional hint to display to the user when this option might be selected.

By default, no hint is displayed.

hint
: 'Next-generation ORM' },
],
'Testing': [
{
value: "jest"

Internal data for this option.

value
: 'jest',
label?: string

The optional, user-facing text for this option.

By default, the value is converted to a string.

label
: 'Jest',
hint?: string

An optional hint to display to the user when this option might be selected.

By default, no hint is displayed.

hint
: 'JavaScript testing framework' },
{
value: "cypress"

Internal data for this option.

value
: 'cypress',
label?: string

The optional, user-facing text for this option.

By default, the value is converted to a string.

label
: 'Cypress',
hint?: string

An optional hint to display to the user when this option might be selected.

By default, no hint is displayed.

hint
: 'End-to-end testing framework' },
{
value: "vitest"

Internal data for this option.

value
: 'vitest',
label?: string

The optional, user-facing text for this option.

By default, the value is converted to a string.

label
: 'Vitest',
hint?: string

An optional hint to display to the user when this option might be selected.

By default, no hint is displayed.

hint
: 'Vite-native testing framework' },
],
},
});
if (
function isCancel(value: unknown): value is symbol
isCancel
(
const tools: symbol | ("typescript" | "eslint" | "prettier" | "node" | "express" | "prisma" | "jest" | "cypress" | "vitest")[]
tools
)) {
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
('Operation cancelled');
var process: NodeJS.Process
process
.
NodeJS.Process.exit(code?: number | string | null | undefined): never

The process.exit() method instructs Node.js to terminate the process synchronously with an exit status of code. If code is omitted, exit uses either the 'success' code 0 or the value of process.exitCode if it has been set. Node.js will not terminate until all the 'exit' event listeners are called.

To exit with a 'failure' code:

import { exit } from 'node:process';
exit(1);

The shell that executed Node.js should see the exit code as 1.

Calling process.exit() will force the process to exit as quickly as possible even if there are still asynchronous operations pending that have not yet completed fully, including I/O operations to process.stdout and process.stderr.

In most situations, it is not actually necessary to call process.exit() explicitly. The Node.js process will exit on its own if there is no additional work pending in the event loop. The process.exitCode property can be set to tell the process which exit code to use when the process exits gracefully.

For instance, the following example illustrates a misuse of the process.exit() method that could lead to data printed to stdout being truncated and lost:

import { exit } from 'node:process';
// This is an example of what *not* to do:
if (someConditionNotMet()) {
printUsageToStdout();
exit(1);
}

The reason this is problematic is because writes to process.stdout in Node.js are sometimes asynchronous and may occur over multiple ticks of the Node.js event loop. Calling process.exit(), however, forces the process to exit before those additional writes to stdout can be performed.

Rather than calling process.exit() directly, the code should set the process.exitCode and allow the process to exit naturally by avoiding scheduling any additional work for the event loop:

import process from 'node:process';
// How to properly set the exit code while letting
// the process exit gracefully.
if (someConditionNotMet()) {
printUsageToStdout();
process.exitCode = 1;
}

If it is necessary to terminate the Node.js process due to an error condition, throwing an uncaught error and allowing the process to terminate accordingly is safer than calling process.exit().

In Worker threads, this function stops the current thread rather than the current process.

@sincev0.1.13

@paramcode The exit code. For string type, only integer strings (e.g.,'1') are allowed.

exit
(0);
}
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
('Selected tools:',
const tools: ("typescript" | "eslint" | "prettier" | "node" | "express" | "prisma" | "jest" | "cypress" | "vitest")[]
tools
);
import {
const confirm: (opts: ConfirmOptions) => Promise<boolean | symbol>
confirm
,
function isCancel(value: unknown): value is symbol
isCancel
} from '@clack/prompts';
const
const shouldProceed: boolean | symbol
shouldProceed
= await
function confirm(opts: ConfirmOptions): Promise<boolean | symbol>
confirm
({
ConfirmOptions.message: string
message
: 'Do you want to continue?',
});
if (
function isCancel(value: unknown): value is symbol
isCancel
(
const shouldProceed: boolean | symbol
shouldProceed
)) {
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
('Operation cancelled');
var process: NodeJS.Process
process
.
NodeJS.Process.exit(code?: number | string | null | undefined): never

The process.exit() method instructs Node.js to terminate the process synchronously with an exit status of code. If code is omitted, exit uses either the 'success' code 0 or the value of process.exitCode if it has been set. Node.js will not terminate until all the 'exit' event listeners are called.

To exit with a 'failure' code:

import { exit } from 'node:process';
exit(1);

The shell that executed Node.js should see the exit code as 1.

Calling process.exit() will force the process to exit as quickly as possible even if there are still asynchronous operations pending that have not yet completed fully, including I/O operations to process.stdout and process.stderr.

In most situations, it is not actually necessary to call process.exit() explicitly. The Node.js process will exit on its own if there is no additional work pending in the event loop. The process.exitCode property can be set to tell the process which exit code to use when the process exits gracefully.

For instance, the following example illustrates a misuse of the process.exit() method that could lead to data printed to stdout being truncated and lost:

import { exit } from 'node:process';
// This is an example of what *not* to do:
if (someConditionNotMet()) {
printUsageToStdout();
exit(1);
}

The reason this is problematic is because writes to process.stdout in Node.js are sometimes asynchronous and may occur over multiple ticks of the Node.js event loop. Calling process.exit(), however, forces the process to exit before those additional writes to stdout can be performed.

Rather than calling process.exit() directly, the code should set the process.exitCode and allow the process to exit naturally by avoiding scheduling any additional work for the event loop:

import process from 'node:process';
// How to properly set the exit code while letting
// the process exit gracefully.
if (someConditionNotMet()) {
printUsageToStdout();
process.exitCode = 1;
}

If it is necessary to terminate the Node.js process due to an error condition, throwing an uncaught error and allowing the process to terminate accordingly is safer than calling process.exit().

In Worker threads, this function stops the current thread rather than the current process.

@sincev0.1.13

@paramcode The exit code. For string type, only integer strings (e.g.,'1') are allowed.

exit
(0);
}
if (
const shouldProceed: boolean
shouldProceed
) {
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
('Proceeding...');
} else {
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
('Operation cancelled');
}
import {
const text: (opts: TextOptions) => Promise<string | symbol>
text
,
const select: <Value>(opts: SelectOptions<Value>) => Promise<Value | symbol>
select
,
const confirm: (opts: ConfirmOptions) => Promise<boolean | symbol>
confirm
,
const groupMultiselect: <Value>(opts: GroupMultiSelectOptions<Value>) => Promise<Value[] | symbol>
groupMultiselect
,
function isCancel(value: unknown): value is symbol
isCancel
} from '@clack/prompts';
async function
function setupProject(): Promise<void>
setupProject
() {
// Get project details
const
const name: string | symbol
name
= await
function text(opts: TextOptions): Promise<string | symbol>
text
({
TextOptions.message: string
message
: 'Project name:',
TextOptions.validate?: (value: string) => string | Error | undefined
validate
: (
value: string
value
) => {
if (
value: string
value
.
String.length: number

Returns the length of a String object.

length
=== 0) return 'Name is required';
if (!/^[a-z0-9-]+$/.
RegExp.test(string: string): boolean

Returns a Boolean value that indicates whether or not a pattern exists in a searched string.

@paramstring String on which to perform the search.

test
(
value: string
value
)) return 'Name can only contain lowercase letters, numbers, and hyphens';
return
var undefined
undefined
;
},
});
if (
function isCancel(value: unknown): value is symbol
isCancel
(
const name: string | symbol
name
)) {
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
('Operation cancelled');
var process: NodeJS.Process
process
.
NodeJS.Process.exit(code?: number | string | null | undefined): never

The process.exit() method instructs Node.js to terminate the process synchronously with an exit status of code. If code is omitted, exit uses either the 'success' code 0 or the value of process.exitCode if it has been set. Node.js will not terminate until all the 'exit' event listeners are called.

To exit with a 'failure' code:

import { exit } from 'node:process';
exit(1);

The shell that executed Node.js should see the exit code as 1.

Calling process.exit() will force the process to exit as quickly as possible even if there are still asynchronous operations pending that have not yet completed fully, including I/O operations to process.stdout and process.stderr.

In most situations, it is not actually necessary to call process.exit() explicitly. The Node.js process will exit on its own if there is no additional work pending in the event loop. The process.exitCode property can be set to tell the process which exit code to use when the process exits gracefully.

For instance, the following example illustrates a misuse of the process.exit() method that could lead to data printed to stdout being truncated and lost:

import { exit } from 'node:process';
// This is an example of what *not* to do:
if (someConditionNotMet()) {
printUsageToStdout();
exit(1);
}

The reason this is problematic is because writes to process.stdout in Node.js are sometimes asynchronous and may occur over multiple ticks of the Node.js event loop. Calling process.exit(), however, forces the process to exit before those additional writes to stdout can be performed.

Rather than calling process.exit() directly, the code should set the process.exitCode and allow the process to exit naturally by avoiding scheduling any additional work for the event loop:

import process from 'node:process';
// How to properly set the exit code while letting
// the process exit gracefully.
if (someConditionNotMet()) {
printUsageToStdout();
process.exitCode = 1;
}

If it is necessary to terminate the Node.js process due to an error condition, throwing an uncaught error and allowing the process to terminate accordingly is safer than calling process.exit().

In Worker threads, this function stops the current thread rather than the current process.

@sincev0.1.13

@paramcode The exit code. For string type, only integer strings (e.g.,'1') are allowed.

exit
(0);
}
const
const type: symbol | "web" | "cli" | "api"
type
= await
select<"web" | "cli" | "api">(opts: SelectOptions<"web" | "cli" | "api">): Promise<symbol | "web" | "cli" | "api">
select
({
SelectOptions<Value>.message: string
message
: 'Project type:',
SelectOptions<"web" | "cli" | "api">.options: ({
value: "web";
label?: string;
hint?: string;
} | {
value: "cli";
label?: string;
hint?: string;
} | {
value: "api";
label?: string;
hint?: string;
})[]
options
: [
{
value: "web"

Internal data for this option.

value
: 'web',
label?: string

The optional, user-facing text for this option.

By default, the value is converted to a string.

label
: 'Web Application',
hint?: string

An optional hint to display to the user when this option might be selected.

By default, no hint is displayed.

hint
: 'Full-stack web application' },
{
value: "cli"

Internal data for this option.

value
: 'cli',
label?: string

The optional, user-facing text for this option.

By default, the value is converted to a string.

label
: 'CLI Tool',
hint?: string

An optional hint to display to the user when this option might be selected.

By default, no hint is displayed.

hint
: 'Command-line interface tool' },
{
value: "api"

Internal data for this option.

value
: 'api',
label?: string

The optional, user-facing text for this option.

By default, the value is converted to a string.

label
: 'API Server',
hint?: string

An optional hint to display to the user when this option might be selected.

By default, no hint is displayed.

hint
: 'REST/GraphQL API server' },
],
});
if (
function isCancel(value: unknown): value is symbol
isCancel
(
const type: symbol | "web" | "cli" | "api"
type
)) {
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
('Operation cancelled');
var process: NodeJS.Process
process
.
NodeJS.Process.exit(code?: number | string | null | undefined): never

The process.exit() method instructs Node.js to terminate the process synchronously with an exit status of code. If code is omitted, exit uses either the 'success' code 0 or the value of process.exitCode if it has been set. Node.js will not terminate until all the 'exit' event listeners are called.

To exit with a 'failure' code:

import { exit } from 'node:process';
exit(1);

The shell that executed Node.js should see the exit code as 1.

Calling process.exit() will force the process to exit as quickly as possible even if there are still asynchronous operations pending that have not yet completed fully, including I/O operations to process.stdout and process.stderr.

In most situations, it is not actually necessary to call process.exit() explicitly. The Node.js process will exit on its own if there is no additional work pending in the event loop. The process.exitCode property can be set to tell the process which exit code to use when the process exits gracefully.

For instance, the following example illustrates a misuse of the process.exit() method that could lead to data printed to stdout being truncated and lost:

import { exit } from 'node:process';
// This is an example of what *not* to do:
if (someConditionNotMet()) {
printUsageToStdout();
exit(1);
}

The reason this is problematic is because writes to process.stdout in Node.js are sometimes asynchronous and may occur over multiple ticks of the Node.js event loop. Calling process.exit(), however, forces the process to exit before those additional writes to stdout can be performed.

Rather than calling process.exit() directly, the code should set the process.exitCode and allow the process to exit naturally by avoiding scheduling any additional work for the event loop:

import process from 'node:process';
// How to properly set the exit code while letting
// the process exit gracefully.
if (someConditionNotMet()) {
printUsageToStdout();
process.exitCode = 1;
}

If it is necessary to terminate the Node.js process due to an error condition, throwing an uncaught error and allowing the process to terminate accordingly is safer than calling process.exit().

In Worker threads, this function stops the current thread rather than the current process.

@sincev0.1.13

@paramcode The exit code. For string type, only integer strings (e.g.,'1') are allowed.

exit
(0);
}
// Get additional details based on project type
let
let framework: any
framework
;
if (
const type: "web" | "cli" | "api"
type
=== 'web') {
let framework: any
framework
= await
select<"next" | "nuxt" | "sveltekit">(opts: SelectOptions<"next" | "nuxt" | "sveltekit">): Promise<symbol | "next" | "nuxt" | "sveltekit">
select
({
SelectOptions<Value>.message: string
message
: 'Choose a framework:',
SelectOptions<"next" | "nuxt" | "sveltekit">.options: ({
value: "next";
label?: string;
hint?: string;
} | {
value: "nuxt";
label?: string;
hint?: string;
} | {
value: "sveltekit";
label?: string;
hint?: string;
})[]
options
: [
{
value: "next"

Internal data for this option.

value
: 'next',
label?: string

The optional, user-facing text for this option.

By default, the value is converted to a string.

label
: 'Next.js',
hint?: string

An optional hint to display to the user when this option might be selected.

By default, no hint is displayed.

hint
: 'React framework for production' },
{
value: "nuxt"

Internal data for this option.

value
: 'nuxt',
label?: string

The optional, user-facing text for this option.

By default, the value is converted to a string.

label
: 'Nuxt',
hint?: string

An optional hint to display to the user when this option might be selected.

By default, no hint is displayed.

hint
: 'Vue framework for production' },
{
value: "sveltekit"

Internal data for this option.

value
: 'sveltekit',
label?: string

The optional, user-facing text for this option.

By default, the value is converted to a string.

label
: 'SvelteKit',
hint?: string

An optional hint to display to the user when this option might be selected.

By default, no hint is displayed.

hint
: 'Svelte framework for production' },
],
});
if (
function isCancel(value: unknown): value is symbol
isCancel
(
let framework: symbol | "next" | "nuxt" | "sveltekit"
framework
)) {
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
('Operation cancelled');
var process: NodeJS.Process
process
.
NodeJS.Process.exit(code?: number | string | null | undefined): never

The process.exit() method instructs Node.js to terminate the process synchronously with an exit status of code. If code is omitted, exit uses either the 'success' code 0 or the value of process.exitCode if it has been set. Node.js will not terminate until all the 'exit' event listeners are called.

To exit with a 'failure' code:

import { exit } from 'node:process';
exit(1);

The shell that executed Node.js should see the exit code as 1.

Calling process.exit() will force the process to exit as quickly as possible even if there are still asynchronous operations pending that have not yet completed fully, including I/O operations to process.stdout and process.stderr.

In most situations, it is not actually necessary to call process.exit() explicitly. The Node.js process will exit on its own if there is no additional work pending in the event loop. The process.exitCode property can be set to tell the process which exit code to use when the process exits gracefully.

For instance, the following example illustrates a misuse of the process.exit() method that could lead to data printed to stdout being truncated and lost:

import { exit } from 'node:process';
// This is an example of what *not* to do:
if (someConditionNotMet()) {
printUsageToStdout();
exit(1);
}

The reason this is problematic is because writes to process.stdout in Node.js are sometimes asynchronous and may occur over multiple ticks of the Node.js event loop. Calling process.exit(), however, forces the process to exit before those additional writes to stdout can be performed.

Rather than calling process.exit() directly, the code should set the process.exitCode and allow the process to exit naturally by avoiding scheduling any additional work for the event loop:

import process from 'node:process';
// How to properly set the exit code while letting
// the process exit gracefully.
if (someConditionNotMet()) {
printUsageToStdout();
process.exitCode = 1;
}

If it is necessary to terminate the Node.js process due to an error condition, throwing an uncaught error and allowing the process to terminate accordingly is safer than calling process.exit().

In Worker threads, this function stops the current thread rather than the current process.

@sincev0.1.13

@paramcode The exit code. For string type, only integer strings (e.g.,'1') are allowed.

exit
(0);
}
}
// Select features
const
const features: symbol | ("typescript" | "eslint" | "prettier" | "jest" | "cypress" | "docker" | "ci")[]
features
= await
groupMultiselect<"typescript" | "eslint" | "prettier" | "jest" | "cypress" | "docker" | "ci">(opts: GroupMultiSelectOptions<"typescript" | "eslint" | "prettier" | "jest" | "cypress" | "docker" | "ci">): Promise<...>
groupMultiselect
({
GroupMultiSelectOptions<Value>.message: string
message
: 'Select features:',
GroupMultiSelectOptions<"typescript" | "eslint" | "prettier" | "jest" | "cypress" | "docker" | "ci">.options: Record<string, ({
value: "typescript";
label?: string;
hint?: string;
} | {
value: "eslint";
label?: string;
hint?: string;
} | {
value: "prettier";
label?: string;
hint?: string;
} | {
value: "jest";
label?: string;
hint?: string;
} | {
...;
} | {
...;
} | {
...;
})[]>
options
: {
'Development': [
{
value: "typescript"

Internal data for this option.

value
: 'typescript',
label?: string

The optional, user-facing text for this option.

By default, the value is converted to a string.

label
: 'TypeScript',
hint?: string

An optional hint to display to the user when this option might be selected.

By default, no hint is displayed.

hint
: 'Type safety' },
{
value: "eslint"

Internal data for this option.

value
: 'eslint',
label?: string

The optional, user-facing text for this option.

By default, the value is converted to a string.

label
: 'ESLint',
hint?: string

An optional hint to display to the user when this option might be selected.

By default, no hint is displayed.

hint
: 'Code linting' },
{
value: "prettier"

Internal data for this option.

value
: 'prettier',
label?: string

The optional, user-facing text for this option.

By default, the value is converted to a string.

label
: 'Prettier',
hint?: string

An optional hint to display to the user when this option might be selected.

By default, no hint is displayed.

hint
: 'Code formatting' },
],
'Testing': [
{
value: "jest"

Internal data for this option.

value
: 'jest',
label?: string

The optional, user-facing text for this option.

By default, the value is converted to a string.

label
: 'Jest',
hint?: string

An optional hint to display to the user when this option might be selected.

By default, no hint is displayed.

hint
: 'Unit testing' },
{
value: "cypress"

Internal data for this option.

value
: 'cypress',
label?: string

The optional, user-facing text for this option.

By default, the value is converted to a string.

label
: 'Cypress',
hint?: string

An optional hint to display to the user when this option might be selected.

By default, no hint is displayed.

hint
: 'E2E testing' },
],
'Deployment': [
{
value: "docker"

Internal data for this option.

value
: 'docker',
label?: string

The optional, user-facing text for this option.

By default, the value is converted to a string.

label
: 'Docker',
hint?: string

An optional hint to display to the user when this option might be selected.

By default, no hint is displayed.

hint
: 'Containerization' },
{
value: "ci"

Internal data for this option.

value
: 'ci',
label?: string

The optional, user-facing text for this option.

By default, the value is converted to a string.

label
: 'CI/CD',
hint?: string

An optional hint to display to the user when this option might be selected.

By default, no hint is displayed.

hint
: 'Continuous integration' },
],
},
});
if (
function isCancel(value: unknown): value is symbol
isCancel
(
const features: symbol | ("typescript" | "eslint" | "prettier" | "jest" | "cypress" | "docker" | "ci")[]
features
)) {
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
('Operation cancelled');
var process: NodeJS.Process
process
.
NodeJS.Process.exit(code?: number | string | null | undefined): never

The process.exit() method instructs Node.js to terminate the process synchronously with an exit status of code. If code is omitted, exit uses either the 'success' code 0 or the value of process.exitCode if it has been set. Node.js will not terminate until all the 'exit' event listeners are called.

To exit with a 'failure' code:

import { exit } from 'node:process';
exit(1);

The shell that executed Node.js should see the exit code as 1.

Calling process.exit() will force the process to exit as quickly as possible even if there are still asynchronous operations pending that have not yet completed fully, including I/O operations to process.stdout and process.stderr.

In most situations, it is not actually necessary to call process.exit() explicitly. The Node.js process will exit on its own if there is no additional work pending in the event loop. The process.exitCode property can be set to tell the process which exit code to use when the process exits gracefully.

For instance, the following example illustrates a misuse of the process.exit() method that could lead to data printed to stdout being truncated and lost:

import { exit } from 'node:process';
// This is an example of what *not* to do:
if (someConditionNotMet()) {
printUsageToStdout();
exit(1);
}

The reason this is problematic is because writes to process.stdout in Node.js are sometimes asynchronous and may occur over multiple ticks of the Node.js event loop. Calling process.exit(), however, forces the process to exit before those additional writes to stdout can be performed.

Rather than calling process.exit() directly, the code should set the process.exitCode and allow the process to exit naturally by avoiding scheduling any additional work for the event loop:

import process from 'node:process';
// How to properly set the exit code while letting
// the process exit gracefully.
if (someConditionNotMet()) {
printUsageToStdout();
process.exitCode = 1;
}

If it is necessary to terminate the Node.js process due to an error condition, throwing an uncaught error and allowing the process to terminate accordingly is safer than calling process.exit().

In Worker threads, this function stops the current thread rather than the current process.

@sincev0.1.13

@paramcode The exit code. For string type, only integer strings (e.g.,'1') are allowed.

exit
(0);
}
// Confirm setup
const
const shouldProceed: boolean | symbol
shouldProceed
= await
function confirm(opts: ConfirmOptions): Promise<boolean | symbol>
confirm
({
ConfirmOptions.message: string
message
: `Create ${
const type: "web" | "cli" | "api"
type
} project "${
const name: string
name
}"${
let framework: "next" | "nuxt" | "sveltekit" | undefined
framework
? ` with ${
let framework: "next" | "nuxt" | "sveltekit"
framework
}` : ''}?`,
});
if (
function isCancel(value: unknown): value is symbol
isCancel
(
const shouldProceed: boolean | symbol
shouldProceed
)) {
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
('Operation cancelled');
var process: NodeJS.Process
process
.
NodeJS.Process.exit(code?: number | string | null | undefined): never

The process.exit() method instructs Node.js to terminate the process synchronously with an exit status of code. If code is omitted, exit uses either the 'success' code 0 or the value of process.exitCode if it has been set. Node.js will not terminate until all the 'exit' event listeners are called.

To exit with a 'failure' code:

import { exit } from 'node:process';
exit(1);

The shell that executed Node.js should see the exit code as 1.

Calling process.exit() will force the process to exit as quickly as possible even if there are still asynchronous operations pending that have not yet completed fully, including I/O operations to process.stdout and process.stderr.

In most situations, it is not actually necessary to call process.exit() explicitly. The Node.js process will exit on its own if there is no additional work pending in the event loop. The process.exitCode property can be set to tell the process which exit code to use when the process exits gracefully.

For instance, the following example illustrates a misuse of the process.exit() method that could lead to data printed to stdout being truncated and lost:

import { exit } from 'node:process';
// This is an example of what *not* to do:
if (someConditionNotMet()) {
printUsageToStdout();
exit(1);
}

The reason this is problematic is because writes to process.stdout in Node.js are sometimes asynchronous and may occur over multiple ticks of the Node.js event loop. Calling process.exit(), however, forces the process to exit before those additional writes to stdout can be performed.

Rather than calling process.exit() directly, the code should set the process.exitCode and allow the process to exit naturally by avoiding scheduling any additional work for the event loop:

import process from 'node:process';
// How to properly set the exit code while letting
// the process exit gracefully.
if (someConditionNotMet()) {
printUsageToStdout();
process.exitCode = 1;
}

If it is necessary to terminate the Node.js process due to an error condition, throwing an uncaught error and allowing the process to terminate accordingly is safer than calling process.exit().

In Worker threads, this function stops the current thread rather than the current process.

@sincev0.1.13

@paramcode The exit code. For string type, only integer strings (e.g.,'1') are allowed.

exit
(0);
}
if (
const shouldProceed: boolean
shouldProceed
) {
// Project creation logic here
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
('Creating project...');
}
}
import {
const text: (opts: TextOptions) => Promise<string | symbol>
text
,
const select: <Value>(opts: SelectOptions<Value>) => Promise<Value | symbol>
select
,
const groupMultiselect: <Value>(opts: GroupMultiSelectOptions<Value>) => Promise<Value[] | symbol>
groupMultiselect
,
function isCancel(value: unknown): value is symbol
isCancel
} from '@clack/prompts';
interface
interface Config
Config
{
Config.port: number
port
: number;
Config.host: string
host
: string;
Config.mode: "development" | "production"
mode
: 'development' | 'production';
Config.features: string[]
features
: string[];
Config.database: {
type: "postgres" | "mysql" | "mongodb";
url: string;
}
database
: {
type: "postgres" | "mysql" | "mongodb"
type
: 'postgres' | 'mysql' | 'mongodb';
url: string
url
: string;
};
}
async function
function setupConfig(): Promise<Config | null>
setupConfig
():
interface Promise<T>

Represents the completion of an asynchronous operation

Promise
<
interface Config
Config
| null> {
const
const port: string | symbol
port
= await
function text(opts: TextOptions): Promise<string | symbol>
text
({
TextOptions.message: string
message
: 'Enter port number:',
TextOptions.placeholder?: string
placeholder
: '3000',
TextOptions.validate?: (value: string) => string | Error | undefined
validate
: (
value: string
value
) => {
const
const num: number
num
=
function parseInt(string: string, radix?: number): number

Converts a string to an integer.

@paramstring A string to convert into a number.

@paramradix A value between 2 and 36 that specifies the base of the number in string. If this argument is not supplied, strings with a prefix of '0x' are considered hexadecimal. All other strings are considered decimal.

parseInt
(
value: string
value
);
if (
function isNaN(number: number): boolean

Returns a Boolean value that indicates whether a value is the reserved value NaN (not a number).

@paramnumber A numeric value.

isNaN
(
const num: number
num
)) return 'Please enter a valid number';
if (
const num: number
num
< 1 ||
const num: number
num
> 65535) return 'Port must be between 1 and 65535';
return
var undefined
undefined
;
},
});
if (
function isCancel(value: unknown): value is symbol
isCancel
(
const port: string | symbol
port
)) {
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
('Operation cancelled');
var process: NodeJS.Process
process
.
NodeJS.Process.exit(code?: number | string | null | undefined): never

The process.exit() method instructs Node.js to terminate the process synchronously with an exit status of code. If code is omitted, exit uses either the 'success' code 0 or the value of process.exitCode if it has been set. Node.js will not terminate until all the 'exit' event listeners are called.

To exit with a 'failure' code:

import { exit } from 'node:process';
exit(1);

The shell that executed Node.js should see the exit code as 1.

Calling process.exit() will force the process to exit as quickly as possible even if there are still asynchronous operations pending that have not yet completed fully, including I/O operations to process.stdout and process.stderr.

In most situations, it is not actually necessary to call process.exit() explicitly. The Node.js process will exit on its own if there is no additional work pending in the event loop. The process.exitCode property can be set to tell the process which exit code to use when the process exits gracefully.

For instance, the following example illustrates a misuse of the process.exit() method that could lead to data printed to stdout being truncated and lost:

import { exit } from 'node:process';
// This is an example of what *not* to do:
if (someConditionNotMet()) {
printUsageToStdout();
exit(1);
}

The reason this is problematic is because writes to process.stdout in Node.js are sometimes asynchronous and may occur over multiple ticks of the Node.js event loop. Calling process.exit(), however, forces the process to exit before those additional writes to stdout can be performed.

Rather than calling process.exit() directly, the code should set the process.exitCode and allow the process to exit naturally by avoiding scheduling any additional work for the event loop:

import process from 'node:process';
// How to properly set the exit code while letting
// the process exit gracefully.
if (someConditionNotMet()) {
printUsageToStdout();
process.exitCode = 1;
}

If it is necessary to terminate the Node.js process due to an error condition, throwing an uncaught error and allowing the process to terminate accordingly is safer than calling process.exit().

In Worker threads, this function stops the current thread rather than the current process.

@sincev0.1.13

@paramcode The exit code. For string type, only integer strings (e.g.,'1') are allowed.

exit
(0);
return null;
}
const
const host: string | symbol
host
= await
function text(opts: TextOptions): Promise<string | symbol>
text
({
TextOptions.message: string
message
: 'Enter host:',
TextOptions.placeholder?: string
placeholder
: 'localhost',
TextOptions.validate?: (value: string) => string | Error | undefined
validate
: (
value: string
value
) => {
if (!
value: string
value
) return 'Host is required';
if (!/^[a-zA-Z0-9.-]+$/.
RegExp.test(string: string): boolean

Returns a Boolean value that indicates whether or not a pattern exists in a searched string.

@paramstring String on which to perform the search.

test
(
value: string
value
)) return 'Invalid host format';
return
var undefined
undefined
;
},
});
if (
function isCancel(value: unknown): value is symbol
isCancel
(
const host: string | symbol
host
)) {
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
('Operation cancelled');
var process: NodeJS.Process
process
.
NodeJS.Process.exit(code?: number | string | null | undefined): never

The process.exit() method instructs Node.js to terminate the process synchronously with an exit status of code. If code is omitted, exit uses either the 'success' code 0 or the value of process.exitCode if it has been set. Node.js will not terminate until all the 'exit' event listeners are called.

To exit with a 'failure' code:

import { exit } from 'node:process';
exit(1);

The shell that executed Node.js should see the exit code as 1.

Calling process.exit() will force the process to exit as quickly as possible even if there are still asynchronous operations pending that have not yet completed fully, including I/O operations to process.stdout and process.stderr.

In most situations, it is not actually necessary to call process.exit() explicitly. The Node.js process will exit on its own if there is no additional work pending in the event loop. The process.exitCode property can be set to tell the process which exit code to use when the process exits gracefully.

For instance, the following example illustrates a misuse of the process.exit() method that could lead to data printed to stdout being truncated and lost:

import { exit } from 'node:process';
// This is an example of what *not* to do:
if (someConditionNotMet()) {
printUsageToStdout();
exit(1);
}

The reason this is problematic is because writes to process.stdout in Node.js are sometimes asynchronous and may occur over multiple ticks of the Node.js event loop. Calling process.exit(), however, forces the process to exit before those additional writes to stdout can be performed.

Rather than calling process.exit() directly, the code should set the process.exitCode and allow the process to exit naturally by avoiding scheduling any additional work for the event loop:

import process from 'node:process';
// How to properly set the exit code while letting
// the process exit gracefully.
if (someConditionNotMet()) {
printUsageToStdout();
process.exitCode = 1;
}

If it is necessary to terminate the Node.js process due to an error condition, throwing an uncaught error and allowing the process to terminate accordingly is safer than calling process.exit().

In Worker threads, this function stops the current thread rather than the current process.

@sincev0.1.13

@paramcode The exit code. For string type, only integer strings (e.g.,'1') are allowed.

exit
(0);
return null;
}
const
const mode: symbol | "development" | "production"
mode
= await
select<"development" | "production">(opts: SelectOptions<"development" | "production">): Promise<symbol | "development" | "production">
select
({
SelectOptions<Value>.message: string
message
: 'Select mode:',
SelectOptions<"development" | "production">.options: ({
value: "development";
label?: string;
hint?: string;
} | {
value: "production";
label?: string;
hint?: string;
})[]
options
: [
{
value: "development"

Internal data for this option.

value
: 'development',
label?: string

The optional, user-facing text for this option.

By default, the value is converted to a string.

label
: 'Development',
hint?: string

An optional hint to display to the user when this option might be selected.

By default, no hint is displayed.

hint
: 'For local development' },
{
value: "production"

Internal data for this option.

value
: 'production',
label?: string

The optional, user-facing text for this option.

By default, the value is converted to a string.

label
: 'Production',
hint?: string

An optional hint to display to the user when this option might be selected.

By default, no hint is displayed.

hint
: 'For production deployment' },
],
});
if (
function isCancel(value: unknown): value is symbol
isCancel
(
const mode: symbol | "development" | "production"
mode
)) {
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
('Operation cancelled');
var process: NodeJS.Process
process
.
NodeJS.Process.exit(code?: number | string | null | undefined): never

The process.exit() method instructs Node.js to terminate the process synchronously with an exit status of code. If code is omitted, exit uses either the 'success' code 0 or the value of process.exitCode if it has been set. Node.js will not terminate until all the 'exit' event listeners are called.

To exit with a 'failure' code:

import { exit } from 'node:process';
exit(1);

The shell that executed Node.js should see the exit code as 1.

Calling process.exit() will force the process to exit as quickly as possible even if there are still asynchronous operations pending that have not yet completed fully, including I/O operations to process.stdout and process.stderr.

In most situations, it is not actually necessary to call process.exit() explicitly. The Node.js process will exit on its own if there is no additional work pending in the event loop. The process.exitCode property can be set to tell the process which exit code to use when the process exits gracefully.

For instance, the following example illustrates a misuse of the process.exit() method that could lead to data printed to stdout being truncated and lost:

import { exit } from 'node:process';
// This is an example of what *not* to do:
if (someConditionNotMet()) {
printUsageToStdout();
exit(1);
}

The reason this is problematic is because writes to process.stdout in Node.js are sometimes asynchronous and may occur over multiple ticks of the Node.js event loop. Calling process.exit(), however, forces the process to exit before those additional writes to stdout can be performed.

Rather than calling process.exit() directly, the code should set the process.exitCode and allow the process to exit naturally by avoiding scheduling any additional work for the event loop:

import process from 'node:process';
// How to properly set the exit code while letting
// the process exit gracefully.
if (someConditionNotMet()) {
printUsageToStdout();
process.exitCode = 1;
}

If it is necessary to terminate the Node.js process due to an error condition, throwing an uncaught error and allowing the process to terminate accordingly is safer than calling process.exit().

In Worker threads, this function stops the current thread rather than the current process.

@sincev0.1.13

@paramcode The exit code. For string type, only integer strings (e.g.,'1') are allowed.

exit
(0);
return null;
}
const
const database: symbol | "postgres" | "mysql" | "mongodb"
database
= await
select<"postgres" | "mysql" | "mongodb">(opts: SelectOptions<"postgres" | "mysql" | "mongodb">): Promise<symbol | "postgres" | "mysql" | "mongodb">
select
({
SelectOptions<Value>.message: string
message
: 'Select database:',
SelectOptions<"postgres" | "mysql" | "mongodb">.options: ({
value: "postgres";
label?: string;
hint?: string;
} | {
value: "mysql";
label?: string;
hint?: string;
} | {
value: "mongodb";
label?: string;
hint?: string;
})[]
options
: [
{
value: "postgres"

Internal data for this option.

value
: 'postgres',
label?: string

The optional, user-facing text for this option.

By default, the value is converted to a string.

label
: 'PostgreSQL',
hint?: string

An optional hint to display to the user when this option might be selected.

By default, no hint is displayed.

hint
: 'Advanced open source database' },
{
value: "mysql"

Internal data for this option.

value
: 'mysql',
label?: string

The optional, user-facing text for this option.

By default, the value is converted to a string.

label
: 'MySQL',
hint?: string

An optional hint to display to the user when this option might be selected.

By default, no hint is displayed.

hint
: 'Most popular open source database' },
{
value: "mongodb"

Internal data for this option.

value
: 'mongodb',
label?: string

The optional, user-facing text for this option.

By default, the value is converted to a string.

label
: 'MongoDB',
hint?: string

An optional hint to display to the user when this option might be selected.

By default, no hint is displayed.

hint
: 'Document-oriented database' },
],
});
if (
function isCancel(value: unknown): value is symbol
isCancel
(
const database: symbol | "postgres" | "mysql" | "mongodb"
database
)) {
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
('Operation cancelled');
var process: NodeJS.Process
process
.
NodeJS.Process.exit(code?: number | string | null | undefined): never

The process.exit() method instructs Node.js to terminate the process synchronously with an exit status of code. If code is omitted, exit uses either the 'success' code 0 or the value of process.exitCode if it has been set. Node.js will not terminate until all the 'exit' event listeners are called.

To exit with a 'failure' code:

import { exit } from 'node:process';
exit(1);

The shell that executed Node.js should see the exit code as 1.

Calling process.exit() will force the process to exit as quickly as possible even if there are still asynchronous operations pending that have not yet completed fully, including I/O operations to process.stdout and process.stderr.

In most situations, it is not actually necessary to call process.exit() explicitly. The Node.js process will exit on its own if there is no additional work pending in the event loop. The process.exitCode property can be set to tell the process which exit code to use when the process exits gracefully.

For instance, the following example illustrates a misuse of the process.exit() method that could lead to data printed to stdout being truncated and lost:

import { exit } from 'node:process';
// This is an example of what *not* to do:
if (someConditionNotMet()) {
printUsageToStdout();
exit(1);
}

The reason this is problematic is because writes to process.stdout in Node.js are sometimes asynchronous and may occur over multiple ticks of the Node.js event loop. Calling process.exit(), however, forces the process to exit before those additional writes to stdout can be performed.

Rather than calling process.exit() directly, the code should set the process.exitCode and allow the process to exit naturally by avoiding scheduling any additional work for the event loop:

import process from 'node:process';
// How to properly set the exit code while letting
// the process exit gracefully.
if (someConditionNotMet()) {
printUsageToStdout();
process.exitCode = 1;
}

If it is necessary to terminate the Node.js process due to an error condition, throwing an uncaught error and allowing the process to terminate accordingly is safer than calling process.exit().

In Worker threads, this function stops the current thread rather than the current process.

@sincev0.1.13

@paramcode The exit code. For string type, only integer strings (e.g.,'1') are allowed.

exit
(0);
return null;
}
const
const dbUrl: string | symbol
dbUrl
= await
function text(opts: TextOptions): Promise<string | symbol>
text
({
TextOptions.message: string
message
: 'Enter database URL:',
TextOptions.placeholder?: string
placeholder
: `postgresql://user:pass@localhost:5432/db`,
TextOptions.validate?: (value: string) => string | Error | undefined
validate
: (
value: string
value
) => {
if (!
value: string
value
) return 'Database URL is required';
try {
new
var URL: new (input: string | {
toString: () => string;
}, base?: string | URL) => URL

URL class is a global reference for import { URL } from 'url' https://nodejs.org/api/url.html#the-whatwg-url-api

@sincev10.0.0

URL
(
value: string
value
);
return
var undefined
undefined
;
} catch {
return 'Invalid URL format';
}
},
});
if (
function isCancel(value: unknown): value is symbol
isCancel
(
const dbUrl: string | symbol
dbUrl
)) {
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
('Operation cancelled');
var process: NodeJS.Process
process
.
NodeJS.Process.exit(code?: number | string | null | undefined): never

The process.exit() method instructs Node.js to terminate the process synchronously with an exit status of code. If code is omitted, exit uses either the 'success' code 0 or the value of process.exitCode if it has been set. Node.js will not terminate until all the 'exit' event listeners are called.

To exit with a 'failure' code:

import { exit } from 'node:process';
exit(1);

The shell that executed Node.js should see the exit code as 1.

Calling process.exit() will force the process to exit as quickly as possible even if there are still asynchronous operations pending that have not yet completed fully, including I/O operations to process.stdout and process.stderr.

In most situations, it is not actually necessary to call process.exit() explicitly. The Node.js process will exit on its own if there is no additional work pending in the event loop. The process.exitCode property can be set to tell the process which exit code to use when the process exits gracefully.

For instance, the following example illustrates a misuse of the process.exit() method that could lead to data printed to stdout being truncated and lost:

import { exit } from 'node:process';
// This is an example of what *not* to do:
if (someConditionNotMet()) {
printUsageToStdout();
exit(1);
}

The reason this is problematic is because writes to process.stdout in Node.js are sometimes asynchronous and may occur over multiple ticks of the Node.js event loop. Calling process.exit(), however, forces the process to exit before those additional writes to stdout can be performed.

Rather than calling process.exit() directly, the code should set the process.exitCode and allow the process to exit naturally by avoiding scheduling any additional work for the event loop:

import process from 'node:process';
// How to properly set the exit code while letting
// the process exit gracefully.
if (someConditionNotMet()) {
printUsageToStdout();
process.exitCode = 1;
}

If it is necessary to terminate the Node.js process due to an error condition, throwing an uncaught error and allowing the process to terminate accordingly is safer than calling process.exit().

In Worker threads, this function stops the current thread rather than the current process.

@sincev0.1.13

@paramcode The exit code. For string type, only integer strings (e.g.,'1') are allowed.

exit
(0);
return null;
}
const
const features: symbol | ("auth" | "cors" | "logging" | "metrics" | "swagger" | "debug")[]
features
= await
groupMultiselect<"auth" | "cors" | "logging" | "metrics" | "swagger" | "debug">(opts: GroupMultiSelectOptions<"auth" | "cors" | "logging" | "metrics" | "swagger" | "debug">): Promise<...>
groupMultiselect
({
GroupMultiSelectOptions<Value>.message: string
message
: 'Select features:',
GroupMultiSelectOptions<"auth" | "cors" | "logging" | "metrics" | "swagger" | "debug">.options: Record<string, ({
value: "auth";
label?: string;
hint?: string;
} | {
value: "cors";
label?: string;
hint?: string;
} | {
value: "logging";
label?: string;
hint?: string;
} | {
value: "metrics";
label?: string;
hint?: string;
} | {
...;
} | {
...;
})[]>
options
: {
'Security': [
{
value: "auth"

Internal data for this option.

value
: 'auth',
label?: string

The optional, user-facing text for this option.

By default, the value is converted to a string.

label
: 'Authentication',
hint?: string

An optional hint to display to the user when this option might be selected.

By default, no hint is displayed.

hint
: 'User authentication' },
{
value: "cors"

Internal data for this option.

value
: 'cors',
label?: string

The optional, user-facing text for this option.

By default, the value is converted to a string.

label
: 'CORS',
hint?: string

An optional hint to display to the user when this option might be selected.

By default, no hint is displayed.

hint
: 'Cross-origin resource sharing' },
],
'Monitoring': [
{
value: "logging"

Internal data for this option.

value
: 'logging',
label?: string

The optional, user-facing text for this option.

By default, the value is converted to a string.

label
: 'Logging',
hint?: string

An optional hint to display to the user when this option might be selected.

By default, no hint is displayed.

hint
: 'Application logging' },
{
value: "metrics"

Internal data for this option.

value
: 'metrics',
label?: string

The optional, user-facing text for this option.

By default, the value is converted to a string.

label
: 'Metrics',
hint?: string

An optional hint to display to the user when this option might be selected.

By default, no hint is displayed.

hint
: 'Performance metrics' },
],
'Development': [
{
value: "swagger"

Internal data for this option.

value
: 'swagger',
label?: string

The optional, user-facing text for this option.

By default, the value is converted to a string.

label
: 'Swagger',
hint?: string

An optional hint to display to the user when this option might be selected.

By default, no hint is displayed.

hint
: 'API documentation' },
{
value: "debug"

Internal data for this option.

value
: 'debug',
label?: string

The optional, user-facing text for this option.

By default, the value is converted to a string.

label
: 'Debug',
hint?: string

An optional hint to display to the user when this option might be selected.

By default, no hint is displayed.

hint
: 'Debugging tools' },
],
},
});
if (
function isCancel(value: unknown): value is symbol
isCancel
(
const features: symbol | ("auth" | "cors" | "logging" | "metrics" | "swagger" | "debug")[]
features
)) {
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
('Operation cancelled');
var process: NodeJS.Process
process
.
NodeJS.Process.exit(code?: number | string | null | undefined): never

The process.exit() method instructs Node.js to terminate the process synchronously with an exit status of code. If code is omitted, exit uses either the 'success' code 0 or the value of process.exitCode if it has been set. Node.js will not terminate until all the 'exit' event listeners are called.

To exit with a 'failure' code:

import { exit } from 'node:process';
exit(1);

The shell that executed Node.js should see the exit code as 1.

Calling process.exit() will force the process to exit as quickly as possible even if there are still asynchronous operations pending that have not yet completed fully, including I/O operations to process.stdout and process.stderr.

In most situations, it is not actually necessary to call process.exit() explicitly. The Node.js process will exit on its own if there is no additional work pending in the event loop. The process.exitCode property can be set to tell the process which exit code to use when the process exits gracefully.

For instance, the following example illustrates a misuse of the process.exit() method that could lead to data printed to stdout being truncated and lost:

import { exit } from 'node:process';
// This is an example of what *not* to do:
if (someConditionNotMet()) {
printUsageToStdout();
exit(1);
}

The reason this is problematic is because writes to process.stdout in Node.js are sometimes asynchronous and may occur over multiple ticks of the Node.js event loop. Calling process.exit(), however, forces the process to exit before those additional writes to stdout can be performed.

Rather than calling process.exit() directly, the code should set the process.exitCode and allow the process to exit naturally by avoiding scheduling any additional work for the event loop:

import process from 'node:process';
// How to properly set the exit code while letting
// the process exit gracefully.
if (someConditionNotMet()) {
printUsageToStdout();
process.exitCode = 1;
}

If it is necessary to terminate the Node.js process due to an error condition, throwing an uncaught error and allowing the process to terminate accordingly is safer than calling process.exit().

In Worker threads, this function stops the current thread rather than the current process.

@sincev0.1.13

@paramcode The exit code. For string type, only integer strings (e.g.,'1') are allowed.

exit
(0);
return null;
}
return {
Config.port: number
port
:
var Number: NumberConstructor
(value?: any) => number

An object that represents a number of any kind. All JavaScript numbers are 64-bit floating-point numbers.

Number
(
const port: string
port
),
Config.host: string
host
,
Config.mode: "development" | "production"
mode
:
const mode: "development" | "production"
mode
as 'development' | 'production',
Config.features: string[]
features
,
Config.database: {
type: "postgres" | "mysql" | "mongodb";
url: string;
}
database
: {
type: "postgres" | "mysql" | "mongodb"
type
:
const database: "postgres" | "mysql" | "mongodb"
database
as 'postgres' | 'mysql' | 'mongodb',
url: string
url
:
const dbUrl: string
dbUrl
,
},
};
}
import {
const text: (opts: TextOptions) => Promise<string | symbol>
text
,
const select: <Value>(opts: SelectOptions<Value>) => Promise<Value | symbol>
select
,
const confirm: (opts: ConfirmOptions) => Promise<boolean | symbol>
confirm
,
const tasks: (tasks: Task[], opts?: CommonOptions) => Promise<void>

Define a group of tasks to be executed

tasks
,
const spinner: ({ indicator, onCancel, output, cancelMessage, errorMessage, frames, delay, }?: SpinnerOptions) => SpinnerResult
spinner
,
function isCancel(value: unknown): value is symbol
isCancel
} from '@clack/prompts';
async function
function cliTool(): Promise<void>
cliTool
() {
while (true) {
const
const action: symbol | "create" | "list" | "delete" | "update" | "exit"
action
= await
select<"create" | "list" | "delete" | "update" | "exit">(opts: SelectOptions<"create" | "list" | "delete" | "update" | "exit">): Promise<symbol | "create" | "list" | "delete" | "update" | "exit">
select
({
SelectOptions<Value>.message: string
message
: 'What would you like to do?',
SelectOptions<"create" | "list" | "delete" | "update" | "exit">.options: ({
value: "create";
label?: string;
hint?: string;
} | {
value: "list";
label?: string;
hint?: string;
} | {
value: "delete";
label?: string;
hint?: string;
} | {
value: "update";
label?: string;
hint?: string;
} | {
...;
})[]
options
: [
{
value: "create"

Internal data for this option.

value
: 'create',
label?: string

The optional, user-facing text for this option.

By default, the value is converted to a string.

label
: 'Create new item',
hint?: string

An optional hint to display to the user when this option might be selected.

By default, no hint is displayed.

hint
: 'Create a new resource' },
{
value: "list"

Internal data for this option.

value
: 'list',
label?: string

The optional, user-facing text for this option.

By default, the value is converted to a string.

label
: 'List items',
hint?: string

An optional hint to display to the user when this option might be selected.

By default, no hint is displayed.

hint
: 'View all resources' },
{
value: "delete"

Internal data for this option.

value
: 'delete',
label?: string

The optional, user-facing text for this option.

By default, the value is converted to a string.

label
: 'Delete item',
hint?: string

An optional hint to display to the user when this option might be selected.

By default, no hint is displayed.

hint
: 'Remove a resource' },
{
value: "update"

Internal data for this option.

value
: 'update',
label?: string

The optional, user-facing text for this option.

By default, the value is converted to a string.

label
: 'Update item',
hint?: string

An optional hint to display to the user when this option might be selected.

By default, no hint is displayed.

hint
: 'Modify a resource' },
{
value: "exit"

Internal data for this option.

value
: 'exit',
label?: string

The optional, user-facing text for this option.

By default, the value is converted to a string.

label
: 'Exit',
hint?: string

An optional hint to display to the user when this option might be selected.

By default, no hint is displayed.

hint
: 'Close the application' },
],
});
if (
function isCancel(value: unknown): value is symbol
isCancel
(
const action: symbol | "create" | "list" | "delete" | "update" | "exit"
action
)) {
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
('Operation cancelled');
var process: NodeJS.Process
process
.
NodeJS.Process.exit(code?: number | string | null | undefined): never

The process.exit() method instructs Node.js to terminate the process synchronously with an exit status of code. If code is omitted, exit uses either the 'success' code 0 or the value of process.exitCode if it has been set. Node.js will not terminate until all the 'exit' event listeners are called.

To exit with a 'failure' code:

import { exit } from 'node:process';
exit(1);

The shell that executed Node.js should see the exit code as 1.

Calling process.exit() will force the process to exit as quickly as possible even if there are still asynchronous operations pending that have not yet completed fully, including I/O operations to process.stdout and process.stderr.

In most situations, it is not actually necessary to call process.exit() explicitly. The Node.js process will exit on its own if there is no additional work pending in the event loop. The process.exitCode property can be set to tell the process which exit code to use when the process exits gracefully.

For instance, the following example illustrates a misuse of the process.exit() method that could lead to data printed to stdout being truncated and lost:

import { exit } from 'node:process';
// This is an example of what *not* to do:
if (someConditionNotMet()) {
printUsageToStdout();
exit(1);
}

The reason this is problematic is because writes to process.stdout in Node.js are sometimes asynchronous and may occur over multiple ticks of the Node.js event loop. Calling process.exit(), however, forces the process to exit before those additional writes to stdout can be performed.

Rather than calling process.exit() directly, the code should set the process.exitCode and allow the process to exit naturally by avoiding scheduling any additional work for the event loop:

import process from 'node:process';
// How to properly set the exit code while letting
// the process exit gracefully.
if (someConditionNotMet()) {
printUsageToStdout();
process.exitCode = 1;
}

If it is necessary to terminate the Node.js process due to an error condition, throwing an uncaught error and allowing the process to terminate accordingly is safer than calling process.exit().

In Worker threads, this function stops the current thread rather than the current process.

@sincev0.1.13

@paramcode The exit code. For string type, only integer strings (e.g.,'1') are allowed.

exit
(0);
}
if (
const action: "create" | "list" | "delete" | "update" | "exit"
action
=== 'exit') break;
switch (
const action: "create" | "list" | "delete" | "update"
action
) {
case 'create': {
const
const name: string | symbol
name
= await
function text(opts: TextOptions): Promise<string | symbol>
text
({
TextOptions.message: string
message
: 'Enter item name:',
TextOptions.validate?: (value: string) => string | Error | undefined
validate
: (
value: string
value
) => {
if (!
value: string
value
) return 'Name is required';
return
var undefined
undefined
;
},
});
if (
function isCancel(value: unknown): value is symbol
isCancel
(
const name: string | symbol
name
)) {
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
('Operation cancelled');
break;
}
const
const spin: SpinnerResult
spin
=
function spinner({ indicator, onCancel, output, cancelMessage, errorMessage, frames, delay, }?: SpinnerOptions): SpinnerResult
spinner
();
const spin: SpinnerResult
spin
.
SpinnerResult.start(msg?: string): void
start
('Creating item...');
await
function tasks(tasks: Task[], opts?: CommonOptions): Promise<void>

Define a group of tasks to be executed

tasks
([
{
title: string

Task title

title
: 'Validating input',
task: (message: (string: string) => void) => string | Promise<string> | void | Promise<void>

Task function

task
: async () => {
// Validation logic
return 'Input validated';
},
},
{
title: string

Task title

title
: 'Creating resource',
task: (message: (string: string) => void) => string | Promise<string> | void | Promise<void>

Task function

task
: async () => {
// Creation logic
return 'Resource created';
},
},
{
title: string

Task title

title
: 'Setting up permissions',
task: (message: (string: string) => void) => string | Promise<string> | void | Promise<void>

Task function

task
: async () => {
// Permission setup
return 'Permissions configured';
},
},
]);
const spin: SpinnerResult
spin
.
SpinnerResult.stop(msg?: string, code?: number): void
stop
('Item created successfully');
break;
}
case 'list': {
const
const spin: SpinnerResult
spin
=
function spinner({ indicator, onCancel, output, cancelMessage, errorMessage, frames, delay, }?: SpinnerOptions): SpinnerResult
spinner
();
const spin: SpinnerResult
spin
.
SpinnerResult.start(msg?: string): void
start
('Fetching items...');
// Simulate API call
await new
var Promise: PromiseConstructor
new <unknown>(executor: (resolve: (value: unknown) => void, reject: (reason?: any) => void) => void) => Promise<unknown>

Creates a new Promise.

@paramexecutor A callback used to initialize the promise. This callback is passed two arguments: a resolve callback used to resolve the promise with a value or the result of another promise, and a reject callback used to reject the promise with a provided reason or error.

Promise
(
resolve: (value: unknown) => void
resolve
=>
function setTimeout(callback: (args: void) => void, ms?: number): NodeJS.Timeout (+1 overload)

Schedules execution of a one-time callback after delay milliseconds.

The callback will likely not be invoked in precisely delay milliseconds. Node.js makes no guarantees about the exact timing of when callbacks will fire, nor of their ordering. The callback will be called as close as possible to the time specified.

When delay is larger than 2147483647 or less than 1, the delay will be set to 1. Non-integer delays are truncated to an integer.

If callback is not a function, a TypeError will be thrown.

This method has a custom variant for promises that is available using timersPromises.setTimeout().

@sincev0.0.1

@paramcallback The function to call when the timer elapses.

@paramdelay The number of milliseconds to wait before calling the callback.

@paramargs Optional arguments to pass when the callback is called.

setTimeout
(
resolve: (value: unknown) => void
resolve
, 1000));
const spin: SpinnerResult
spin
.
SpinnerResult.stop(msg?: string, code?: number): void
stop
('Items fetched successfully');
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
('Listing items...');
break;
}
case 'delete': {
const
const item: string | symbol
item
= await
function text(opts: TextOptions): Promise<string | symbol>
text
({
TextOptions.message: string
message
: 'Enter item to delete:',
TextOptions.validate?: (value: string) => string | Error | undefined
validate
: (
value: string
value
) => {
if (!
value: string
value
) return 'Item name is required';
return
var undefined
undefined
;
},
});
if (
function isCancel(value: unknown): value is symbol
isCancel
(
const item: string | symbol
item
)) {
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
('Operation cancelled');
break;
}
const
const shouldDelete: boolean | symbol
shouldDelete
= await
function confirm(opts: ConfirmOptions): Promise<boolean | symbol>
confirm
({
ConfirmOptions.message: string
message
: `Are you sure you want to delete ${
const item: string
item
}?`,
});
if (
function isCancel(value: unknown): value is symbol
isCancel
(
const shouldDelete: boolean | symbol
shouldDelete
)) {
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
('Operation cancelled');
break;
}
if (
const shouldDelete: boolean
shouldDelete
) {
const
const spin: SpinnerResult
spin
=
function spinner({ indicator, onCancel, output, cancelMessage, errorMessage, frames, delay, }?: SpinnerOptions): SpinnerResult
spinner
();
const spin: SpinnerResult
spin
.
SpinnerResult.start(msg?: string): void
start
('Deleting item...');
await
function tasks(tasks: Task[], opts?: CommonOptions): Promise<void>

Define a group of tasks to be executed

tasks
([
{
title: string

Task title

title
: 'Checking dependencies',
task: (message: (string: string) => void) => string | Promise<string> | void | Promise<void>

Task function

task
: async () => {
// Check dependencies
return 'Dependencies checked';
},
},
{
title: string

Task title

title
: 'Removing item',
task: (message: (string: string) => void) => string | Promise<string> | void | Promise<void>

Task function

task
: async () => {
// Delete logic
return 'Item removed';
},
},
{
title: string

Task title

title
: 'Cleaning up',
task: (message: (string: string) => void) => string | Promise<string> | void | Promise<void>

Task function

task
: async () => {
// Cleanup logic
return 'Cleanup completed';
},
},
]);
const spin: SpinnerResult
spin
.
SpinnerResult.stop(msg?: string, code?: number): void
stop
('Item deleted successfully');
}
break;
}
case 'update': {
const
const item: string | symbol
item
= await
function text(opts: TextOptions): Promise<string | symbol>
text
({
TextOptions.message: string
message
: 'Enter item to update:',
TextOptions.validate?: (value: string) => string | Error | undefined
validate
: (
value: string
value
) => {
if (!
value: string
value
) return 'Item name is required';
return
var undefined
undefined
;
},
});
if (
function isCancel(value: unknown): value is symbol
isCancel
(
const item: string | symbol
item
)) {
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
('Operation cancelled');
break;
}
const
const spin: SpinnerResult
spin
=
function spinner({ indicator, onCancel, output, cancelMessage, errorMessage, frames, delay, }?: SpinnerOptions): SpinnerResult
spinner
();
const spin: SpinnerResult
spin
.
SpinnerResult.start(msg?: string): void
start
('Updating item...');
await
function tasks(tasks: Task[], opts?: CommonOptions): Promise<void>

Define a group of tasks to be executed

tasks
([
{
title: string

Task title

title
: 'Fetching current state',
task: (message: (string: string) => void) => string | Promise<string> | void | Promise<void>

Task function

task
: async () => {
// Fetch current state
return 'Current state fetched';
},
},
{
title: string

Task title

title
: 'Applying updates',
task: (message: (string: string) => void) => string | Promise<string> | void | Promise<void>

Task function

task
: async () => {
// Update logic
return 'Updates applied';
},
},
{
title: string

Task title

title
: 'Verifying changes',
task: (message: (string: string) => void) => string | Promise<string> | void | Promise<void>

Task function

task
: async () => {
// Verification logic
return 'Changes verified';
},
},
]);
const spin: SpinnerResult
spin
.
SpinnerResult.stop(msg?: string, code?: number): void
stop
('Item updated successfully');
break;
}
}
}
}
import {
const text: (opts: TextOptions) => Promise<string | symbol>
text
,
const select: <Value>(opts: SelectOptions<Value>) => Promise<Value | symbol>
select
,
const groupMultiselect: <Value>(opts: GroupMultiSelectOptions<Value>) => Promise<Value[] | symbol>
groupMultiselect
,
function isCancel(value: unknown): value is symbol
isCancel
} from '@clack/prompts';
interface
interface UserData
UserData
{
UserData.name: string
name
: string;
UserData.email: string
email
: string;
UserData.age: number
age
: number;
UserData.role: string
role
: string;
UserData.skills: string[]
skills
: string[];
UserData.preferences: {
theme: "light" | "dark" | "system";
notifications: boolean;
language: string;
}
preferences
: {
theme: "light" | "dark" | "system"
theme
: 'light' | 'dark' | 'system';
notifications: boolean
notifications
: boolean;
language: string
language
: string;
};
}
async function
function collectUserData(): Promise<UserData | null>
collectUserData
():
interface Promise<T>

Represents the completion of an asynchronous operation

Promise
<
interface UserData
UserData
| null> {
const
const name: string | symbol
name
= await
function text(opts: TextOptions): Promise<string | symbol>
text
({
TextOptions.message: string
message
: 'Full name:',
TextOptions.validate?: (value: string) => string | Error | undefined
validate
: (
value: string
value
) => {
if (
value: string
value
.
String.length: number

Returns the length of a String object.

length
< 2) return 'Name must be at least 2 characters';
if (!/^[a-zA-Z\s]*$/.
RegExp.test(string: string): boolean

Returns a Boolean value that indicates whether or not a pattern exists in a searched string.

@paramstring String on which to perform the search.

test
(
value: string
value
)) return 'Name can only contain letters and spaces';
return
var undefined
undefined
;
},
});
if (
function isCancel(value: unknown): value is symbol
isCancel
(
const name: string | symbol
name
)) {
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
('Operation cancelled');
var process: NodeJS.Process
process
.
NodeJS.Process.exit(code?: number | string | null | undefined): never

The process.exit() method instructs Node.js to terminate the process synchronously with an exit status of code. If code is omitted, exit uses either the 'success' code 0 or the value of process.exitCode if it has been set. Node.js will not terminate until all the 'exit' event listeners are called.

To exit with a 'failure' code:

import { exit } from 'node:process';
exit(1);

The shell that executed Node.js should see the exit code as 1.

Calling process.exit() will force the process to exit as quickly as possible even if there are still asynchronous operations pending that have not yet completed fully, including I/O operations to process.stdout and process.stderr.

In most situations, it is not actually necessary to call process.exit() explicitly. The Node.js process will exit on its own if there is no additional work pending in the event loop. The process.exitCode property can be set to tell the process which exit code to use when the process exits gracefully.

For instance, the following example illustrates a misuse of the process.exit() method that could lead to data printed to stdout being truncated and lost:

import { exit } from 'node:process';
// This is an example of what *not* to do:
if (someConditionNotMet()) {
printUsageToStdout();
exit(1);
}

The reason this is problematic is because writes to process.stdout in Node.js are sometimes asynchronous and may occur over multiple ticks of the Node.js event loop. Calling process.exit(), however, forces the process to exit before those additional writes to stdout can be performed.

Rather than calling process.exit() directly, the code should set the process.exitCode and allow the process to exit naturally by avoiding scheduling any additional work for the event loop:

import process from 'node:process';
// How to properly set the exit code while letting
// the process exit gracefully.
if (someConditionNotMet()) {
printUsageToStdout();
process.exitCode = 1;
}

If it is necessary to terminate the Node.js process due to an error condition, throwing an uncaught error and allowing the process to terminate accordingly is safer than calling process.exit().

In Worker threads, this function stops the current thread rather than the current process.

@sincev0.1.13

@paramcode The exit code. For string type, only integer strings (e.g.,'1') are allowed.

exit
(0);
return null;
}
const
const email: string | symbol
email
= await
function text(opts: TextOptions): Promise<string | symbol>
text
({
TextOptions.message: string
message
: 'Email address:',
TextOptions.validate?: (value: string) => string | Error | undefined
validate
: (
value: string
value
) => {
const
const emailRegex: RegExp
emailRegex
= /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
if (!
const emailRegex: RegExp
emailRegex
.
RegExp.test(string: string): boolean

Returns a Boolean value that indicates whether or not a pattern exists in a searched string.

@paramstring String on which to perform the search.

test
(
value: string
value
)) return 'Please enter a valid email address';
return
var undefined
undefined
;
},
});
if (
function isCancel(value: unknown): value is symbol
isCancel
(
const email: string | symbol
email
)) {
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
('Operation cancelled');
var process: NodeJS.Process
process
.
NodeJS.Process.exit(code?: number | string | null | undefined): never

The process.exit() method instructs Node.js to terminate the process synchronously with an exit status of code. If code is omitted, exit uses either the 'success' code 0 or the value of process.exitCode if it has been set. Node.js will not terminate until all the 'exit' event listeners are called.

To exit with a 'failure' code:

import { exit } from 'node:process';
exit(1);

The shell that executed Node.js should see the exit code as 1.

Calling process.exit() will force the process to exit as quickly as possible even if there are still asynchronous operations pending that have not yet completed fully, including I/O operations to process.stdout and process.stderr.

In most situations, it is not actually necessary to call process.exit() explicitly. The Node.js process will exit on its own if there is no additional work pending in the event loop. The process.exitCode property can be set to tell the process which exit code to use when the process exits gracefully.

For instance, the following example illustrates a misuse of the process.exit() method that could lead to data printed to stdout being truncated and lost:

import { exit } from 'node:process';
// This is an example of what *not* to do:
if (someConditionNotMet()) {
printUsageToStdout();
exit(1);
}

The reason this is problematic is because writes to process.stdout in Node.js are sometimes asynchronous and may occur over multiple ticks of the Node.js event loop. Calling process.exit(), however, forces the process to exit before those additional writes to stdout can be performed.

Rather than calling process.exit() directly, the code should set the process.exitCode and allow the process to exit naturally by avoiding scheduling any additional work for the event loop:

import process from 'node:process';
// How to properly set the exit code while letting
// the process exit gracefully.
if (someConditionNotMet()) {
printUsageToStdout();
process.exitCode = 1;
}

If it is necessary to terminate the Node.js process due to an error condition, throwing an uncaught error and allowing the process to terminate accordingly is safer than calling process.exit().

In Worker threads, this function stops the current thread rather than the current process.

@sincev0.1.13

@paramcode The exit code. For string type, only integer strings (e.g.,'1') are allowed.

exit
(0);
return null;
}
const
const ageInput: string | symbol
ageInput
= await
function text(opts: TextOptions): Promise<string | symbol>
text
({
TextOptions.message: string
message
: 'Age:',
TextOptions.validate?: (value: string) => string | Error | undefined
validate
: (
value: string
value
) => {
const
const num: number
num
=
function parseInt(string: string, radix?: number): number

Converts a string to an integer.

@paramstring A string to convert into a number.

@paramradix A value between 2 and 36 that specifies the base of the number in string. If this argument is not supplied, strings with a prefix of '0x' are considered hexadecimal. All other strings are considered decimal.

parseInt
(
value: string
value
);
if (
function isNaN(number: number): boolean

Returns a Boolean value that indicates whether a value is the reserved value NaN (not a number).

@paramnumber A numeric value.

isNaN
(
const num: number
num
)) return 'Please enter a valid number';
if (
const num: number
num
< 18 ||
const num: number
num
> 100) return 'Age must be between 18 and 100';
return
var undefined
undefined
;
},
});
if (
function isCancel(value: unknown): value is symbol
isCancel
(
const ageInput: string | symbol
ageInput
)) {
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
('Operation cancelled');
var process: NodeJS.Process
process
.
NodeJS.Process.exit(code?: number | string | null | undefined): never

The process.exit() method instructs Node.js to terminate the process synchronously with an exit status of code. If code is omitted, exit uses either the 'success' code 0 or the value of process.exitCode if it has been set. Node.js will not terminate until all the 'exit' event listeners are called.

To exit with a 'failure' code:

import { exit } from 'node:process';
exit(1);

The shell that executed Node.js should see the exit code as 1.

Calling process.exit() will force the process to exit as quickly as possible even if there are still asynchronous operations pending that have not yet completed fully, including I/O operations to process.stdout and process.stderr.

In most situations, it is not actually necessary to call process.exit() explicitly. The Node.js process will exit on its own if there is no additional work pending in the event loop. The process.exitCode property can be set to tell the process which exit code to use when the process exits gracefully.

For instance, the following example illustrates a misuse of the process.exit() method that could lead to data printed to stdout being truncated and lost:

import { exit } from 'node:process';
// This is an example of what *not* to do:
if (someConditionNotMet()) {
printUsageToStdout();
exit(1);
}

The reason this is problematic is because writes to process.stdout in Node.js are sometimes asynchronous and may occur over multiple ticks of the Node.js event loop. Calling process.exit(), however, forces the process to exit before those additional writes to stdout can be performed.

Rather than calling process.exit() directly, the code should set the process.exitCode and allow the process to exit naturally by avoiding scheduling any additional work for the event loop:

import process from 'node:process';
// How to properly set the exit code while letting
// the process exit gracefully.
if (someConditionNotMet()) {
printUsageToStdout();
process.exitCode = 1;
}

If it is necessary to terminate the Node.js process due to an error condition, throwing an uncaught error and allowing the process to terminate accordingly is safer than calling process.exit().

In Worker threads, this function stops the current thread rather than the current process.

@sincev0.1.13

@paramcode The exit code. For string type, only integer strings (e.g.,'1') are allowed.

exit
(0);
return null;
}
const
const role: symbol | "admin" | "user" | "guest"
role
= await
select<"admin" | "user" | "guest">(opts: SelectOptions<"admin" | "user" | "guest">): Promise<symbol | "admin" | "user" | "guest">
select
({
SelectOptions<Value>.message: string
message
: 'Select role:',
SelectOptions<"admin" | "user" | "guest">.options: ({
value: "admin";
label?: string;
hint?: string;
} | {
value: "user";
label?: string;
hint?: string;
} | {
value: "guest";
label?: string;
hint?: string;
})[]
options
: [
{
value: "admin"

Internal data for this option.

value
: 'admin',
label?: string

The optional, user-facing text for this option.

By default, the value is converted to a string.

label
: 'Administrator',
hint?: string

An optional hint to display to the user when this option might be selected.

By default, no hint is displayed.

hint
: 'Full system access' },
{
value: "user"

Internal data for this option.

value
: 'user',
label?: string

The optional, user-facing text for this option.

By default, the value is converted to a string.

label
: 'User',
hint?: string

An optional hint to display to the user when this option might be selected.

By default, no hint is displayed.

hint
: 'Standard user access' },
{
value: "guest"

Internal data for this option.

value
: 'guest',
label?: string

The optional, user-facing text for this option.

By default, the value is converted to a string.

label
: 'Guest',
hint?: string

An optional hint to display to the user when this option might be selected.

By default, no hint is displayed.

hint
: 'Limited access' },
],
});
if (
function isCancel(value: unknown): value is symbol
isCancel
(
const role: symbol | "admin" | "user" | "guest"
role
)) {
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
('Operation cancelled');
var process: NodeJS.Process
process
.
NodeJS.Process.exit(code?: number | string | null | undefined): never

The process.exit() method instructs Node.js to terminate the process synchronously with an exit status of code. If code is omitted, exit uses either the 'success' code 0 or the value of process.exitCode if it has been set. Node.js will not terminate until all the 'exit' event listeners are called.

To exit with a 'failure' code:

import { exit } from 'node:process';
exit(1);

The shell that executed Node.js should see the exit code as 1.

Calling process.exit() will force the process to exit as quickly as possible even if there are still asynchronous operations pending that have not yet completed fully, including I/O operations to process.stdout and process.stderr.

In most situations, it is not actually necessary to call process.exit() explicitly. The Node.js process will exit on its own if there is no additional work pending in the event loop. The process.exitCode property can be set to tell the process which exit code to use when the process exits gracefully.

For instance, the following example illustrates a misuse of the process.exit() method that could lead to data printed to stdout being truncated and lost:

import { exit } from 'node:process';
// This is an example of what *not* to do:
if (someConditionNotMet()) {
printUsageToStdout();
exit(1);
}

The reason this is problematic is because writes to process.stdout in Node.js are sometimes asynchronous and may occur over multiple ticks of the Node.js event loop. Calling process.exit(), however, forces the process to exit before those additional writes to stdout can be performed.

Rather than calling process.exit() directly, the code should set the process.exitCode and allow the process to exit naturally by avoiding scheduling any additional work for the event loop:

import process from 'node:process';
// How to properly set the exit code while letting
// the process exit gracefully.
if (someConditionNotMet()) {
printUsageToStdout();
process.exitCode = 1;
}

If it is necessary to terminate the Node.js process due to an error condition, throwing an uncaught error and allowing the process to terminate accordingly is safer than calling process.exit().

In Worker threads, this function stops the current thread rather than the current process.

@sincev0.1.13

@paramcode The exit code. For string type, only integer strings (e.g.,'1') are allowed.

exit
(0);
return null;
}
const
const skills: symbol | ("react" | "vue" | "angular" | "node" | "python" | "java" | "sql" | "mongodb" | "redis")[]
skills
= await
groupMultiselect<"react" | "vue" | "angular" | "node" | "python" | "java" | "sql" | "mongodb" | "redis">(opts: GroupMultiSelectOptions<"react" | "vue" | "angular" | "node" | "python" | "java" | "sql" | "mongodb" | "redis">): Promise<...>
groupMultiselect
({
GroupMultiSelectOptions<Value>.message: string
message
: 'Select skills:',
GroupMultiSelectOptions<"react" | "vue" | "angular" | "node" | "python" | "java" | "sql" | "mongodb" | "redis">.options: Record<string, ({
value: "react";
label?: string;
hint?: string;
} | {
value: "vue";
label?: string;
hint?: string;
} | {
value: "angular";
label?: string;
hint?: string;
} | {
value: "node";
label?: string;
hint?: string;
} | ... 4 more ... | {
...;
})[]>
options
: {
'Frontend': [
{
value: "react"

Internal data for this option.

value
: 'react',
label?: string

The optional, user-facing text for this option.

By default, the value is converted to a string.

label
: 'React',
hint?: string

An optional hint to display to the user when this option might be selected.

By default, no hint is displayed.

hint
: 'UI library' },
{
value: "vue"

Internal data for this option.

value
: 'vue',
label?: string

The optional, user-facing text for this option.

By default, the value is converted to a string.

label
: 'Vue',
hint?: string

An optional hint to display to the user when this option might be selected.

By default, no hint is displayed.

hint
: 'Progressive framework' },
{
value: "angular"

Internal data for this option.

value
: 'angular',
label?: string

The optional, user-facing text for this option.

By default, the value is converted to a string.

label
: 'Angular',
hint?: string

An optional hint to display to the user when this option might be selected.

By default, no hint is displayed.

hint
: 'Platform' },
],
'Backend': [
{
value: "node"

Internal data for this option.

value
: 'node',
label?: string

The optional, user-facing text for this option.

By default, the value is converted to a string.

label
: 'Node.js',
hint?: string

An optional hint to display to the user when this option might be selected.

By default, no hint is displayed.

hint
: 'Runtime' },
{
value: "python"

Internal data for this option.

value
: 'python',
label?: string

The optional, user-facing text for this option.

By default, the value is converted to a string.

label
: 'Python',
hint?: string

An optional hint to display to the user when this option might be selected.

By default, no hint is displayed.

hint
: 'Language' },
{
value: "java"

Internal data for this option.

value
: 'java',
label?: string

The optional, user-facing text for this option.

By default, the value is converted to a string.

label
: 'Java',
hint?: string

An optional hint to display to the user when this option might be selected.

By default, no hint is displayed.

hint
: 'Language' },
],
'Database': [
{
value: "sql"

Internal data for this option.

value
: 'sql',
label?: string

The optional, user-facing text for this option.

By default, the value is converted to a string.

label
: 'SQL',
hint?: string

An optional hint to display to the user when this option might be selected.

By default, no hint is displayed.

hint
: 'Query language' },
{
value: "mongodb"

Internal data for this option.

value
: 'mongodb',
label?: string

The optional, user-facing text for this option.

By default, the value is converted to a string.

label
: 'MongoDB',
hint?: string

An optional hint to display to the user when this option might be selected.

By default, no hint is displayed.

hint
: 'NoSQL database' },
{
value: "redis"

Internal data for this option.

value
: 'redis',
label?: string

The optional, user-facing text for this option.

By default, the value is converted to a string.

label
: 'Redis',
hint?: string

An optional hint to display to the user when this option might be selected.

By default, no hint is displayed.

hint
: 'Cache' },
],
},
});
if (
function isCancel(value: unknown): value is symbol
isCancel
(
const skills: symbol | ("react" | "vue" | "angular" | "node" | "python" | "java" | "sql" | "mongodb" | "redis")[]
skills
)) {
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
('Operation cancelled');
var process: NodeJS.Process
process
.
NodeJS.Process.exit(code?: number | string | null | undefined): never

The process.exit() method instructs Node.js to terminate the process synchronously with an exit status of code. If code is omitted, exit uses either the 'success' code 0 or the value of process.exitCode if it has been set. Node.js will not terminate until all the 'exit' event listeners are called.

To exit with a 'failure' code:

import { exit } from 'node:process';
exit(1);

The shell that executed Node.js should see the exit code as 1.

Calling process.exit() will force the process to exit as quickly as possible even if there are still asynchronous operations pending that have not yet completed fully, including I/O operations to process.stdout and process.stderr.

In most situations, it is not actually necessary to call process.exit() explicitly. The Node.js process will exit on its own if there is no additional work pending in the event loop. The process.exitCode property can be set to tell the process which exit code to use when the process exits gracefully.

For instance, the following example illustrates a misuse of the process.exit() method that could lead to data printed to stdout being truncated and lost:

import { exit } from 'node:process';
// This is an example of what *not* to do:
if (someConditionNotMet()) {
printUsageToStdout();
exit(1);
}

The reason this is problematic is because writes to process.stdout in Node.js are sometimes asynchronous and may occur over multiple ticks of the Node.js event loop. Calling process.exit(), however, forces the process to exit before those additional writes to stdout can be performed.

Rather than calling process.exit() directly, the code should set the process.exitCode and allow the process to exit naturally by avoiding scheduling any additional work for the event loop:

import process from 'node:process';
// How to properly set the exit code while letting
// the process exit gracefully.
if (someConditionNotMet()) {
printUsageToStdout();
process.exitCode = 1;
}

If it is necessary to terminate the Node.js process due to an error condition, throwing an uncaught error and allowing the process to terminate accordingly is safer than calling process.exit().

In Worker threads, this function stops the current thread rather than the current process.

@sincev0.1.13

@paramcode The exit code. For string type, only integer strings (e.g.,'1') are allowed.

exit
(0);
return null;
}
const
const theme: symbol | "light" | "dark" | "system"
theme
= await
select<"light" | "dark" | "system">(opts: SelectOptions<"light" | "dark" | "system">): Promise<symbol | "light" | "dark" | "system">
select
({
SelectOptions<Value>.message: string
message
: 'Select theme:',
SelectOptions<"light" | "dark" | "system">.options: ({
value: "light";
label?: string;
hint?: string;
} | {
value: "dark";
label?: string;
hint?: string;
} | {
value: "system";
label?: string;
hint?: string;
})[]
options
: [
{
value: "light"

Internal data for this option.

value
: 'light',
label?: string

The optional, user-facing text for this option.

By default, the value is converted to a string.

label
: 'Light',
hint?: string

An optional hint to display to the user when this option might be selected.

By default, no hint is displayed.

hint
: 'Light mode' },
{
value: "dark"

Internal data for this option.

value
: 'dark',
label?: string

The optional, user-facing text for this option.

By default, the value is converted to a string.

label
: 'Dark',
hint?: string

An optional hint to display to the user when this option might be selected.

By default, no hint is displayed.

hint
: 'Dark mode' },
{
value: "system"

Internal data for this option.

value
: 'system',
label?: string

The optional, user-facing text for this option.

By default, the value is converted to a string.

label
: 'System',
hint?: string

An optional hint to display to the user when this option might be selected.

By default, no hint is displayed.

hint
: 'Follow system preference' },
],
});
if (
function isCancel(value: unknown): value is symbol
isCancel
(
const theme: symbol | "light" | "dark" | "system"
theme
)) {
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
('Operation cancelled');
var process: NodeJS.Process
process
.
NodeJS.Process.exit(code?: number | string | null | undefined): never

The process.exit() method instructs Node.js to terminate the process synchronously with an exit status of code. If code is omitted, exit uses either the 'success' code 0 or the value of process.exitCode if it has been set. Node.js will not terminate until all the 'exit' event listeners are called.

To exit with a 'failure' code:

import { exit } from 'node:process';
exit(1);

The shell that executed Node.js should see the exit code as 1.

Calling process.exit() will force the process to exit as quickly as possible even if there are still asynchronous operations pending that have not yet completed fully, including I/O operations to process.stdout and process.stderr.

In most situations, it is not actually necessary to call process.exit() explicitly. The Node.js process will exit on its own if there is no additional work pending in the event loop. The process.exitCode property can be set to tell the process which exit code to use when the process exits gracefully.

For instance, the following example illustrates a misuse of the process.exit() method that could lead to data printed to stdout being truncated and lost:

import { exit } from 'node:process';
// This is an example of what *not* to do:
if (someConditionNotMet()) {
printUsageToStdout();
exit(1);
}

The reason this is problematic is because writes to process.stdout in Node.js are sometimes asynchronous and may occur over multiple ticks of the Node.js event loop. Calling process.exit(), however, forces the process to exit before those additional writes to stdout can be performed.

Rather than calling process.exit() directly, the code should set the process.exitCode and allow the process to exit naturally by avoiding scheduling any additional work for the event loop:

import process from 'node:process';
// How to properly set the exit code while letting
// the process exit gracefully.
if (someConditionNotMet()) {
printUsageToStdout();
process.exitCode = 1;
}

If it is necessary to terminate the Node.js process due to an error condition, throwing an uncaught error and allowing the process to terminate accordingly is safer than calling process.exit().

In Worker threads, this function stops the current thread rather than the current process.

@sincev0.1.13

@paramcode The exit code. For string type, only integer strings (e.g.,'1') are allowed.

exit
(0);
return null;
}
const
const notifications: boolean | symbol
notifications
= await
select<boolean>(opts: SelectOptions<boolean>): Promise<boolean | symbol>
select
({
SelectOptions<Value>.message: string
message
: 'Enable notifications?',
SelectOptions<boolean>.options: ({
value: false;
label?: string;
hint?: string;
} | {
value: true;
label?: string;
hint?: string;
})[]
options
: [
{
value: true

Internal data for this option.

value
: true,
label?: string

The optional, user-facing text for this option.

By default, the value is converted to a string.

label
: 'Yes',
hint?: string

An optional hint to display to the user when this option might be selected.

By default, no hint is displayed.

hint
: 'Receive notifications' },
{
value: false

Internal data for this option.

value
: false,
label?: string

The optional, user-facing text for this option.

By default, the value is converted to a string.

label
: 'No',
hint?: string

An optional hint to display to the user when this option might be selected.

By default, no hint is displayed.

hint
: 'No notifications' },
],
});
if (
function isCancel(value: unknown): value is symbol
isCancel
(
const notifications: boolean | symbol
notifications
)) {
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
('Operation cancelled');
var process: NodeJS.Process
process
.
NodeJS.Process.exit(code?: number | string | null | undefined): never

The process.exit() method instructs Node.js to terminate the process synchronously with an exit status of code. If code is omitted, exit uses either the 'success' code 0 or the value of process.exitCode if it has been set. Node.js will not terminate until all the 'exit' event listeners are called.

To exit with a 'failure' code:

import { exit } from 'node:process';
exit(1);

The shell that executed Node.js should see the exit code as 1.

Calling process.exit() will force the process to exit as quickly as possible even if there are still asynchronous operations pending that have not yet completed fully, including I/O operations to process.stdout and process.stderr.

In most situations, it is not actually necessary to call process.exit() explicitly. The Node.js process will exit on its own if there is no additional work pending in the event loop. The process.exitCode property can be set to tell the process which exit code to use when the process exits gracefully.

For instance, the following example illustrates a misuse of the process.exit() method that could lead to data printed to stdout being truncated and lost:

import { exit } from 'node:process';
// This is an example of what *not* to do:
if (someConditionNotMet()) {
printUsageToStdout();
exit(1);
}

The reason this is problematic is because writes to process.stdout in Node.js are sometimes asynchronous and may occur over multiple ticks of the Node.js event loop. Calling process.exit(), however, forces the process to exit before those additional writes to stdout can be performed.

Rather than calling process.exit() directly, the code should set the process.exitCode and allow the process to exit naturally by avoiding scheduling any additional work for the event loop:

import process from 'node:process';
// How to properly set the exit code while letting
// the process exit gracefully.
if (someConditionNotMet()) {
printUsageToStdout();
process.exitCode = 1;
}

If it is necessary to terminate the Node.js process due to an error condition, throwing an uncaught error and allowing the process to terminate accordingly is safer than calling process.exit().

In Worker threads, this function stops the current thread rather than the current process.

@sincev0.1.13

@paramcode The exit code. For string type, only integer strings (e.g.,'1') are allowed.

exit
(0);
return null;
}
const
const language: symbol | "en" | "es" | "fr" | "de"
language
= await
select<"en" | "es" | "fr" | "de">(opts: SelectOptions<"en" | "es" | "fr" | "de">): Promise<symbol | "en" | "es" | "fr" | "de">
select
({
SelectOptions<Value>.message: string
message
: 'Select language:',
SelectOptions<"en" | "es" | "fr" | "de">.options: ({
value: "en";
label?: string;
hint?: string;
} | {
value: "es";
label?: string;
hint?: string;
} | {
value: "fr";
label?: string;
hint?: string;
} | {
value: "de";
label?: string;
hint?: string;
})[]
options
: [
{
value: "en"

Internal data for this option.

value
: 'en',
label?: string

The optional, user-facing text for this option.

By default, the value is converted to a string.

label
: 'English',
hint?: string

An optional hint to display to the user when this option might be selected.

By default, no hint is displayed.

hint
: 'English' },
{
value: "es"

Internal data for this option.

value
: 'es',
label?: string

The optional, user-facing text for this option.

By default, the value is converted to a string.

label
: 'Spanish',
hint?: string

An optional hint to display to the user when this option might be selected.

By default, no hint is displayed.

hint
: 'Español' },
{
value: "fr"

Internal data for this option.

value
: 'fr',
label?: string

The optional, user-facing text for this option.

By default, the value is converted to a string.

label
: 'French',
hint?: string

An optional hint to display to the user when this option might be selected.

By default, no hint is displayed.

hint
: 'Français' },
{
value: "de"

Internal data for this option.

value
: 'de',
label?: string

The optional, user-facing text for this option.

By default, the value is converted to a string.

label
: 'German',
hint?: string

An optional hint to display to the user when this option might be selected.

By default, no hint is displayed.

hint
: 'Deutsch' },
],
});
if (
function isCancel(value: unknown): value is symbol
isCancel
(
const language: symbol | "en" | "es" | "fr" | "de"
language
)) {
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
('Operation cancelled');
var process: NodeJS.Process
process
.
NodeJS.Process.exit(code?: number | string | null | undefined): never

The process.exit() method instructs Node.js to terminate the process synchronously with an exit status of code. If code is omitted, exit uses either the 'success' code 0 or the value of process.exitCode if it has been set. Node.js will not terminate until all the 'exit' event listeners are called.

To exit with a 'failure' code:

import { exit } from 'node:process';
exit(1);

The shell that executed Node.js should see the exit code as 1.

Calling process.exit() will force the process to exit as quickly as possible even if there are still asynchronous operations pending that have not yet completed fully, including I/O operations to process.stdout and process.stderr.

In most situations, it is not actually necessary to call process.exit() explicitly. The Node.js process will exit on its own if there is no additional work pending in the event loop. The process.exitCode property can be set to tell the process which exit code to use when the process exits gracefully.

For instance, the following example illustrates a misuse of the process.exit() method that could lead to data printed to stdout being truncated and lost:

import { exit } from 'node:process';
// This is an example of what *not* to do:
if (someConditionNotMet()) {
printUsageToStdout();
exit(1);
}

The reason this is problematic is because writes to process.stdout in Node.js are sometimes asynchronous and may occur over multiple ticks of the Node.js event loop. Calling process.exit(), however, forces the process to exit before those additional writes to stdout can be performed.

Rather than calling process.exit() directly, the code should set the process.exitCode and allow the process to exit naturally by avoiding scheduling any additional work for the event loop:

import process from 'node:process';
// How to properly set the exit code while letting
// the process exit gracefully.
if (someConditionNotMet()) {
printUsageToStdout();
process.exitCode = 1;
}

If it is necessary to terminate the Node.js process due to an error condition, throwing an uncaught error and allowing the process to terminate accordingly is safer than calling process.exit().

In Worker threads, this function stops the current thread rather than the current process.

@sincev0.1.13

@paramcode The exit code. For string type, only integer strings (e.g.,'1') are allowed.

exit
(0);
return null;
}
return {
UserData.name: string
name
,
UserData.email: string
email
,
UserData.age: number
age
:
function parseInt(string: string, radix?: number): number

Converts a string to an integer.

@paramstring A string to convert into a number.

@paramradix A value between 2 and 36 that specifies the base of the number in string. If this argument is not supplied, strings with a prefix of '0x' are considered hexadecimal. All other strings are considered decimal.

parseInt
(
const ageInput: string
ageInput
),
UserData.role: string
role
,
UserData.skills: string[]
skills
,
UserData.preferences: {
theme: "light" | "dark" | "system";
notifications: boolean;
language: string;
}
preferences
: {
theme: "light" | "dark" | "system"
theme
:
const theme: "light" | "dark" | "system"
theme
as 'light' | 'dark' | 'system',
notifications: boolean
notifications
:
const notifications: boolean
notifications
as boolean,
language: string
language
,
},
};
}

For more examples and best practices, check out our GitHub repository.