Deno CLI cheatsheet

May 14, 2020

Deno has a great CLI tool. There are around a dozen commands which you can use. Here is a quick overview of them.

Deno CLI commands:

  • deno bundle

    • This will combine JS and dependencies of a project into a single file
  • deno cache

    • This will download and compile a module (with all of its static dependencies) and save them in the local cache. No code will be executed.
  • deno completions

    • Outputs a shell completion script. Use it with something like deno completions bash > /usr/local/etc/bash_completion.d/deno.bash (then load it with source /usr/local/etc/bash_completion.d/deno.bash)
  • deno doc

    • Use deno doc to show documention for a module.
    • You can use it like deno doc ./path/to/module.ts
    • For JSON output deno doc --json ./path/to/module.ts
  • deno eval

    • Use deno eval to run Javascript.
    • Example: deno eval "console.log('hello from webdevetc.com')"
    • You can also use it for running TypeScript but it requires an additional argument of -T: deno eval -T "const v: string = 'hello'; console.log(v)"
  • deno fmt

    • Format JavaScript/TypeScript automatically.
    • To ignore formatting some code, add // deno-fmt-ignore above it.
    • To ignore an entire file, add // deno-fmt-ignore-file at the start of the file.
    • Usage example: deno fmt, deno fmt myfile1.ts myfile2.ts
    • To check for formatting without writing: deno fmt --check
    • If you have ever used Go lang, then you will be familiar with Go's fmt function. This is very similar.
  • deno help

    • Show all available commands and some debug info
  • deno info

    • Use deno info to get information about a module.
    • Can use it with urls, such as deno info https://deno.land/std/http/file_server.ts
  • deno install

    • Installs a script locally, as an executable.
    • deno install --allow-net --allow-read https://deno.land/std/http/file_server.ts
    • deno install https://deno.land/std/examples/colors.ts
    • You can use arguments such as --allow-net or --allow-read to give permissions to break out of the sandbox.
    • deno install --allow-net --allow-read -n serve https://deno.land/std/http/file_server.ts
  • deno repl

    • Use deno repl to enter the "Read-Eval-Print-Loop" mode.
    • This lets you type Javascript/TypeScript and it will be executed straight away.
    • Great for testing things out.
    • You can use the --v8-flags=<v8-flats> option for specific v8 engine flags
  • deno run

    • Use deno run with a filename or URL as an argument to run it.
    • By default, it will run in a sandbox. Use things such as --allow-read or --allow-net to give extra permissions.
    • Use -A to give all permissions.
  • deno test

    • Run tests using the built-in test runner.
    • It will run all tests declared with Deno.test() and output to standard output.
    • By default matches {*_,}test.{js,ts,jsx,tsx}
  • deno types

    • output type declaration file.
    • Use it like deno types > lib.deno.d.ts
  • deno upgrade

    • As mentioned previously, this will upgrade Deno to the latest version

The Deno Sandbox & Permissions

The Deno sandbox is one of it's greatest features. Deno's sandbox is similar to your web browser's Javascript sandbox, which prevents accessing you files on your computer.

By default, the sandbox is enabled, which means Deno is very restricted in what it can do.

The following restrictions can be removed by allowing certain actions:

  • -A or --allow-all - Allow all permissions (use with caution!)
  • --allow-env - Allow environment access
  • --allow-hrtime - Allow high-resolution time measurement
  • --allow-net - Allow network access

    • You can set specific restrictions with --allow-net=
  • --allow-plugin - Allow loading plugins
  • --allow-read - Allow file system read access

    • You can set specific restrictions, such as allowing access only to /var/log with deno --allow-read=/var/log
  • --allow-run - Allow running subprocesses
  • --allow-write - Allow file system write access

    • You can optionally specify what directories it can write to (similar to --allow-read)