使用node命令不输出结果_使用Node输出到命令行

使用node命令不输出结果

使用控制台模块的基本输出 (Basic output using the console module)

Node provides a console module which provides tons of very useful ways to interact with the command line.

Node提供了一个console模块 ,该模块提供了大量与命令行交互的非常有用的方法。

It is basically the same as the console object you find in the browser.

它基本上与您在浏览器中找到的console对象相同。

The most basic and most used method is console.log(), which prints the string you pass to it to the console.

最基本和最常用的方法是console.log() ,该方法将您传递给控制台的字符串打印出来。

If you pass an object, it will render it as a string.

如果传递对象,它将把它渲染成字符串。

You can pass multiple variables to console.log, for example:

您可以将多个变量传递给console.log ,例如:

const x = 'x'
const y = 'y'
console.log(x, y)

and Node will print both.

和Node将同时打印。

We can also format pretty phrases by passing variables and a format specifier.

我们还可以通过传递变量和格式说明符来格式化漂亮的短语。

For example:

例如:

console.log('My %s has %d years', 'cat', 2)
  • %s format a variable as a string

    %s将变量格式化为字符串

  • %d or %i format a variable as an integer

    %d%i将变量格式化为整数

  • %f format a variable as a floating point number

    %f将变量格式化为浮点数

  • %O used to print an object representation

    %O用于打印对象表示

Example:

例:

console.log('%O', Number)

清除控制台 (Clear the console)

console.clear() clears the console (the behavior might depend on the console used)

console.clear()清除控制台(其行为可能取决于所使用的控制台)

计数元素 (Counting elements)

console.count() is a handy method.

console.count()是一种方便的方法。

Take this code:

采取以下代码:

const x = 1
const y = 2
const z = 3
console.count(
  'The value of x is ' + x + ' and has been checked .. how many times?'
)
console.count(
  'The value of x is ' + x + ' and has been checked .. how many times?'
)
console.count(
  'The value of y is ' + y + ' and has been checked .. how many times?'
)

What happens is that count will count the number of times a string is printed, and print the count next to it:

发生的是该计数将对打印字符串的次数进行计数,并在其旁边打印计数:

You can just count apples and oranges:

您可以只数苹果和橙子:

const oranges = ['orange', 'orange']
const apples = ['just one apple']
oranges.forEach(fruit => {
  console.count(fruit)
})
apples.forEach(fruit => {
  console.count(fruit)
})

There might be cases where it’s useful to print the call stack trace of a function, maybe to answer the question how did you reach that part of the code?

在某些情况下,打印函数的调用堆栈跟踪很有用,也许可以回答以下问题: 如何到达代码的那一部分?

You can do so using console.trace():

您可以使用console.trace()这样做:

const function2 = () => console.trace()
const function1 = () => function2()
function1()

This will print the stack trace. This is what’s printed if I try this in the Node REPL:

这将打印堆栈跟踪。 如果我在Node REPL中尝试此操作,将显示以下内容:

Trace
    at function2 (repl:1:33)
    at function1 (repl:1:25)
    at repl:1:1
    at ContextifyScript.Script.runInThisContext (vm.js:44:33)
    at REPLServer.defaultEval (repl.js:239:29)
    at bound (domain.js:301:14)
    at REPLServer.runBound [as eval] (domain.js:314:12)
    at REPLServer.onLine (repl.js:440:10)
    at emitOne (events.js:120:20)
    at REPLServer.emit (events.js:210:7)

计算花费的时间 (Calculate the time spent)

You can easily calculate how much time a function takes to run, using time() and timeEnd()

您可以使用time()timeEnd()轻松计算函数运行所需的time()

const doSomething = () => console.log('test')
const measureDoingSomething = () => {
  console.time('doSomething()')
  //do something, and measure the time it takes
  doSomething()
  console.timeEnd('doSomething()')
}
measureDoingSomething()

标准输出和标准错误 (stdout and stderr)

As we saw console.log is great for printing messages in the Console. This is what’s called the standard output, or stdout.

如我们所见,console.log非常适合在控制台中打印消息。 这就是所谓的标准输出或stdout

console.error prints to the stderr stream.

console.error打印到stderr流。

It will not appear in the console, but it will appear in the error log.

它不会出现在控制台中,但是会出现在错误日志中。

为输出着色 (Color the output)

You can color the output of your text in the console by using escape sequences. An escape sequence is a set of characters that identifies a color.

您可以使用转义序列在控制台中为文本的输出着色。 转义序列是标识颜色的一组字符。

Example:

例:

console.log('\x1b[33m%s\x1b[0m', 'hi!')

You can try that in the Node REPL, and it will print hi! in yellow.

您可以在Node REPL中尝试该方法,它会打印成hi! 黄色。

However, this is the low-level way to do this. The simplest way to go about coloring the console output is by using a library. Chalk is such a library, and in addition to coloring it also helps with other styling facilities, like making text bold, italic or underlined.

但是,这是执行此操作的底层方法。 为控制台输出着色的最简单方法是使用库。 Chalk是这样的一个库,除了为它上色外,它还有助于其他样式设置,例如使文本加粗,斜体或带下划线。

You install it with npm install chalk, then you can use it:

您可以使用npm install chalk安装它,然后可以使用它:

const chalk = require('chalk')
console.log(chalk.yellow('hi!'))

Using chalk.yellow is much more convenient than trying to remember the escape codes, and the code is much more readable.

与尝试记住转义代码相比,使用chalk.yellow方便得多,并且代码更具可读性。

Check the project link I posted above for more usage examples.

检查我上面发布的项目链接以获取更多用法示例。

创建进度条 (Create a progress bar)

Progress is an awesome package to create a progress bar in the console. Install it using npm install progress

Progress是一个很棒的软件包,可在控制台中创建进度条。 使用npm install progress进行npm install progress

This snippet creates a 10-step progress bar, and every 100ms one step is completed. When the bar completes we clear the interval:

此代码段创建了一个10步的进度条,每100毫秒完成一次。 当小节结束时,我们清除间隔:

const ProgressBar = require('progress')

const bar = new ProgressBar(':bar', { total: 10 })
const timer = setInterval(() => {
  bar.tick()
  if (bar.complete) {
    clearInterval(timer)
  }
}, 100)

翻译自: https://flaviocopes.com/node-output-to-cli/

使用node命令不输出结果

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值