Running tasks
Tasks are a way to avoid rewriting long commands in the terminal. They are very convenient and widely used.
Make a new folder somewhere on your computer, open it with VS Code, and create two files:
main.ts
, where we will write our code.deno.json
, which lets VS Code know this is a Deno project and enables the Deno extension. You can tweak settings in this file, but we’ll keep it empty for now.
Copy and paste the following code into main.ts
. This script comes from the Fetching data lesson. It fetches a JSON file from GitHub, parses it, and logs it.
const response = await fetch(
"https://raw.githubusercontent.com/nshiab/data-fetch-lesson/refs/heads/main/temp.json",
);
const data = await response.json();
console.log(data);
Now, to run and watch this file, we previously used deno run --watch --check -A main.ts
, but it’s a bit annoying to write, and remembering all these options is tiring. The more complex your projects get, the longer these terminal commands can become.
So, let’s create a main
task instead.
Open your deno.json
and add this. Don’t forget to save! 🙃
{
"tasks": {
"main": "deno run --watch --check -A main.ts"
}
}
Now, you can run deno task main
. Deno will look inside your deno.json
to find a task named main
and execute the relevant command.
This is much easier to type! And if you want to add more options or update the task, you’ll still be able to run it with deno task main
.
You can have as many tasks as you want, and you can name them however you like. In other popular runtimes, like Node.js or Bun, tasks
are called scripts
and are stored in package.json
instead of deno.json
. But Deno is compatible with them and will find them anyway.
That’s it! Happy coding! 🥳