Skip to main content

Building scripts and CLIs with Deno


Why Deno?

Writing scripts in Deno is fast, easy, and productive. There’s no separate step to install dependencies, configure TypeScript, setup a linter, formatter, test runner, or any other part of your toolchain.

For example, you can write TypeScript, install a module and its type definitions right when you run the script for the first time:

import cowsay from "npm:cowsay@1.5.0";

let output: string = cowsay.say({ text: "Hello from typescript!" });

console.log(output);

And when we run it the first time, it installs all dependencies and caches them:

$ deno run --allow-read main.ts

 ________________________
< Hello from typescript! >
 ------------------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

Web Platform APIs

Scripting with Deno is easier with its built-in Web Platform APIs. This makes accessing the network, parsing HTTP responses, and more simple and intuitive.

const res = await fetch("https://examples.deno.land/hello-world.ts");
const text = await res.text();
console.log(text);

And running this script we see the body of the script:

$ deno run --allow-net main.ts
console.log("Hello, World!")
What about Node?
Node.js does not use web platform APIs so you would need to import `node:http` or third party dependencies in order to access the network and make HTTP requests. Learn more about how this leads to JavaScript bloat, dependency headaches, and more.

Sharing and distributing

Deno can execute programs and scripts via URL, so sharing your script is as simple as hosting your script on gist.github.com. An example of a script is https://examples.deno.land/hello-world.ts, which you can run from a URL:

$ deno run https://examples.deno.land/hello-world.ts
Hello, World!
What about Node?
In Node.js, sharing a simple JavaScript script requires a dependency manifest in the form of a `package.json` file. Deno is built like a browser — it can run any script simply from a URL. Learn about how Node’s divergence from browser and web standards leads to unnecessary complexity.

If you want to share your JavaScript or TypeScript program as a binary, Deno’s all-in-one modern toolchain includes deno compile, which makes it easy to compile your script into a single, portable binary that you can share on all major platforms.

$ deno compile --allow-net ./main.ts
Compile file:///deno/main to main

$ ./main
console.log("Hello, World!")

Additional Resources

Here are some examples, blog posts, and videos about writing scripts or CLIs with Deno.