Beginner
clideploy
Importing & Exporting
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
!--frsh-copybutton:1-->
export function sayHello(thing: string) {
console.log(`Hello, ${thing}!`);
}
You can also export types, variables, and classes.
./util.ts
!--frsh-copybutton:2-->
export interface Foo {}
export class Bar {}
export const baz = "baz";
To import things from files other files can use the import keyword.
./main.ts
!--frsh-copybutton:4-->
import { sayHello } from "./util.ts";
sayHello("World");
You can also import all exports from a file.
./main.ts
!--frsh-copybutton:5-->
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
!--frsh-copybutton:6-->
import { VERSION } from "https://deno.land/std/version.ts";
console.log(VERSION);
Run this example locally using the Deno CLI:
deno run https://byexample-wwv03xf36j0g.deno.dev/import-export/main.ts
Additional resources: