0
0
DenoHow-ToBeginner ยท 3 min read

How to Use Deno Task: Simple Guide with Examples

Use deno task to run scripts defined in your deno.json or deno.jsonc file under the tasks section. Define commands as key-value pairs, then run them with deno task taskName in your terminal.
๐Ÿ“

Syntax

The deno task command runs scripts defined in the tasks section of your deno.json or deno.jsonc file. Each task is a key with a string or array of strings as the command to execute.

Example parts:

  • tasks: The section where you list your scripts.
  • taskName: The name you use to run the script.
  • command: The shell command or Deno command to run.
json
{
  "tasks": {
    "taskName": "command to run"
  }
}
๐Ÿ’ป

Example

This example shows how to define a task to run a simple Deno script that prints a message. You run it with deno task greet.

json and typescript
{
  "tasks": {
    "greet": "deno run greet.ts"
  }
}

// greet.ts
console.log("Hello from Deno Task!");
Output
Hello from Deno Task!
โš ๏ธ

Common Pitfalls

Common mistakes include:

  • Not having a deno.json or deno.jsonc file in your project root.
  • Using invalid JSON syntax in the config file.
  • Trying to run a task name that does not exist.
  • Forgetting to install dependencies or permissions needed by the task.

Always check your JSON syntax and task names carefully.

json
/* Wrong: Missing tasks section or typo in task name */
{
  "task": {
    "start": "deno run app.ts"
  }
}

/* Right: Correct tasks section and task name */
{
  "tasks": {
    "start": "deno run app.ts"
  }
}
๐Ÿ“Š

Quick Reference

CommandDescription
deno taskLists all available tasks from deno.json
deno task Runs the specified task
deno task --helpShows help for the task command
deno task --quietRuns task with minimal output
โœ…

Key Takeaways

Define your scripts under the tasks section in deno.json or deno.jsonc.
Run tasks with the command deno task taskName in your terminal.
Ensure your JSON config is valid and located in the project root.
Use deno task to simplify running common commands and scripts.
Check task names carefully to avoid errors when running tasks.