📖 Scenario: You are building a small Deno project that uses external libraries. To make sure your project always uses the exact same versions of these libraries, you will create and use a lock file.
🎯 Goal: Create a Deno project that imports a library, generate a lock file to lock dependencies, and configure Deno to use the lock file when running the project.
📋 What You'll Learn
Create a Deno script that imports the colors module from https://deno.land/std@0.203.0/fmt/colors.ts
Create a lock file named lock.json to lock the dependencies
Configure the Deno run command to use the lock file and ensure it is checked
Modify the script to print a colored message using the imported module
💡 Why This Matters
🌍 Real World
Lock files help keep your project dependencies stable and secure by preventing unexpected changes when others run your code or when you deploy.
💼 Career
Understanding how to use lock files is important for software developers to maintain reliable builds and avoid bugs caused by changing dependencies.
Progress0 / 4 steps
1
Create a Deno script importing the colors module
Create a file named mod.ts and write a line to import green from https://deno.land/std@0.203.0/fmt/colors.ts.
Deno
Hint
Use the import statement with curly braces to import green from the exact URL.
2
Add a message variable to use the green color function
In mod.ts, create a constant called message and assign it the result of calling green with the string "Hello, Deno!".
Deno
Hint
Use const message = green("Hello, Deno!") to create the colored message.
3
Generate a lock file for dependencies
Run the Deno command to cache dependencies and generate a lock file named lock.json for your project.
Deno
Hint
Use deno cache --lock=lock.json --lock-write mod.ts in your terminal to generate the lock file.
4
Run the script using the lock file to ensure dependency integrity
Add a line to mod.ts to print the message variable. Then run the script with Deno using the --lock=lock.json and --lock-check flags to enforce the lock file.
Deno
Hint
Use console.log(message) to print the colored message. Run with deno run --lock=lock.json --lock-check mod.ts to use the lock file.
Practice
(1/5)
1. What is the main purpose of a lock file in a Deno project?
easy
A. To list all available Deno commands
B. To speed up the execution of Deno scripts
C. To save exact versions of dependencies for consistent builds
D. To store environment variables securely
Solution
Step 1: Understand what a lock file does
A lock file records the exact versions and hashes of dependencies used in a project to ensure consistency.
Step 2: Identify the purpose in Deno context
In Deno, the lock file prevents unexpected changes by locking dependency versions during installs and runs.
Final Answer:
To save exact versions of dependencies for consistent builds -> Option C
Quick Check:
Lock file = exact dependency versions [OK]
Hint: Lock files keep dependencies stable and consistent [OK]
Common Mistakes:
Thinking lock files speed up script execution
Confusing lock files with environment variable storage
Assuming lock files list commands
2. Which Deno command option is used to create or update a lock file?
easy
A. --unstable
B. --lock
C. --reload
D. --lock-write
Solution
Step 1: Identify command options related to lock files
--lock is used to specify a lock file to use, but does not create or update it.
Step 2: Understand which option writes the lock file
--lock-write tells Deno to create or update the lock file with current dependencies.
Final Answer:
--lock-write -> Option D
Quick Check:
Update lock file = --lock-write [OK]
Hint: Use --lock-write to create or update lock files [OK]
Common Mistakes:
Using --lock alone to update lock files
Confusing --reload with lock file creation
Thinking --unstable manages lock files
3. Given the command deno run --lock=lock.json --unstable app.ts, what will happen if lock.json is missing?
medium
A. Deno will throw an error and refuse to run
B. Deno will create a new lock.json automatically
C. Deno will run the script but ignore dependencies
D. Deno will run the script without checking the lock file
Solution
Step 1: Understand the --lock option behavior
Using --lock requires the specified lock file to exist; otherwise, Deno refuses to run to ensure integrity.
Step 2: Check what happens if the lock file is missing
If lock.json is missing, Deno throws an error and stops execution to prevent unverified dependencies.
Final Answer:
Deno will throw an error and refuse to run -> Option A