📖 Scenario: You are building a small Deno project that uses multiple external libraries. To keep your import statements clean and easy to manage, you want to use an import map.Import maps let you create short aliases for long URLs, so your code looks neat and is easier to update.
🎯 Goal: Create a Deno project that uses an import map to simplify imports of external modules. You will define the import map, configure Deno to use it, and write a simple script that imports and uses the aliased modules.
📋 What You'll Learn
Create an import map JSON file with aliases for two external modules
Configure Deno to use the import map when running the script
Write a Deno script that imports modules using the aliases from the import map
Use the imported modules in a simple way to demonstrate the imports work
💡 Why This Matters
🌍 Real World
Import maps help keep your code clean and manageable when using many external libraries, especially in Deno and browser projects.
💼 Career
Understanding import maps is useful for modern JavaScript and TypeScript development, improving code maintainability and simplifying dependency management.
Progress0 / 4 steps
1
Create the import map file
Create a file named import_map.json with an imports object that maps "lodash" to "https://cdn.skypack.dev/lodash" and "dayjs" to "https://cdn.skypack.dev/dayjs".
Deno
Hint
Remember, the import map is a JSON file with an imports key that holds alias-to-URL mappings.
2
Write the Deno script with aliased imports
Create a file named app.ts and write import statements that import _ from "lodash" and dayjs from "dayjs". Then, write a line that uses _.capitalize to capitalize the string "deno" and store it in a variable capitalized. Also, create a variable today that stores the current date formatted as YYYY-MM-DD using dayjs().format.
Deno
Hint
Use the aliases exactly as defined in the import map for your import statements.
3
Configure Deno to use the import map
Run the Deno script app.ts using the --import-map=import_map.json flag to enable the import map.
Deno
Hint
Use the --import-map flag exactly as shown when running your script.
4
Add console logs to show the results
Add two console.log statements to print the variables capitalized and today to the console.
Deno
Hint
Use console.log to show the values of your variables.
Practice
(1/5)
1. What is the main purpose of using import maps in Deno?
easy
A. To automatically update all dependencies to their latest versions
B. To speed up the execution of Deno scripts
C. To compile TypeScript files into JavaScript
D. To replace long URLs with short, easy-to-read names in imports
Solution
Step 1: Understand what import maps do
Import maps allow you to map long URLs to short names, making imports cleaner.
Step 2: Identify the main benefit
This makes code easier to read and maintain by avoiding long URLs in import statements.
Final Answer:
To replace long URLs with short, easy-to-read names in imports -> Option D
Quick Check:
Import maps = cleaner imports [OK]
Hint: Import maps shorten URLs to simple names [OK]
Common Mistakes:
Thinking import maps speed up code execution
Confusing import maps with dependency updates
Believing import maps compile code
2. Which command correctly runs a Deno script using an import map file named imports.json?
easy
A. deno run --importmap imports.json script.ts
B. deno run --import-map=imports.json script.ts
C. deno run --map-import=imports.json script.ts
D. deno run -importmap imports.json script.ts
Solution
Step 1: Recall the correct flag syntax
The correct flag to specify an import map file is --import-map=.
Step 2: Match the command with correct syntax
deno run --import-map=imports.json script.ts uses --import-map=imports.json, which is the correct form.
Final Answer:
deno run --import-map=imports.json script.ts -> Option B
Quick Check:
Use --import-map=filename [OK]
Hint: Use double dash and equal sign for import map flag [OK]