Learn about Ant’s ES modules, built-in modules, and module resolution
Ant implements a full ES module system with support for both ECMAScript modules (ESM) and CommonJS. The runtime includes built-in Node.js-compatible modules and Ant-specific modules with the ant: prefix.
import { $ } from 'ant:shell';// Execute command and get resultconst result = $`ls -la`;console.log(result.text());// Command with interpolationconst dir = '/tmp';const output = $`ls ${dir}`;// Example from examples/demo/destr.jsimport { $ } from 'ant:shell';import parse from 'https://esm.sh/destr';const data = $`maid -g json-hydrated`.text();console.log(parse(data));
import { open } from 'ant:lmdb';// Open databaseconst db = open('./mydb', { create: true });// Store and retrieve datadb.put('key', 'value');const value = db.get('key');// Transactionsdb.transaction(() => { db.put('key1', 'value1'); db.put('key2', 'value2');});
// CommonJS exportmodule.exports = { add: (a, b) => a + b, subtract: (a, b) => a - b};// CommonJS requireconst math = require('./math.cjs');console.log(math.add(2, 3));
// Current module URLconsole.log(import.meta.url);// Is this the main module?if (import.meta.main) { console.log('Running as main module');}// Module directory (Ant-specific)console.log(__dirname);console.log(__filename);
Ant automatically strips TypeScript type annotations:
// math.ts - TypeScript with typesexport function add(a: number, b: number): number { return a + b;}export interface Point { x: number; y: number;}// Types are stripped at runtime// No compilation step needed!
Ant strips type annotations but doesn’t type-check. Use tsc --noEmit for type checking during development.
ES modules are the modern standard and provide better static analysis:
// ✅ Good: ES modulesimport { func } from './module.js';export { func };// ❌ Avoid: CommonJS (unless needed for compatibility)const { func } = require('./module');module.exports = { func };
Include file extensions
Always include .js or .mjs extensions in import paths:
// ✅ Good: includes extensionimport { func } from './module.js';// ❌ Avoid: missing extension (works but slower)import { func } from './module';
Organize with barrel exports
Use index.js to aggregate exports:
// utils/index.jsexport { add, subtract } from './math.js';export { format } from './string.js';export { fetch } from './http.js';// Usageimport { add, format, fetch } from './utils/index.js';