From 360c71e39a31b96e82828563f46f8269b80595cc Mon Sep 17 00:00:00 2001 From: Sebastian Rindom Date: Sun, 28 Jan 2024 00:28:11 +0100 Subject: [PATCH] fix: improve error message on incorrect service export (#6240) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit **What** - Provide a more helpful error message if a user adds a file in `src/services` that doesn't export a service class. **Why** If you forget to `export default MyClass` in a custom service you can end up with this error: ![CleanShot 2024-01-27 at 6  24 19](https://github.com/medusajs/medusa/assets/7554214/d3700487-33e0-4629-b2c4-a2a37cb86b3e) It's almost impossible to know how to recover from this which can be an issue for new users. The new error message informs the user that there is a missing class export and shows which file the error is related to. --- .changeset/smart-ants-compete.md | 5 +++++ packages/medusa/src/loaders/plugins.ts | 6 ++++++ 2 files changed, 11 insertions(+) create mode 100644 .changeset/smart-ants-compete.md diff --git a/.changeset/smart-ants-compete.md b/.changeset/smart-ants-compete.md new file mode 100644 index 0000000000..63a1d5ddb5 --- /dev/null +++ b/.changeset/smart-ants-compete.md @@ -0,0 +1,5 @@ +--- +"@medusajs/medusa": patch +--- + +fix(medusa): improve error message on incorrect service export diff --git a/packages/medusa/src/loaders/plugins.ts b/packages/medusa/src/loaders/plugins.ts index 1e21f1f8cb..97e2399837 100644 --- a/packages/medusa/src/loaders/plugins.ts +++ b/packages/medusa/src/loaders/plugins.ts @@ -469,6 +469,12 @@ export async function registerServices( const loaded = require(fn).default const name = formatRegistrationName(fn) + if (typeof loaded !== "function") { + throw new Error( + `Cannot register ${name}. Make sure to default export a service class in ${fn}` + ) + } + const context = { container, pluginDetails, registrationName: name } registerPaymentProcessorFromClass(loaded, context)