11

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.

wcarhart
  • 2,685
  • 1
  • 23
  • 44

2 Answers2

8

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

wcarhart
  • 2,685
  • 1
  • 23
  • 44
  • Does `Deno.stdout.write(text)` work too? just reading the [api docs](https://doc.deno.land/builtin/stable#Deno.Writer), first I've heard of deno. – Matt Oct 17 '20 at 02:33
  • 1
    @Matt Unfortunately, it does not. I also tried `Deno.write()`, but per the docs _This function is one of the lowest level APIs and most users should not work with this directly, but rather use Deno.writeAll() instead. It is not guaranteed that the full buffer will be written in a single call._ (https://doc.deno.land/builtin/stable#Deno.write) – wcarhart Oct 17 '20 at 02:49
  • @Matt also, just for reference, deno is Ryan Dahl's next project after node: https://www.youtube.com/watch?v=M3BM9TB-8yA&vl=en – wcarhart Oct 17 '20 at 02:55
  • 2
    `Deno.writeAll(...)` needs `await` before it. There is also `Deno.writeAllSync(...)` for people who would prefer the synchronous version – joe Aug 25 '21 at 17:01
  • 1
    `Deno.writeAllSync` and `Deno.WriteAll` are now deprecated. Use the alternatives in https://deno.land/std/streams/conversion.ts instead – 17xande Apr 28 '22 at 09:25
  • The proper import URL nowadays is https://deno.land/std@v0.190.0/streams/mod.ts (which should work indefinitely because it's pinned to a specific version) – Bruno Bernardino Jul 11 '23 at 09:04
4
import { writeAllSync } from "https://deno.land/std/streams/conversion.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.

17xande
  • 2,430
  • 1
  • 24
  • 33
  • Nowadays the structure changed a bit. The above would've worked if it had been pinned to a specific version. Today the import line looks like: `import { writeAllSync } from 'https://deno.land/std@v0.190.0/streams/mod.ts';` Everything else is still the same. – Bruno Bernardino Jul 11 '23 at 09:03