Deno - Export and Import Local & Remote Modules Examples

This tutorial shows you how to export and import local modules as well as how to import remote modules in Deno.

In an application, it's quite common to have some helper or utility files that can be used in other files. In Deno, a function, constant, or class defined in a file is not automatically accessible from another file. You have to explicitly export in the file where it's declared and then import it in the file where you want to use it. Deno uses the ECMAScript 6 export/import standard. To import a file, the file name must be specified fully including the extension. In addition, unlike Node.js, it doesn't have special handling for index.js file.

Export Modules

In order for a function, constant, or class to be accessible from other files, it must be exported explicitly. What you need to do is add export keyword to the beginning of the function, constant, or class. Alternatively, you can export desired members at the end of the module using a single export statement.

utils/number.ts

  export const ZERO = 0;
  export const ONE = 1;
  
  export function compare(a: number, b: number): number {
    if (a === b) {
        return 0;
    }
  
    return a < b ? -1 : 1;
  }
  
  function maximum(a: number, b: number): number {
    return a >= b ? a : b;
  }

  export class CustomNumber {
    constructor() {}
  }

  class SuperNumber {
    constructor() {}
  }

  export { maximum, SuperNumber };

 

Import Local Modules

To use the function, constant or class declared on utils/number.ts file above, you need to import them first. Deno supports ES6 import style. The file name must be specified by its path relative to the current file. The extension of the file cannot be removed. To import a file in the same directory or in a sub directory relative to the current file, the file path must be preceded by ./. If the imported file is in the upper directory, use ../ to move up one directory, which means you may need multiple ../ if you need to go up more than one directory. There is no special treatment for any file name including index.js. You can also import with alias by adding as keyword followed with the alias name after the function/constant name.

main.ts

  import { ZERO, compare, maximum as max, CustomNumber } from './utils/number.ts';

  console.log(compare(ZERO, 2));
  console.log(max(ZERO, -1));
  const customNumber = new CustomNumber();

If the imported module doesn't exist, you'll get 'Cannot resolve module' error. It also happens if you forget to add the file extension.

  error: Cannot resolve module "file:///home/ivan/Projects/deno/src/examples/utils/number" from "file:///home/ivan/Projects/deno/src/examples/import_local.ts".
    at file:///home/ivan/Projects/deno/src/examples/import_local.ts:1:0

If one of the imported members doesn't exist or is not exported, you'll get an error indicating that the module has no exported member.

  error: TS2305 [ERROR]: Module '"file:///home/ivan/Projects/deno/src/examples/utils/number.ts"' has no exported member 'x'.
import { ZERO, compare, maximum as max, CustomNumber, x } from './utils/number.ts';

 

Import Remote Modules

If you need to use functions from a remote module, you can use a similar import statement, but with the URL of the module instead of a local file path. The URL usually contains the version of the module, so that it's guaranteed that you always get a specific version of the module. You can also import a JavaScript module to a TypeScript module.

  import { v4 } from 'https://deno.land/std@0.79.0/uuid/mod.ts';
  
  console.log(v4.generate());

If the imported module doesn't exist, including using a non-existing version, you'll get 404 not found error

  error: Import 'https://deno.land/std@0.79.10000/uuid/mod.ts' failed: 404 Not Found
      at file:///home/ivan/Projects/deno/src/examples/deps.ts:1:0

The above example imports a function and directly uses it in the same file. However, it's recommended to put all dependencies to remote modules in a file. The convention is creating a file named deps.ts containing all dependencies. Inside dept.ts, you need to re-export the functions and classes to be used. Therefore, it becomes easier to change the version of the module because you only need to change the one in deps.ts.

deps.ts

  import { v4 as uuidV4 } from 'https://deno.land/std@0.79.0/uuid/mod.ts';
  
  export { uuidV4 };

main.ts

  import { uuidV4 } from './deps.ts';
  
  console.log(uuidV4.generate());

That's how to export and import modules using Deno. For local export and import, it's quite similar to the ES6 standard. For local import, you need to use the URL of the module and it's recommended to put all dependencies to remote modules in a file named deps.ts.