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. Only the main entry file is compiled and executed; imports are resolved relative to it.
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))
The alias becomes a namespace containing all exported functions from that module. Call exported functions via the alias.
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.
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 for the future, which will add registry-based packages on top of file imports.