loading...
Cover image for Use your favorite package in Deno

Use your favorite package in Deno

gobeli profile image Etienne 惻1 min read

Deno - TypeScript

As you probably know, deno has first class TypeScript support. This means it can interpret and run TypeScript without any additional tooling (unlike node). Since TypeScript is an extension of JavaScript, you are able to execute normal JS code with Deno.

As a consequence of this, many great JS packages can still be used in Deno. The main requirement being, that they are available in the form of an es module. This is awesome, because many great and proven JS packages are not yet available as a Deno module

Using es modules in Deno

There are a few CDN's which provide npm packages as es modules, for instance pika or jspm.

To use an es module in Deno you can simply import it like you would any other module. The following is an example using the modules gray-matter and marked to parse a markdown file with front matter.

import { readFileStr, exists } from "https://deno.land/std/fs/mod.ts";
import matter from "https://dev.jspm.io/gray-matter";
import marked from "https://dev.jspm.io/marked";

async function main(file: string) {
  const fileExists = await exists(file);

  if (!fileExists) {
    throw new Error(`File ${file} not found`);
  }

  const content = await readFileStr(file);
  const parsed = matter(content);
  parsed.content = marked(parsed.content);
  console.log(parsed);
}

main(Deno.args[0]);

Image by @hashrock

Discussion

markdown guide