📖 Scenario: You are building a small Deno script that uses a utility function from an online source. This is common when you want to reuse code without copying it into your project.
🎯 Goal: Learn how to import a function directly from a URL and use it in your Deno script.
📋 What You'll Learn
Create a variable that imports a function from a URL
Create a local variable to hold a number
Use the imported function on the local variable
Print the result using console.log
💡 Why This Matters
🌍 Real World
Developers often reuse code by importing libraries or utilities from URLs, especially in Deno where this is a common pattern.
💼 Career
Knowing how to import code from URLs helps you quickly use shared code and libraries without copying files, a useful skill in modern JavaScript and TypeScript development.
Progress0 / 4 steps
1
Import the double function from the URL
Write an import statement to import the function double from the URL https://deno.land/std/examples/double.ts.
Deno
Hint
Use the import keyword followed by curly braces and the function name, then from and the URL in quotes.
2
Create a variable num with value 5
Create a variable called num and set it to the number 5.
Deno
Hint
Use const num = 5; to create the variable.
3
Use the double function on num
Create a variable called result and set it to the value of calling double(num).
Deno
Hint
Use const result = double(num); to call the function and store the result.
4
Print the result using console.log
Write a line to print the variable result using console.log(result).
Deno
Hint
Use console.log(result); to show the output in the console.
Practice
(1/5)
1. What is the main benefit of importing modules from URLs in Deno?
easy
A. You can use code directly from the web without installing it locally.
B. It makes your code run faster than local imports.
C. It automatically updates your code to the latest version.
D. It disables caching to always fetch fresh code.
Solution
Step 1: Understand URL imports in Deno
Deno allows importing modules directly from web URLs, so you don't need to install packages locally.
Step 2: Identify the main benefit
This feature simplifies using external code by fetching it on demand without manual installs.
Final Answer:
You can use code directly from the web without installing it locally. -> Option A
Quick Check:
Importing from URLs = No local install needed [OK]
Hint: Remember: URL imports mean no local installs needed [OK]
Common Mistakes:
Thinking URL imports always update to latest code
Believing URL imports disable caching
Assuming URL imports make code run faster
2. Which of the following is the correct syntax to import a module from a URL in Deno?
easy
A. import { serve } from "https://deno.land/std/http/server.ts";
B. require("https://deno.land/std/http/server.ts");
C. import serve from "./server.ts";
D. load { serve } from "https://deno.land/std/http/server.ts";
Solution
Step 1: Recall Deno import syntax
Deno uses ES module syntax with import { ... } from "url"; to import from URLs.
Step 2: Check each option
import { serve } from "https://deno.land/std/http/server.ts"; uses correct ES module import syntax with a URL. require("https://deno.land/std/http/server.ts"); uses Node.js style require, which Deno does not use. import serve from "./server.ts"; imports locally, not from URL. load { serve } from "https://deno.land/std/http/server.ts"; uses invalid keyword load.
Final Answer:
import { serve } from "https://deno.land/std/http/server.ts"; -> Option A
Hint: Deno uses ES import syntax, not require or load [OK]
Common Mistakes:
Using require() instead of import
Trying to import local files with URL syntax
Using invalid keywords like load
3. What will happen when you run this Deno code snippet?
import { serve } from "https://deno.land/std/http/server.ts";
console.log(typeof serve);
medium
A. "object"
B. "undefined"
C. Runtime error: Module not found
D. "function"
Solution
Step 1: Understand what is imported
The serve function is exported from the URL module as a function.
Step 2: Check the typeof operator on serve
Since serve is a function, typeof serve returns the string "function".
Final Answer:
"function" -> Option D
Quick Check:
typeof serve = "function" [OK]
Hint: Imported functions have typeof 'function' in JS/TS [OK]
Common Mistakes:
Expecting 'undefined' if module loads correctly
Thinking import causes runtime error
Confusing function with object type
4. Identify the error in this Deno import statement:
import { serve } from "https://deno.land/std/http/server";
medium
A. URL must start with http:// not https://.
B. Using curly braces {} around serve is incorrect.
C. Missing file extension ".ts" in the URL.
D. Cannot import from URLs in Deno.
Solution
Step 1: Check URL correctness
Deno requires full file paths including extensions for URL imports, so missing ".ts" causes error.
Step 2: Validate other parts
Curly braces are correct for named imports. HTTPS is supported. Deno supports URL imports.
Final Answer:
Missing file extension ".ts" in the URL. -> Option C
Quick Check:
File extension required in URL imports [OK]
Hint: Always include file extensions in Deno URL imports [OK]
Common Mistakes:
Omitting file extensions in import URLs
Thinking HTTPS is unsupported
Misunderstanding import syntax with braces
5. You want to ensure your Deno project always uses the same version of a library imported from a URL. Which practice helps achieve this?
hard
A. Disable Deno's cache to force fresh downloads every run.
B. Use a versioned URL like "https://deno.land/std@0.203.0/http/server.ts".
C. Manually download and import local copies only.
D. Import from the base URL without version to get latest updates.
Solution
Step 1: Understand versioning in URL imports
Using versioned URLs locks the imported code to a specific release, ensuring stability.
Step 2: Evaluate options
Use a versioned URL like "https://deno.land/std@0.203.0/http/server.ts". uses a versioned URL, which is best practice. Import from the base URL without version to get latest updates. fetches latest, risking breaking changes. Manually download and import local copies only. is manual and not URL import. Disable Deno's cache to force fresh downloads every run. disables caching, causing inefficiency.
Final Answer:
Use a versioned URL like "https://deno.land/std@0.203.0/http/server.ts". -> Option B
Quick Check:
Versioned URLs = stable imports [OK]
Hint: Lock versions in URL to keep imports stable [OK]
Common Mistakes:
Using unversioned URLs expecting stability
Disabling cache wastes time and bandwidth
Confusing manual local imports with URL imports
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.