Skip to main content

import

The import statement is used to include external modules, libraries, and other Soul files into your program. It enables code reuse and modular programming by allowing you to use functionality defined in other files.

Basic Import

Import an entire module:

Import with Alias

Import a module with a custom name:

Import from Relative Paths

Import from local files using relative paths:

Import from Absolute Paths

Import using absolute paths:

Import with Path Aliases

Using path aliases defined in soul.json:

Import from soul_modules

Import from installed packages:

Import Standard Library Modules

Import built-in Soul modules:

Import with Specific Exports

Import specific functions or classes:

Import with Mixed Exports

Combine default and named imports:

Import with Destructuring

Destructure imported objects:

Conditional Imports

Import modules conditionally:

Dynamic Imports

Import modules at runtime:

Import Error Handling

Handle import errors gracefully:

Import with Module Configuration

Configure imported modules:

Import in Different Contexts

In Main Files

In Module Files

In Class Files

Import Best Practices

Organize Imports

Use Aliases for Clarity

Import Only What You Need

Common Import Patterns

Configuration Module

Utility Module

Service Module

Module Resolution

Soul resolves imports in the following order:
  1. Relative paths: ./, ../
  2. Path aliases: @/, @components/
  3. soul_modules: Installed packages
  4. Standard library: Built-in modules

Import Debugging

Debug import issues:
The import statement is essential for organizing your Soul applications into modular, reusable components. Use it to structure your code effectively and leverage external libraries and modules.