DEV Community

Cover image for Deno Basics: Deno's config file, dependencies and the Lock file
Dannar Mawardi
Dannar Mawardi

Posted on

Deno Basics: Deno's config file, dependencies and the Lock file

Deno's configuration file (Deno.json) is Deno's answer to the conveniences of package.json in NodeJS and allows you to create tasks that can be run as commands using terminal.

Note: Deno will automatically detect the configuration file if placed in the current working directory.

deno.json

{
  "tasks": {
    "start": "deno run -A --watch=public/,routes/,db/,services/,controllers/ mod.ts",
    "prod": "deno run -A mod.ts"
  },
}
Enter fullscreen mode Exit fullscreen mode

To run the created tasks, you can use: deno task
For example: deno task start

Importing dependencies

There are two options for using dependencies in Deno: creating a deps.ts file or using an import map.

Deps file

The deps file was the original method of listing all your dependencies in a single file to export from. With the convention to split standard library and third library dependencies.

deps.ts

// Standard library dependencies
export * as log from "https://deno.land/std@0.147.0/log/mod.ts";

// Third party dependencies
export * as _ from "https://deno.land/x/lodash@4.17.15-es/lodash.js";
Enter fullscreen mode Exit fullscreen mode

Import Map

The import map is the newer method to use for importing dependencies and is used in conjuction with the deno.json file.

The convention is to place the import map in your current working directory along with your deno.json.

import_map.json

{
  "imports": {
    "twind": "https://esm.sh/twind@0.16.17",
    "log": "https://deno.land/std@0.147.0/log/mod.ts",
    "oak": "https://deno.land/x/oak@v10.6.0/mod.ts",
}
}
Enter fullscreen mode Exit fullscreen mode

deno.json

{
  "tasks": {
    "start": "deno run -A --watch=public/,routes/,db/,services/,controllers/ mod.ts",
    "prod": "deno run -A mod.ts"
  },

  "importMap": "./import_map.json",
}
Enter fullscreen mode Exit fullscreen mode

If you are already using a deno config file, it makes sense to use the import map method.

Lock File

Deno has its own solution to avoid conflict with dependencies between different environments: lock files. A lock file is able to go online and assess the current state of all your dependencies. It stores this in a hash for each dependency.

This gives you the ability to ensure that the version that is being used with the project, is the exact one used when you first created the lock file.

deno cache --lock=lock.json --lock-write src/deps.ts
--lock=: Indicates to Deno where your lock file is
--lock-write: Lets Deno know you wish to write a new one

src/deps.ts: A sample path provided for your dependencies

Top comments (0)