Starting a project in Deno
Recently I developed a small service in Deno. It was the first time I used Deno. And before I started working on the service, I researched what tools or libraries are required to install. Luckily I needed only Deno itself and the code editor. Deno is the runtime for JavaScript, TypeScript, and WebAssembly. Deno is also a fascinating and self-sufficient development platform. Most needed tools — such as test runner, linter and formatter — are already there. In this article, I will share the details of configuring the development environment for a project in Deno.
Install Deno
This step is obvious. There’s nothing more to add to the official installation instructions.
Init Deno project
Create a project directory and run deno init
in it. The command creates two files: main.ts and main_test.ts. It is a great starting point. main.ts is an entry point to the application. The main_test.ts is an example of how to use the Deno test runner and standard assertion library.
Configure the Deno extension for VS Code
Deno has a language server that assists the development process. It analyses the code, supports autocompletion and helps prevent errors. The VS Code extension for Deno brings the language server support to the editor.
After the extension is installed, initialise the VS Code workspace. Open VS Code command palette and run Deno: Initialise Workspace Configuration. It creates a .vscode directory with the settings.json. Initially, two options are configured: deno.enable and deno.unstable.
All configurations related to the deno extension have “deno.” prefix.
When deno.enable is enabled, the VS Code extension uses the Deno language server instead of built-in JavaScript and TypeScript services.
For my project, I extended the default configuration with the formatting settings — I configured when the formatter should automatically run and what formatter the editor should use.
Sometimes it is hard to find a specific editor configuration in VS Code settings interface. Therefore I prefer updating settings.json. Following is an example of a workspace’s settings.json.
Deno also supports other code editors, but their review is out of the scope of this article. Feel free to share how you configure the editor of your choice for a Deno project in the comments.
Configure Deno (optional)
Deno supports configuration file that enables customisations of build-in linter, formatter and TypeScript compiler. Deno automatically detects configuration files deno.json or deno.jsonc in the current working directory.
Using a configuration file is not required, Deno still operates great with the default options.
When the Deno configuration file is added to a project, update VS Code workspace settings — add the deno.config setting referencing the configuration file. It allows VS Code deno extension to use formatting and linting rules provided in the configuration file.
Another great feature of Deno is the task runner. It provides a way to define and execute various commands specific to a codebase. The tasks are declared in the configuration file in the tasks section.
Since I was developing an HTTP service, I had to deal with the Deno permissions model. I had to grant access to environment variables and allow network operations explicitly. The command to launch my application was getting cumbersome and error-prone when typed manually. To solve the issue, I defined a task to launch my application.
Your project’s needs may grow beyond basic deno run
, deno test
, deno cache
commands. And that’s where the tasks can help. To invoke a task, run deno task <task name>
.
In my project, I just scratched the surface of what’s Deno capable of. And I’m excited to do more with Deno. It’s a fantastic platform that provides a great developer experience.
To speed up the process of starting a new Deno project in future, I created a proto-deno template repository. It includes:
- VS Code workspace settings
- Deno configuration
- Dependencies organised in deps.ts
- GitHub actions workflow
Deno configuration includes:
- Formatter rules
- Linter rules
- Tasks to: create and update dependencies cache; launch an application
Feel free to check it out and let me know what you think.