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
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, https, or [JSR](https://jsr.io) URLs.
./main.ts
import { camelCase } from "jsr:@luca/cases@1";
console.log(camelCase("hello world")); // helloWorld
import OpenAI from "https://deno.land/x/openai@v4.53.0/mod.ts";
const client = new OpenAI();