Modules & Imports
Build multi-file projects with exports and imports.
Module System
Kria supports multi-file projects where modules can be imported and used. Each file is a module, and only explicitly exported functions are accessible to importers.
To run a multi-file project, you provide the main entry file:
kria main.krx
The main file can import other modules. The entry file and its imported dependencies are compiled as one project and executed together. Each import path is resolved relative to the file that contains that import.
Exporting Functions
Use export fn to make a function visible to importers:
// math.krx
export fn add(a, b) {
return a + b
}
export fn mul(a, b) {
return a * b
}
fn secret() {
return 0
}
Only exported functions are visible.
Functions without export are private to the module.
Importing Modules
Import a module with import alias from "./path":
// main.krx
import math from "./math.krx"
print(math.add(2, 3))
print(math.mul(4, 5))
Use the import alias to refer to an exported function. The compiler resolves these references to the imported function; the alias is not a general-purpose runtime module object. Only exported functions are available this way.
Relative Paths
Import paths must be relative and use forward slashes:
./filename.krx— Import from same directory../sibling.krx— Import from parent directory./subdir/module.krx— Import from subdirectory
Paths are resolved relative to the file doing the importing.
Imports must be at module top level and use the .krx extension.
Circular imports are rejected. The current system has no package registry or
non-relative package search path; the REPL does not support imports.
Module globals share the compiled program's global namespace, so avoid
reusing global names across files.
Project Structure Example
project/
├── main.krx
├── math/
│ └── operations.krx
└── utils/
└── helpers.krx
In main.krx:
import math from "./math/operations.krx"
import utils from "./utils/helpers.krx"
Best Practices
- Clear naming — Use descriptive names for imports and modules
- Logical organization — Group related functions in modules
- Avoid circular imports — They are detected and rejected at compile time
- Keep modules focused — Each module should have a single responsibility
- Document exports — Add comments explaining exported functions
Kria's package manager (KPM) is planned; the current language repository documents only relative file imports.