...

/

Managing Dependencies

Managing Dependencies

Learn how to manage dependencies in Deno using a single JavaScript file.

Exporting dependencies

If we have used other tools, or even Node.js, we might feel that it isn’t very intuitive to have URLs all around the code. We can also argue that by directly writing URLs in our code, we might cause problems, such as having two different versions of the same dependency or having typos in the URL.

Deno solved this problem by getting rid of complex module resolution strategies and using plain JavaScript and absolute imports instead.

The proposed solution to keep track of dependencies, which is no more than a suggestion, is to use a file that exports all the required dependencies and place them in a single file that contains URLs. Let’s see it in action.

We create a file called deps.js and add our dependencies there, exporting the ones we need:

Press + to interact
export { serve } from "https://deno.land/std@0.83.0/http/server.ts";

Using the preceding syntax, we’re importing the serve method from the standard library’s HTTP server and exporting it.

Back in our hello-http-server.js file, we can now change the imports so that we can use the exported function from the deps.js file:

Press + to interact
import { serve } from "./deps.js";
for await (const req of serve(":8080")) {
req.respond({ body: "Hello deno" });
}

Every time we add a dependency, we can run the following command to guarantee that we have a local copy of the module.

deno cache deps.js

This is Deno’s way of managing dependencies. It’s that simple, with no complex standards, just a file that imports and exports symbols.

Note: click the “Run” button in the terminal toward the end of the lesson, and execute this ...