The Deno CLI is a batteries included executable with everything you need to develop, lint, test, and run Deno code.

CLI commands in Deno version 1.4:

  • deno bundle
  • deno cache
  • deno completions
  • deno doc
  • deno eval
  • deno fmt
  • deno help
  • deno info
  • deno install
  • deno lint
  • deno repl
  • deno run
  • deno test
  • deno types
  • deno upgrade

In this article, we’ll briefly discuss each command.

Tip: You can use the --help option to get information about each command, for example deno run --help.

Common CLI options in Deno version 1.4 supported by all commands:

  • --help: Display usage and example information about one or all commands.
  • --log-level <debug|info>: Filter output based on log level.
  • --quiet: Disable most intermediary output.
  • --unstable: Enable experimental Deno APIs, commands, and options.
  • --version: Display the versions of Deno, V8, and TypeScript included in the deno executable.

Other than these, some commands support permission options such as --allow-net. We will not discuss these in this article.

Bundle JavaScript module

The deno bundle command is used to bundle a module and its dependency tree into a single JavaScript module.

Usage

deno bundle [options] <source-module> [output-file] where additional options are:

  • --cert <file>: Use specified HTTPS certificate for resolving remote modules.
  • --config <file>: Use specified TypeScript configuration (tsconfig.json ) to compile the module and its dependencies.
  • --importmap <file>: Use specified import map for module resolution. See Deno manual 4.4. Import maps. Experimental feature.
  • --reload[=<module-refresh-allowlist>]: Download and recompile all or the specified remote modules when bundling.

Example

Let's compile and bundle a Hello World HTTP server using an import map that references the standard http library.

We have the following workspace structure, files, and folders:
 

deno_hello_world
├── dist
├── src
│   └── hello_world.ts
├── import_map.json
└── tsconfig.json

Workspace structure.
 

{
  "//": "import_map.json",
  "imports": {
    "http/": "https://deno.land/std/http/"
  }
}

Import map.
 

{
  "//": "tsconfig.json",
  "compilerOptions": {
    "noImplicitReturns": true,
    "noFallthroughCasesInSwitch": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "strict": true
  }
}

TypeScript configuration.
 

// hello_world.ts
import { serve } from 'http/server.ts';

const server = serve(':8000');
const body = new TextEncoder().encode('Hello, World!\n');

async function main() {
  for await (const request of server) {
    request.respond({ body });
  }
}

main();

 

Hello World HTTP server.

With the files above, use this command to bundle the Hello World HTTP server into a single module:
 

deno bundle --unstable --importmap import_map.json --config tsconfig.json src/hello_world.ts dist/hello_world.js
Bundle file:///C:/projects/sandbox/deno-cli-app/src/hello_world.ts
Emit "dist/hello_world.js" (68.99KB)

 

Bundle the Hello World HTTP server.

We're now left with a single JavaScript bundle that uses a Map to keep track of sub-modules and dependencies.

When using this command, be careful when using features that are not supported by V8, such as top-level await. However, top-level await works with the V8 flag --harmony-top-level-await.

Cache remote Deno modules

For any module, we can cache (download) the module and every dependency if they're remote modules.

Usage

deno cache [options] <source-module> where additional options are:

  • --cert <file>: Use specified HTTPS certificate for resolving remote modules.
  • --config <file>: Use specified TypeScript configuration (tsconfig.json ) to compile the module and its dependencies.
  • --importmap <file>: Use specified import map for module resolution. See Deno manual 4.4. Import maps. Experimental feature.
  • --lock <file>: Consult module hashes in the specified lockfile for caching purposes.
  • --lock-write: Create/update the specified lockfile. Specify the lockfile with the --lock option.
  • --no-remote: Disallow remote modules.
  • --no-check: Skip type checking.
  • --reload=[module-whitelist-patterns]: (Re-)download remote modules. Option to specify one or more patterns for which remote modules to allow.

Example

We have a Hello World HTTP server. We want to cache its remote dependency and lock their hashes to prevent unintentional updates.

# Initial run to produce lockfile
deno cache src/hello_world.ts --lock deno-lock.json --lock-write --reload

# Later run by you or your colleagues
deno cache src/hello_world.ts --lock deno-lock.json

#deno #nodejs #javascript

A quick overview of the Deno CLI
25.60 GEEK