See also Getting started in the docs.

See also my MichaelCurrin/deno-project-template repo.

Thanks also to info from Medium post.

Install

Install a module

  • deno install -h
      deno-install
      Installs a script as an executable in the installation root's bin directory.
      deno install --allow-net --allow-read https://deno.land/std/http/file_server.ts
      deno install https://deno.land/std/examples/colors.ts
      ...
        
      -r, --reload=<CACHE_BLOCKLIST>     
              Reload source code cache (recompile TypeScript)
              --reload
                Reload everything
              --reload=https://deno.land/std
                Reload only standard modules
              --reload=https://deno.land/std/fs/utils.ts,https://deno.land/std/fmt/colors.ts
                Reloads specific modules
      ...
    
$ deno install https://deno.land/std/examples/welcome.ts

The script will be downloaded to the cache and an executable will be added as a stub as below.

  • ~/.deno/bin/welcome
      #!/bin/sh
      # generated by deno install
      deno "run" "https://deno.land/std/examples/welcome.ts" "$@"
    

It can be run directly like this:

$ ~/.deno/bin/welcome
Welcome to Deno 🦕

If the bin directory is in your PATH you can just run:

$ welcome
Welcome to Deno 🦕

Update dependencies

Remote code is fetched and cached on the first execution. It will only be updated when requested.

  • deno cache -h
      deno-cache 
      Cache and compile remote dependencies recursively.
    
      Download and compile a module with all of its static dependencies and save them
      in the local cache, without running any code:
        deno cache https://deno.land/std/http/file_server.ts
    
      Future runs of this module will trigger no downloads or compilation unless
      --reload is specified.
      ...
        
          -r, --reload=<CACHE_BLOCKLIST>    Reload source code cache (recompile TypeScript)
    

If you need to force a fresh download, can run this. This also rebuilds the standard library so can solve errors when switching Deno versions.

$ deno cache --reload src/index.ts

Upgrade Deno

$ deno upgrade

Upgrade to a target version.

$ deno upgrade --version 1.2.0

Run

Note that you can use the run command without first doing an install and all external modules will be downloaded.

Run local script

Run a script in your project. Use -A to allow all permissions.

$ deno run src/index.ts

Run HTTP module

Pass a URL to the run command. The module will be downloaded from the Deno packages the first time and on subsequent runs it will run quicker as it is already installed in bin.

$ deno run https://deno.land/std/examples/welcome.ts
Welcome to Deno 🦕

If you installed it with deno install URL or with the run command above, it will be stored and can be run directly:

$ ~/.deno/bin/welcome

Open console

Start Deno in interactive mode.

$ deno

Or

$ deno repl

Development

Bundle

Bundle a script.

$ deno bundle https://deno.land/std/examples/echo_server.ts server.bundle.js
Bundling https://deno.land/std/examples/echo_server.ts
Emitting bundle to "server.bundle.js"
2.61 KB emitted.

Run it.

$ deno run --allow-net server.bundle.js
Listening on 0.0.0.0:8080

Format

$ deno fmt src/index.ts
$ deno fmt --check PATH

Lint

$ deno lint PATH

Test

  • test.ts or src/index_test.ts or tests/index_test.ts.
      import { assertEquals } from "https://deno.land/std/testing/asserts.ts";
    
      Deno.test("deno test", () => {
          const name = "Foo";
          const surname = "Bar";
          const fullname = `${name} ${surname}`;
          assertEquals(fullname, "Foo Bar");
      });
    
$ deno test test.ts

Assertions:

  • equal
  • assert
  • assertEquals
  • assertStrictEq
  • assertStrContains
  • assertMatch
  • assertArrayContains
  • assertThrows
  • assertThrowsAsync
  • unimplemented
  • unreachable

Debugging

Browser

Allow browser dev tools debugging by using a flag.

$ deno --inspect
  • --inspect - allows attaching a debugger at any point in time.
  • --inspect-brk - will wait for debugger breakpoint and pause execution on the first line of code.

VS Code

  • .vscode/extensions.json - Suggest extensions to the user.
      {
        "recommendations": [
          "denoland.vscode-deno"
        ]
      }
    
  • .vscode/settings.json
      {
        "deno.enable": true
      }
    
  • .vscode/launch.json
      {
          "version": "0.2.0",
          "configurations": [
              {
              "name": "Deno",
              "type": "node",
              "request": "launch",
              "cwd": "${workspaceFolder}",
              "runtimeExecutable": "deno",
              "runtimeArgs": ["run", "--inspect-brk", "-A", "src/index.ts"],
              "port": 9229
              }
          ]
      }