What is the purpose of importing from npm packages in Deno?
Importing from npm packages allows you to use pre-built code libraries from the npm ecosystem in your Deno projects, saving time and adding functionality without writing everything from scratch.
Click to reveal answer
beginner
How do you import an npm package in Deno?
You use the special npm: prefix in the import statement, like <code>import express from "npm:express";</code>. This tells Deno to fetch the package from npm.
Click to reveal answer
intermediate
What command do you run for importing npm packages in Deno?
You run your Deno script with the --allow-net permission, for example: deno run --allow-net script.ts. This allows Deno to fetch npm packages over the network.
Click to reveal answer
intermediate
Can you import any npm package directly in Deno without extra setup?
No, Deno requires network permissions to import npm packages. Also, some packages may not work if they rely on Node.js-specific APIs.
Click to reveal answer
intermediate
What is a benefit of using npm packages in Deno compared to traditional Node.js?
Deno imports npm packages directly via URLs with no need for a separate package manager like npm or yarn, simplifying dependency management.
Click to reveal answer
How do you import the lodash package from npm in Deno?
Aimport _ from "lodash";
Bimport _ from "npm:lodash";
Cimport _ from "https://lodash.com";
Dimport _ from "deno:lodash";
✗ Incorrect
In Deno, npm packages are imported using the npm: prefix, like import _ from "npm:lodash";.
Which flag is no longer required to run a Deno script that imports npm packages?
A--unstable
B--allow-read
C--allow-env
D--no-check
✗ Incorrect
npm package support is now stable in Deno and no longer requires the --unstable flag.
What permission is commonly needed to import npm packages in Deno?
A--allow-write
B--allow-run
C--allow-net
D--allow-ffi
✗ Incorrect
Deno needs network access permission --allow-net to fetch npm packages from the internet.
Why might some npm packages not work in Deno?
AThey need a package.json file
BThey are too large
CDeno does not support JavaScript
DThey require Node.js-specific APIs not available in Deno
✗ Incorrect
Some npm packages depend on Node.js features that Deno does not support, causing incompatibility.
What is a key advantage of Deno's npm import system?
ANo need for a separate package manager
BIt only works offline
CIt requires manual downloads
DIt uses CommonJS modules only
✗ Incorrect
Deno imports npm packages directly via URLs, so you don't need npm or yarn to manage packages.
Explain how to import and use an npm package in a Deno project.
Think about the special prefix and permissions needed.
You got /3 concepts.
Describe the benefits and limitations of importing npm packages in Deno.
Consider what makes Deno's approach easier and what challenges remain.
You got /4 concepts.
Practice
(1/5)
1. In Deno, how do you import a package from npm?
easy
A. Use import pkg from "deno:package-name";
B. Use import pkg from "https://npmjs.com/package-name";
C. Use import pkg from "npm:package-name";
D. Use require("package-name")
Solution
Step 1: Understand Deno's npm import syntax
Deno uses the prefix npm: to import packages from npm directly.
Step 2: Identify the correct import statement
The correct syntax is import pkg from "npm:package-name"; which tells Deno to fetch from npm.
Final Answer:
Use import pkg from "npm:package-name"; -> Option C
Quick Check:
npm imports use "npm:" prefix [OK]
Hint: Remember npm imports start with "npm:" prefix [OK]
Common Mistakes:
Using URL instead of npm prefix
Using require() which is Node.js syntax
Using wrong prefix like deno:
2. Which of the following is the correct syntax to import only the join function from the npm package path in Deno?
easy
A. import { join } from "npm:path";
B. import join from "npm:path/join";
C. import * as join from "npm:path";
D. import join from "path";
Solution
Step 1: Understand named imports from npm packages in Deno
You can import specific functions using curly braces from the package name with npm prefix.
Step 2: Match correct syntax
The correct syntax is import { join } from "npm:path"; to import only the join function.
Final Answer:
import { join } from "npm:path"; -> Option A
Quick Check:
Named imports use curly braces with npm prefix [OK]
Hint: Use curly braces to import specific parts from npm packages [OK]
Common Mistakes:
Trying to import default when function is named export
Using wrong path like "npm:path/join"
Omitting npm prefix
3. What will be the output of this Deno code snippet?
import { v4 } from "npm:uuid";
console.log(typeof v4);
medium
A. "string"
B. "object"
C. "undefined"
D. "function"
Solution
Step 1: Understand what v4 from uuid is
The v4 export from the uuid package is a function that generates UUID strings.
Step 2: Check the type of v4
Using typeof v4 returns "function" because v4 is a function.
Final Answer:
"function" -> Option D
Quick Check:
uuid v4 is a function [OK]
Hint: Remember uuid v4 is a function generating IDs [OK]
Common Mistakes:
Thinking v4 is a string UUID directly
Confusing type with object or undefined
Forgetting to import with npm prefix
4. You wrote this import in Deno:
import express from "npm:express";
But running the code gives an error: SyntaxError: The requested module does not provide an export named 'default'. What is the likely fix?
medium
A. Change import to import * as express from "npm:express";
B. Change import to import express from "express";
C. Add --allow-net flag when running Deno
D. Use require("express") instead
Solution
Step 1: Understand the error about missing default export
The error means express does not have a default export, so import express from ... fails.
Step 2: Use namespace import to get all exports
Using import * as express from "npm:express"; imports all exports as an object, fixing the error.
Final Answer:
Change import to import * as express from "npm:express"; -> Option A
Quick Check:
No default export means use * as import [OK]
Hint: If default export missing, try namespace import * as [OK]
Common Mistakes:
Removing npm prefix
Using require() which is not supported in Deno
Ignoring error and expecting code to run
5. You want to import the lodash package from npm in Deno and use its chunk function to split an array into smaller arrays of size 2. Which code snippet correctly does this?
hard
A. import { chunk } from "npm:lodash";
console.log(chunk([1,2,3,4,5], 2));
B. import _ from "npm:lodash";
console.log(_.chunk([1,2,3,4,5], 2));
C. import chunk from "npm:lodash/chunk";
console.log(chunk([1,2,3,4,5], 2));
D. import { chunk } from "lodash";
console.log(chunk([1,2,3,4,5], 2));
Solution
Step 1: Understand lodash exports and npm import in Deno
Lodash exports a default object with functions like chunk. To use chunk, import the default as _.
Step 2: Check import syntax with npm prefix
Use import _ from "npm:lodash"; to import lodash default export, then call _.chunk(...).
Final Answer:
import _ from "npm:lodash";
console.log(_.chunk([1,2,3,4,5], 2)); -> Option B