Deno Quick Reference ↩
This document gathers Deno hints and tips. |
Covered topics include: Deno versus Node.js, Deno commands, Deno runtime, Windows specific fixes.
-
CommonJS versus ECMAScript modules
Deno supports only the ECMAScript modules (short ES modules or ESM) while Node.js supports both formats for the CommonJS and ECMAScript modules.
const http = require('http'); /* CommonJS */ import { serve } from "https://deno.land/std/http/server.ts"; /* ECMAScript */
🔎 Deno supports the
@deno-types
compiler hint to specify a type definition file when type checking instead of the imported JavaScript file (see "Providing types when importing"). -
Runtime versus Standard library
Unlike Node.js Deno has a lightweight built-in runtime of around 130 methods, functions and classes. See section Deno Runtime for more details.
Deno Commands ▲
deno cache
makes sure we have a local copy of our code's dependencies without having to run it
Note: It makes think of commandnpm install
on Node.js.- With the
--reload
flagdeno cache
forces the dependencies to be downloaded. A comma-separated list of modules that must be reloaded can be sent as a parameter to the--reload
flag.
The following command displays the documentation for method serve
of the standard library's HTTP module.
> deno doc https://deno.land/std@0.208.0/http/server.ts serve Defined in https://deno.land/std@0.208.0/http/server.ts:572:0 function serve(addr: string | HTTPOptions): Server Serves HTTP requests with the given handler. You can specify an object with a port and hostname option, which is the address to listen on. The default is port 8000 on hostname "0.0.0.0". The below example serves with the port 8000. [...]
The following command displays the documentation for method writeFile
of the Deno built-in API:
> deno doc --builtin Deno.writeFile Defined in lib.deno.d.ts:2048:2 function writeFile(path: string | URL, data: Uint8Array, options?: WriteFileOptions): Promise Write `data` to the given `path`, by default creating a new file if needed, else overwriting. ```ts const encoder = new TextEncoder(); const data = encoder.encode("Hello world\n"); await Deno.writeFile("hello1.txt", data); // overwrite "hello1.txt" or create it await Deno.writeFile("hello2.txt", data, {create: false}); // only works if "hello2.txt" exists await Deno.writeFile("hello3.txt", data, {mode: 0o777}); // set permissions on new file await Deno.writeFile("hello4.txt", data, {append: true}); // add data to the end of the file ``` Requires `allow-write` permission, and `allow-read` if `options.create` is `false`.
Deno Runtime ▴
Two types of functions are available on Deno without any imports: Web APIs and the Deno built-in API. Code written using Web APIs can be bundled and run in the browser with no transformations.
-
WebAssembly
namespaceInterfaces in the
WebAssembly
namespace are:> set NO_COLOR=true & deno doc --builtin WebAssembly |findstr /r /c:"^[ ][ ]interface" interface GlobalDescriptor interface MemoryDescriptor interface ModuleExportDescriptor interface ModuleImportDescriptor interface TableDescriptor interface WebAssemblyInstantiatedSource
Functions in the
WebAsssembly
namepspace are:> set NO_COLOR=true & deno doc --builtin WebAssembly |findstr /r /c:"^[ ][ ]function" function compile(bytes: BufferSource): Promise function compileStreaming(source: Response | Promise): Promise function instantiate(bytes: BufferSource, importObject?: Imports): Promise function instantiate(moduleObject: Module, importObject?: Imports): Promise function instantiateStreaming(response: Response | PromiseLike, importObject?: Imports): Promise function validate(bytes: BufferSource): boolean
🔎 The above functions are defined in the MDN Web Docs.
-
Deno
namespaceInterfaces in the
Deno
namespace are:> set NO_COLOR=true & deno doc --builtin Deno |findstr /r /c:"^[ ][ ]interface" interface MemoryUsage interface TestDefinition interface Reader interface ReaderSync interface Writer interface WriterSync interface Closer interface Seeker interface SeekerSync interface OpenOptions interface ReadFileOptions interface MkdirOptions interface MakeTempOptions interface RemoveOptions interface FileInfo interface DirEntry interface WriteFileOptions interface Metrics interface FsEvent interface FsWatcher extends AsyncIterable interface RunOptions [...]
read
functions in theDeno
namespace are:> set NO_COLOR=true & deno doc --builtin Deno |findstr /r /c:"^[ ][ ]function read" function readSync(rid: number, buffer: Uint8Array): number | null function read(rid: number, buffer: Uint8Array): Promise<number | null> function readAll(r: Reader): Promise<Uint8Array> function readAllSync(r: ReaderSync): Uint8Array function readTextFileSync(path: string | URL): string function readTextFile(path: string | URL, options?: ReadFileOptions): Promise function readFileSync(path: string | URL): Uint8Array function readFile(path: string | URL, options?: ReadFileOptions): Promise function readDirSync(path: string | URL): Iterable function readDir(path: string | URL): AsyncIterable function readLinkSync(path: string | URL): string function readLink(path: string | URL): Promise
🔎 We can evaluate the code example presented in the
readTextFile
documentation:> deno doc --builtin Deno.readTextFile Defined in lib.deno.d.ts:1725:2 function readFile(path: string | URL, options?: ReadFileOptions): Promise Reads and resolves to the entire contents of a file as an array of bytes. `TextDecoder` can be used to transform the bytes to string if required. Reading a directory returns an empty data array. ```ts const data = await Deno.readTextFile("hello.txt"); console.log(data); ``` Requires `allow-read` permission.
For instance:
> deno eval "const data = await Deno.readTextFile('.gitignore'); console.log(data);" .idea/ .project .settings/ .vscode/ *_LOCAL/ out/ target/
1.14.2 (2021.09.28)
- fix: subprocess kill support on windows (#12134).