From 1f3d2d56f17bdc61f91b72cb770667378ef521e0 Mon Sep 17 00:00:00 2001 From: Dominic Saadi Date: Thu, 2 Nov 2023 12:44:18 -0700 Subject: [PATCH] fix(server-file): prefix `MODULE_NOT_FOUND` with `ERR_` (#9372) If you try to `await import` a module and it can't be found, it turns out Node throws a different error than if you try to `require` a module and it can't be found: - `require(mod)` -> `MODULE_NOT_FOUND` - `await import(mod)` -> `ERR_MODULE_NOT_FOUND` We used to tranpsile `await import`s to `require`. When we stopped doing that, we forgot to change the error codes we were checking. --- packages/internal/src/generate/graphqlSchema.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/packages/internal/src/generate/graphqlSchema.ts b/packages/internal/src/generate/graphqlSchema.ts index d253057ef161..b4542780ec81 100644 --- a/packages/internal/src/generate/graphqlSchema.ts +++ b/packages/internal/src/generate/graphqlSchema.ts @@ -24,13 +24,16 @@ export const generateGraphQLSchema = async () => { 'subscriptions/**/*.{js,ts}': {}, } - // If we are serverful and the user is using realtime, we need to include the live directive for realtime support. + // If we're serverful and the user is using realtime, we need to include the live directive for realtime support. + // Note the `ERR_ prefix in`ERR_MODULE_NOT_FOUND`. Since we're using `await import`, + // if the package (here, `@redwoodjs/realtime`) can't be found, it throws this error, with the prefix. + // Whereas `require('@redwoodjs/realtime')` would throw `MODULE_NOT_FOUND`. if (resolveFile(`${getPaths().api.src}/server`)) { try { const { liveDirectiveTypeDefs } = await import('@redwoodjs/realtime') schemaPointerMap[liveDirectiveTypeDefs] = {} } catch (error) { - if ((error as { code: string }).code !== 'MODULE_NOT_FOUND') { + if ((error as { code: string }).code !== 'ERR_MODULE_NOT_FOUND') { throw error } }