logoDenoby example

Beginner

clideploy

Importing & Exporting

Edit

To build composable programs, it is necessary to be able to import and export functions from other modules. This is accomplished by using ECMA script modules in Deno.

To export a function, you use the export keyword.
./util.ts
export function sayHello(thing: string) {
  console.log(`Hello, ${thing}!`);
}
You can also export types, variables, and classes.
./util.ts
export interface Foo {}
export class Bar {}
export const baz = "baz";
To import things from files other files can use the import keyword.
./main.ts
import { sayHello } from "./util.ts";
sayHello("World");
You can also import all exports from a file.
./main.ts
import * as util from "./util.ts";
util.sayHello("World");
Imports don't have to be relative, they can also reference absolute file or https URLs.
./main.ts
import { VERSION } from "https://deno.land/std/version.ts";
console.log(VERSION);

Run this example locally using the Deno CLI:

deno run https://examples.deno.land/import-export/main.ts