- Get Started
- Product
- Resources
- Tools & SDKs
- Framework
- Reference
Menu
- Get Started
- Product
- Resources
- Tools & SDKs
- Framework
- Reference
3.7. Loaders
In this chapter, you’ll learn about loaders and how to use them.
What is a Loader?#
A loader is a function executed when the Medusa application starts. You define and export it in a module.
Loaders are useful to perform a task at the application start-up, such as to sync data between Medusa and a third-party service.
How to Create a Loader?#
A loader is created in a TypeScript or JavaScript file under a module's loaders
directory.
For example, create the file src/modules/hello/loaders/hello-world.ts
with the following content:
Export Loader in Module Definition#
Import the loader in src/modules/hello/index.ts
and export it in the module's definition:
The value of the loaders
property is an array of loader functions.
Test the Loader#
Start the Medusa application:
Among the messages logged in the terminal, you’ll see the following message:
When to Use Loaders#
Use loaders when:
- You're performing an action at application start-up.
- You're establishing a one-time connection with an external system.
Don't use loaders if: You want to perform an action continuously or at a set time pattern in the application. Use scheduled jobs instead, which is explained in an upcoming chapter.
Was this chapter helpful?