How to console.log without a newline in Deno?

3.5k views Asked by At

How do I print a new line to the terminal without a newline in Deno? In node.js I used to do:

process.stdout.write('hello, deno!')

Is this possible in Deno? Deno does not have the process module, and I could not find an equivalent option in https://doc.deno.land/builtin/stable.

3

There are 3 answers

6
wcarhart On BEST ANSWER

I figured it out. Deno does not have node.js's process module but it has different functionality to replicate it. I was able to print to the terminal without a new line with:

const text = new TextEncoder().encode('Hello, deno!')

// asynchronously
await Deno.writeAll(Deno.stdout, text)

// or, sychronously
Deno.writeAllSync(Deno.stdout, text)

Documentation link: https://doc.deno.land/builtin/stable#Deno.writeAll

1
17xande On
import { writeAllSync } from "https://deno.land/std/streams/mod.ts";

const text = new TextEncoder().encode('Hello')
writeAllSync(Deno.stdout, text)

Deno.writeAllSync and Deno.writeAll are deprecated, it's recommended to use the package above instead.

0
Lionel Rowe On

The easiest way to do this without using deprecated APIs or importing any modules is using Deno.stdout.write or Deno.stdout.writeSync, again, with a TextEncoder:

await Deno.stdout.write(new TextEncoder().encode(str))

Deno.stdout.writeSync(new TextEncoder().encode(str))