TechLearn Hub

Simple Technology Learning for Everyone

What Is Deno? A Beginner's Guide

Deno is a modern runtime for JavaScript, TypeScript and WebAssembly. It can be used to build web servers, APIs, scripts, command-line tools and other applications.

Deno provides several development tools as part of its ecosystem, including tools for testing, formatting, linting and managing project tasks.

Simple definition: Deno is a JavaScript and TypeScript runtime with a permission-based security model and built-in developer tools.

1. What Does Deno Mean?

Deno is pronounced "dee-no". It was created by Ryan Dahl, who also created Node.js, as a modern approach to running JavaScript and TypeScript outside a web browser.

Deno supports modern web APIs and provides tools intended to reduce the configuration needed for many projects.

2. Why Was Deno Created?

JavaScript development can involve separate tools for package management, testing, formatting and linting. Deno was designed to bring many of these capabilities together while providing more restrictive default permissions for sensitive operations.

3. Main Features of Deno

4. Deno and TypeScript

Deno can run TypeScript files directly without requiring you to separately install the TypeScript compiler for basic execution.

function greet(name: string): string {
  return `Hello, ${name}!`;
}

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

Save this example as main.ts and run it with Deno.

5. Installing Deno

Deno supports several operating systems. Follow the official installation instructions for your operating system at Deno's installation guide.

After installation, check the installed version:

deno --version

6. Your First Deno Program

Create a file named main.ts containing:

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

Run the program:

deno run main.ts

You can also run a script using deno main.ts.

7. Deno Permissions

Deno restricts access to sensitive resources by default. Depending on what a program needs to do, you may have to grant specific permissions.

Allow network access:

deno run --allow-net main.ts

Allow file reading:

deno run --allow-read main.ts

Allow environment-variable access:

deno run --allow-env main.ts

Other permissions include --allow-write for writing files and --allow-run for running subprocesses.

Important: Grant only the permissions your program needs. Broad permissions such as -A should be used carefully, especially when running code from sources you do not trust.

8. Creating a Deno Project

Deno provides an initializer for creating a starter project:

deno init my_project

A simple project may contain files like these:

my_project/
├── deno.json
├── main.ts
└── main_test.ts

The exact generated files may vary by Deno version.

9. What Is deno.json?

The deno.json file stores configuration for many Deno projects. It can define tasks, imports, compiler options and other settings.

{
  "tasks": {
    "start": "deno run --allow-net main.ts",
    "test": "deno test"
  }
}

Run the start task:

deno task start

Run the tests:

deno task test

10. Installing and Using Packages

Deno supports packages from different ecosystems, including npm and JSR. Many dependencies can be referenced directly in import statements.

For example, a JSR import can look like this:

import { assertEquals } from "jsr:@std/assert";

For packages that provide command-line executables, Deno also provides commands such as deno install and deno x. Their exact usage depends on the package and the Deno version.

11. Using NPM Packages

Deno supports many npm packages through the npm: specifier.

import express from "npm:express";

Package compatibility can depend on the package's dependencies and assumptions about the Node.js environment. Check the package documentation before using it in a production application.

12. What Is JSR?

JSR is a package registry designed for modern JavaScript and TypeScript packages.

import { assertEquals } from "jsr:@std/assert";

assertEquals(2 + 3, 5);

This example imports an assertion function that can be used in tests.

13. Deno Test

Deno includes a test runner that can run tests written in JavaScript or TypeScript.

import { assertEquals } from "jsr:@std/assert";

Deno.test("addition works", () => {
  assertEquals(2 + 3, 5);
});

Run the test with:

deno test

14. Deno Formatter

The formatter helps keep code consistently formatted.

deno fmt

You can use it during development to make source files easier to read and maintain.

15. Deno Linter

The built-in linter can identify certain coding issues and patterns that may need attention.

deno lint

16. Type Checking

Use deno check to check TypeScript types and related module information.

deno check main.ts

17. Building an HTTP Server

Deno provides Deno.serve() for creating HTTP servers.

Deno.serve(() => {
  return new Response("Hello from my Deno server!");
});

Save the example as server.ts, then run it with network permission:

deno run --allow-net server.ts

18. Creating API Routes

You can inspect the request URL and return a different response for each route.

Deno.serve((request) => {
  const url = new URL(request.url);

  if (url.pathname === "/api/hello") {
    return Response.json({
      message: "Hello from Deno API"
    });
  }

  return new Response("Not Found", {
    status: 404
  });
});

19. Returning JSON

The Response.json() method can return JSON data to an API client.

Deno.serve(() => {
  return Response.json({
    name: "TechLearn Hub",
    status: "online"
  });
});

20. Reading Environment Variables

Environment variables can hold configuration values. Sensitive values such as API keys should be kept out of public source code.

const apiKey = Deno.env.get("API_KEY");

console.log(apiKey);

Allow environment access when running the file:

deno run --allow-env main.ts

21. Reading Files

Deno provides APIs for reading file contents.

const text = await Deno.readTextFile("data.txt");

console.log(text);

Run the program with the appropriate read permission:

deno run --allow-read main.ts

22. Writing Files

await Deno.writeTextFile(
  "message.txt",
  "Hello from Deno"
);

Allow write access when running the program:

deno run --allow-write main.ts

23. Deno and Web Standards

Deno uses familiar web APIs such as Request, Response, URL, fetch and Web Streams. This can help developers transfer knowledge from browser development to server-side applications.

24. Deno and React

Deno can be used in projects involving React and other modern frontend technologies. The setup depends on the framework, build tools and project structure. Deno can also work with npm packages used by many frontend projects.

25. Deno and Node.js

Deno and Node.js both run JavaScript outside a browser, but their default tools and design choices differ.

FeatureDenoNode.js
JavaScriptYesYes
TypeScriptBuilt-in runtime supportSupport depends on version and setup
PermissionsPermission flags restrict many sensitive operationsUses traditional process permissions by default
PackagesSupports npm and JSRLarge npm ecosystem
Formatter and linterBuilt-in toolsCommonly configured separately
TestingBuilt-in test runnerBuilt-in test runner available

26. Deno vs Bun

FeatureDenoBun
JavaScript runtimeYesYes
TypeScriptSupported directlySupported directly
Package ecosystemnpm and JSR supportStrong npm compatibility
TestingBuilt-in test runnerBuilt-in test runner
Security approachPermission-based access controlsDifferent default security model

Both tools offer modern JavaScript development workflows, but they differ in architecture, compatibility and tooling.

27. Deno vs Vite

Deno and Vite serve different primary purposes:

28. Deno vs Webpack

Webpack is primarily a module bundler, while Deno is a runtime and development platform. They are not direct replacements for one another, and a project may use different tools for different tasks.

29. Running Tasks

Define useful commands in deno.json:

{
  "tasks": {
    "dev": "deno run --allow-net server.ts",
    "test": "deno test",
    "format": "deno fmt",
    "lint": "deno lint"
  }
}

Run the development task with:

deno task dev

30. Running Package Commands

The deno x command can execute supported package binaries. For example, it can be used to run a Vite project generator from npm:

deno x npm:create-vite my-app

Check the official documentation for the latest command options and package-specific requirements.

31. Watch Mode

Watch mode can help during development by rerunning or restarting supported commands when files change.

deno serve --watch server.ts

Check the command's help for options supported by your installed Deno version.

32. Running a Server

Deno provides the deno serve command for serving supported HTTP applications.

deno serve server.ts

To specify a port, use an option supported by your installed version, such as:

deno serve --port=3000 server.ts

33. Compiling a Deno Application

Deno can create standalone executables from supported applications.

deno compile main.ts

Compilation options depend on the target platform and any permissions or resources required by the application.

34. Example Project Structure

my-deno-app/
├── deno.json
├── main.ts
├── routes/
│   ├── home.ts
│   └── api.ts
├── services/
│   └── user.ts
├── tests/
│   └── app_test.ts
└── README.md

35. Example Deno API Project

function router(request: Request): Response {
  const url = new URL(request.url);

  if (url.pathname === "/") {
    return new Response("TechLearn API");
  }

  if (url.pathname === "/api/status") {
    return Response.json({
      status: "online"
    });
  }

  return new Response("Not Found", {
    status: 404
  });
}

Deno.serve(router);

Save the code in server.ts and run it with:

deno run --allow-net server.ts

36. Testing a Function

import { assertEquals } from "jsr:@std/assert";

function add(a: number, b: number): number {
  return a + b;
}

Deno.test("add function", () => {
  assertEquals(add(2, 4), 6);
});

Run the test:

deno test

37. Deno Security Best Practices

Beginner tip: If an application only needs to read files from one directory, restrict its access to that directory instead of granting access to the entire filesystem.

38. Common Deno Mistakes

Mistake 1: Forgetting Permissions

A program that needs network access may require --allow-net.

deno run --allow-net server.ts

Mistake 2: Giving Too Many Permissions

Avoid broad permissions when narrower permissions are sufficient.

Mistake 3: Confusing Deno With a Bundler

Deno is a runtime and toolchain, not simply a module bundler.

Mistake 4: Ignoring Type Checking

Use deno check main.ts to check a TypeScript entry point.

Mistake 5: Hard-Coding Secrets

Do not place private keys, passwords or API secrets in publicly accessible source code.

39. Useful Deno Commands

deno --version
deno init
deno run main.ts
deno check main.ts
deno test
deno fmt
deno lint
deno task start
deno serve server.ts
deno compile main.ts

40. Deno Command Cheat Sheet

CommandPurpose
deno initCreate a starter project
deno runRun JavaScript or TypeScript
deno checkCheck types and modules
deno testRun tests
deno fmtFormat code
deno lintCheck code for common issues
deno taskRun project tasks
deno serveServe a supported HTTP application
deno compileCreate a standalone executable
deno xExecute a package binary

41. Advantages of Deno

42. Things to Consider

43. When Should You Use Deno?

Deno can be useful for building:

44. A Simple Learning Roadmap

  1. Learn JavaScript basics.
  2. Learn TypeScript basics.
  3. Install Deno.
  4. Run your first TypeScript program.
  5. Learn about permissions.
  6. Explore deno.json.
  7. Learn packages and imports.
  8. Write and run tests.
  9. Create an HTTP server.
  10. Build a small API.
  11. Learn deployment and production security.
  12. Build a complete project.

45. Frequently Asked Questions

Is Deno a programming language?

No. Deno is a runtime and development platform for JavaScript, TypeScript and WebAssembly.

Can Deno run JavaScript?

Yes. Deno can run JavaScript directly.

Can Deno run TypeScript?

Yes. Deno supports TypeScript directly.

Is Deno secure?

Deno uses a permission-based security model that restricts many sensitive operations by default. Security still depends on how the application and its dependencies are written and configured.

Can Deno use npm packages?

Yes. Deno supports many npm packages through the npm: specifier, although compatibility can vary.

Does Deno have a test runner?

Yes. Deno includes a built-in test runner.

Does Deno have a formatter?

Yes. The deno fmt command formats supported files.

Does Deno have a linter?

Yes. Use deno lint to check supported source files.

Can Deno create APIs?

Yes. Deno.serve() can be used to create HTTP servers and APIs.

Can Deno replace Node.js?

Deno can run many projects and supports npm packages, but whether it can replace Node.js depends on a project's dependencies and requirements.

46. Conclusion

Deno is a modern runtime for JavaScript, TypeScript and WebAssembly. Its permission system, web API support and built-in development tools make it useful for many scripts, servers and backend applications.

A good way to begin is to run a simple TypeScript program, learn the permission system, write a test and then build a small HTTP API.

Next step: Explore JavaScript, TypeScript, Node.js, Bun, Vite and React guides on TechLearn Hub to expand your web development skills.

Official resource: Deno Documentation