Bird
Raised Fist0
Denoframework~15 mins

Running your first Deno program - Deep Dive

Choose your learning style10 modes available
Overview - Running your first Deno program
What is it?
Deno is a modern runtime for JavaScript and TypeScript that lets you run code outside the browser. Running your first Deno program means writing a simple script and executing it using the Deno command-line tool. This introduces you to how Deno works and how it differs from older runtimes. It is the first step to building applications with Deno.
Why it matters
Before Deno, running JavaScript outside the browser was mostly done with Node.js, which has some legacy issues and security concerns. Deno solves these by being secure by default and supporting TypeScript natively. Without Deno, developers would miss out on a simpler, safer, and more modern way to run scripts and build apps. Learning to run your first program unlocks the door to this new ecosystem.
Where it fits
You should know basic JavaScript or TypeScript syntax before starting. After learning to run your first program, you can explore Deno's built-in tools, permissions system, and how to build web servers or scripts. This topic is an early step in learning Deno and modern JavaScript runtimes.
Mental Model
Core Idea
Running your first Deno program is like turning on a new device by pressing its power button and seeing it work immediately with no setup fuss.
Think of it like...
Imagine buying a new coffee machine that comes ready to use right out of the box. You just plug it in and press start, no complicated installation or extra parts needed. Running your first Deno program is just like that simple first press to see it work.
┌───────────────┐
│ Write script  │
└──────┬────────┘
       │
       ▼
┌───────────────┐
│ Run with Deno │
└──────┬────────┘
       │
       ▼
┌───────────────┐
│ See output   │
└───────────────┘
Build-Up - 6 Steps
1
FoundationInstall Deno runtime
🤔
Concept: Learn how to get Deno on your computer so you can run programs.
To run Deno programs, you first need to install the Deno runtime. Visit deno.land and follow the installation instructions for your operating system. For example, on macOS or Linux, you can run a simple command in the terminal: curl -fsSL https://deno.land/install.sh | sh After installation, verify by running: deno --version This shows the installed Deno version.
Result
Deno is installed and ready to use on your computer.
Understanding how to install Deno is the essential first step before you can run any code with it.
2
FoundationWrite a simple Deno script
🤔
Concept: Create a basic JavaScript or TypeScript file that Deno can run.
Open a text editor and create a file named hello.ts with this content: console.log('Hello, Deno!'); This script prints a message to the console. Save the file in a folder you can access from the terminal.
Result
You have a simple script ready to run with Deno.
Writing a minimal script helps you focus on running code without distractions from complex logic.
3
IntermediateRun the script with Deno command
🤔Before reading on: do you think you need to compile the script before running it with Deno, or can you run it directly? Commit to your answer.
Concept: Learn how to execute your script using the Deno command-line tool without extra compilation steps.
Open your terminal and navigate to the folder containing hello.ts. Run the script by typing: deno run hello.ts Deno will execute the script and print: Hello, Deno! You do not need to compile TypeScript separately; Deno handles it automatically.
Result
The message 'Hello, Deno!' appears in the terminal output.
Knowing that Deno runs TypeScript directly simplifies the development process and reduces setup time.
4
IntermediateUnderstand Deno's permission system
🤔Before reading on: do you think Deno allows scripts to access files and network by default, or does it restrict them? Commit to your answer.
Concept: Deno restricts access to files, network, and environment by default for security. You must explicitly allow permissions.
Try running a script that reads a file without permissions: deno run read_file.ts You will get a permission error. To allow file access, run: deno run --allow-read read_file.ts This permission system protects your computer from untrusted code.
Result
Scripts run only with explicit permissions, preventing accidental or malicious access.
Understanding permissions helps you write safer code and avoid unexpected security issues.
5
AdvancedRun scripts with cached dependencies
🤔Before reading on: do you think Deno downloads dependencies every time you run a script, or does it cache them? Commit to your answer.
Concept: Deno caches remote dependencies on first run and reuses them later to improve performance.
If your script imports modules from URLs, Deno downloads them once and stores them locally. For example: import { serve } from 'https://deno.land/std/http/server.ts'; Run your script: deno run server.ts On first run, Deno fetches the module. On subsequent runs, it uses the cached copy unless you clear the cache.
Result
Scripts run faster after the first execution due to caching.
Knowing about caching helps you understand Deno's efficiency and how to manage dependencies.
6
ExpertUse Deno's built-in tooling with scripts
🤔Before reading on: do you think Deno requires separate tools for formatting and linting, or does it include them? Commit to your answer.
Concept: Deno includes built-in tools for formatting, linting, testing, and bundling, integrated with running scripts.
After writing your script, you can format it with: deno fmt hello.ts Lint your code with: deno lint hello.ts Run tests with: deno test This integration reduces setup and keeps your workflow simple.
Result
You maintain clean, error-free code using Deno's built-in tools without extra installations.
Understanding Deno's all-in-one tooling streamlines development and encourages best practices.
Under the Hood
Deno runs JavaScript and TypeScript code using the V8 engine, the same engine used by Chrome. It compiles TypeScript on the fly to JavaScript before execution. Deno's runtime is built in Rust and provides secure APIs for file, network, and environment access. It enforces permissions by intercepting system calls and checking allowed flags before proceeding.
Why designed this way?
Deno was designed to fix Node.js's security and complexity issues. By building on modern technologies like Rust and V8, and by integrating TypeScript support natively, Deno offers a secure, simple, and modern runtime. The permission system was added to prevent scripts from doing harmful actions without explicit consent.
┌───────────────┐
│ User Script   │
│ (TypeScript)  │
└──────┬────────┘
       │
       ▼
┌───────────────┐
│ Deno Runtime  │
│ (Rust + V8)   │
└──────┬────────┘
       │
       ▼
┌───────────────┐
│ Permission   │
│ Check Layer  │
└──────┬────────┘
       │
       ▼
┌───────────────┐
│ System Calls  │
│ (File, Net)   │
└───────────────┘
Myth Busters - 4 Common Misconceptions
Quick: Does Deno allow all scripts full access to your computer by default? Commit to yes or no.
Common Belief:Deno runs scripts with full access to files and network by default, just like Node.js.
Tap to reveal reality
Reality:Deno restricts access by default and requires explicit permission flags to allow file, network, or environment access.
Why it matters:Assuming full access can lead to security risks or confusion when scripts fail due to permission errors.
Quick: Do you think you must install separate tools to run TypeScript with Deno? Commit to yes or no.
Common Belief:You need to compile TypeScript to JavaScript manually before running it with Deno.
Tap to reveal reality
Reality:Deno runs TypeScript files directly by compiling them internally on the fly.
Why it matters:Believing this adds unnecessary complexity and setup steps, slowing down learning and development.
Quick: Does Deno download remote dependencies every time you run a script? Commit to yes or no.
Common Belief:Deno fetches remote modules from the internet every time you run a script.
Tap to reveal reality
Reality:Deno caches remote dependencies locally after the first download and reuses them on subsequent runs.
Why it matters:Misunderstanding this can cause confusion about performance and network usage.
Quick: Is Deno just a replacement for Node.js with no new features? Commit to yes or no.
Common Belief:Deno is basically Node.js with a new name and no significant improvements.
Tap to reveal reality
Reality:Deno introduces built-in TypeScript support, a secure permission system, and integrated tooling, making it a modern runtime.
Why it matters:Underestimating Deno's innovations can prevent developers from adopting better practices and tools.
Expert Zone
1
Deno's permission system is fine-grained and can be combined with environment variables to create secure, configurable scripts.
2
Deno caches compiled TypeScript files and remote modules separately, allowing efficient incremental builds and updates.
3
The runtime's Rust foundation allows Deno to offer fast startup times and low memory usage compared to older runtimes.
When NOT to use
Deno is not ideal if you rely heavily on the vast Node.js ecosystem with native addons or require legacy Node.js APIs. In such cases, Node.js remains the better choice. Also, for very large enterprise projects with existing infrastructure, migration costs may outweigh benefits.
Production Patterns
In production, Deno scripts are often bundled into single executable files for easy deployment. Permissions are locked down strictly, and scripts use Deno's built-in testing and formatting tools to maintain code quality. Remote dependencies are vendored or cached to ensure reliability.
Connections
Operating System Permissions
Deno's permission system builds on the idea of OS-level access control but applies it at the runtime level for scripts.
Understanding OS permissions helps grasp why Deno restricts script capabilities and how it enhances security.
Just-In-Time Compilation
Deno compiles TypeScript to JavaScript on the fly, similar to how JIT compilers optimize code during execution.
Knowing JIT concepts clarifies how Deno runs TypeScript efficiently without separate build steps.
Secure Software Design
Deno's design reflects principles of secure software by default, minimizing attack surfaces.
Learning about secure design in software engineering explains why Deno enforces permissions and limits by default.
Common Pitfalls
#1Trying to run a script that accesses files without permission flags.
Wrong approach:deno run read_file.ts
Correct approach:deno run --allow-read read_file.ts
Root cause:Not understanding Deno's permission system causes scripts to fail unexpectedly.
#2Assuming you must compile TypeScript manually before running with Deno.
Wrong approach:tsc hello.ts && deno run hello.js
Correct approach:deno run hello.ts
Root cause:Confusing Deno with older workflows that require separate compilation.
#3Running scripts without checking if dependencies are cached, causing slow startup.
Wrong approach:deno run https://deno.land/std/http/server.ts
Correct approach:deno cache https://deno.land/std/http/server.ts deno run server.ts
Root cause:Not leveraging Deno's caching leads to inefficient runs.
Key Takeaways
Deno is a modern runtime that runs JavaScript and TypeScript securely and simply.
Running your first Deno program involves installing Deno, writing a script, and executing it with the deno run command.
Deno enforces permissions by default, requiring explicit flags to access files, network, or environment.
Deno compiles TypeScript on the fly, so no separate build step is needed.
Built-in tools for formatting, linting, and testing make Deno a streamlined development environment.

Practice

(1/5)
1. What command do you use to run a JavaScript or TypeScript file with Deno?
easy
A. node filename.js
B. deno run filename.ts
C. npm start filename.ts
D. deno start filename.js

Solution

  1. Step 1: Understand the Deno run command

    Deno uses the command deno run followed by the file name to execute scripts.
  2. Step 2: Identify the correct command for running a file

    The correct syntax is deno run filename.ts or deno run filename.js. Other options are for Node.js or incorrect commands.
  3. Final Answer:

    deno run filename.ts -> Option B
  4. Quick Check:

    Deno runs scripts with 'deno run' [OK]
Hint: Use 'deno run' plus your file name to run scripts [OK]
Common Mistakes:
  • Using 'node' instead of 'deno run'
  • Typing 'deno start' which is invalid
  • Using 'npm start' which is for Node.js projects
2. Which of the following is the correct syntax to print 'Hello, Deno!' to the console in a Deno program?
easy
A. console.print('Hello, Deno!')
B. print('Hello, Deno!')
C. console.log('Hello, Deno!')
D. echo('Hello, Deno!')

Solution

  1. Step 1: Recall JavaScript console output method

    In JavaScript and TypeScript, the standard way to print to the console is using console.log().
  2. Step 2: Identify the correct function

    Options like print() or echo() are not valid in JavaScript or Deno. console.print() is not a function.
  3. Final Answer:

    console.log('Hello, Deno!') -> Option C
  4. Quick Check:

    Use console.log() to print text [OK]
Hint: Use console.log() to print messages in Deno [OK]
Common Mistakes:
  • Using print() which is not defined in JavaScript
  • Trying console.print() which does not exist
  • Using echo() which is from shell scripting
3. What will be the output when you run this Deno program?
console.log('Welcome to Deno!');
medium
A. Welcome to Deno!
B. console.log('Welcome to Deno!');
C. SyntaxError
D. No output

Solution

  1. Step 1: Understand console.log behavior

    The console.log() function prints the string inside the parentheses to the console.
  2. Step 2: Predict the output

    The string 'Welcome to Deno!' will be printed exactly as is, without quotes.
  3. Final Answer:

    Welcome to Deno! -> Option A
  4. Quick Check:

    console.log prints the string content [OK]
Hint: console.log prints text without quotes [OK]
Common Mistakes:
  • Expecting the code line itself as output
  • Thinking it causes a syntax error
  • Assuming no output without running
4. You wrote this Deno program:
console.log('Hello Deno!')

But when you run deno run hello.ts, you get an error. What is the most likely cause?
medium
A. You must run with 'deno start' instead
B. You need to add a semicolon at the end
C. Deno does not support console.log
D. You forgot to save the file before running

Solution

  1. Step 1: Check common causes of errors when running Deno

    If the file is not saved, Deno cannot find the latest code to run, causing errors.
  2. Step 2: Evaluate other options

    Semicolons are optional in JavaScript/TypeScript, console.log is supported, and 'deno start' is not a valid command.
  3. Final Answer:

    You forgot to save the file before running -> Option D
  4. Quick Check:

    Unsaved files cause run errors [OK]
Hint: Always save your file before running with Deno [OK]
Common Mistakes:
  • Thinking semicolons are mandatory
  • Believing console.log is unsupported
  • Using wrong Deno commands like 'deno start'
5. You want to run a Deno program named greet.ts that prints a greeting. Which command will correctly run the program if your terminal is in the same folder as the file?
medium
A. deno run ./greet.ts
B. deno start greet.ts
C. deno run ../greet.ts
D. deno rungreet.ts

Solution

  1. Step 1: Understand file path usage in Deno run

    Deno requires the file path to be correct. Using ./greet.ts explicitly points to the current directory.
  2. Step 2: Evaluate other options

    deno rungreet.ts is invalid because there's no space after run. deno run ../greet.ts points to the parent directory, which is wrong here. deno start greet.ts is invalid.
  3. Final Answer:

    deno run ./greet.ts -> Option A
  4. Quick Check:

    Use './filename' to run files in current folder [OK]
Hint: Use './filename' to run files in current directory [OK]
Common Mistakes:
  • Missing space after 'run' like 'deno rungreet.ts'
  • Using '../' which points to wrong folder
  • Trying 'deno start' which is invalid
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard