How to Set Up Deno for Modern JavaScript and TypeScript Projects

In recent years, Deno has gained popularity as a modern runtime for JavaScript and TypeScript. It offers a secure, streamlined environment that simplifies development workflows. This guide provides step-by-step instructions on how to set up Deno for your projects, enabling you to write clean, efficient, and modern code.

Installing Deno

The first step is installing Deno on your system. Deno supports multiple platforms, including Windows, macOS, and Linux. You can install Deno using the following methods:

  • Using the Shell (Unix-based systems): Run curl -fsSL https://deno.land/x/install/install.sh | sh
  • Using Homebrew (macOS): Run brew install deno
  • Using Chocolatey (Windows): Run choco install deno
  • Using Scoop (Windows): Run scoop install deno

After installation, verify by running deno --version in your terminal or command prompt.

Creating Your First Deno Project

Set up a new directory for your project and create an entry file, such as main.ts. Deno uses TypeScript by default, but you can also write JavaScript.

Example main.ts:

console.log("Hello, Deno!");

Run your project with the command:

deno run main.ts

Managing Permissions

Deno emphasizes security by requiring explicit permissions for network, file system, and environment access. Use flags to grant permissions:

  • Network access: --allow-net
  • File system access: --allow-read and --allow-write
  • Environment variables: --allow-env

Example:

deno run --allow-net --allow-read main.ts

Using External Modules

Just like Node.js, Deno can import modules directly from URLs. For example, to use the popular Oak web framework:

import { Application } from "https://deno.land/x/oak/mod.ts";

const app = new Application();

app.use((ctx) => {
  ctx.response.body = "Hello from Oak!";
});

await app.listen({ port: 8000 });

Run your server with:

deno run --allow-net server.ts

Using TypeScript Features

Deno supports modern TypeScript features out of the box, including decorators, async/await, and type annotations. You can organize your code into modules and use import/export syntax seamlessly.

Example of a simple module:

// greetings.ts
export function greet(name: string): string {
  return `Hello, ${name}!`;
}

And import it in your main file:

import { greet } from "./greetings.ts";

console.log(greet("World"));

Conclusion

Setting up Deno for modern JavaScript and TypeScript projects is straightforward and offers a secure, efficient environment for development. With native TypeScript support, URL-based module imports, and built-in tooling, Deno is an excellent choice for contemporary web development.