diff --git a/packages/next/src/export/worker.ts b/packages/next/src/export/worker.ts index 41f62fd619e07..da9a21ce7027c 100644 --- a/packages/next/src/export/worker.ts +++ b/packages/next/src/export/worker.ts @@ -424,7 +424,9 @@ export default async function exportPage({ const filename = posix.join(distDir, 'server', 'app', page) // Load the module for the route. - const module = RouteModuleLoader.load(filename) + const module = await RouteModuleLoader.load( + filename + ) // Call the handler with the request and context from the module. const response = await module.handle(request, context) diff --git a/packages/next/src/lib/server-external-packages.json b/packages/next/src/lib/server-external-packages.json index 3cfffa1b8825d..6b9eb5db08b4f 100644 --- a/packages/next/src/lib/server-external-packages.json +++ b/packages/next/src/lib/server-external-packages.json @@ -1,5 +1,6 @@ [ "@prisma/client", + "@vercel/og", "@sentry/nextjs", "@sentry/node", "@swc/core", diff --git a/packages/next/src/server/future/helpers/module-loader/module-loader.ts b/packages/next/src/server/future/helpers/module-loader/module-loader.ts index 35376aebbb03b..ffdb6835fae8e 100644 --- a/packages/next/src/server/future/helpers/module-loader/module-loader.ts +++ b/packages/next/src/server/future/helpers/module-loader/module-loader.ts @@ -2,5 +2,5 @@ * Loads a given module for a given ID. */ export interface ModuleLoader { - load(id: string): M + load(id: string): Promise } diff --git a/packages/next/src/server/future/helpers/module-loader/node-module-loader.ts b/packages/next/src/server/future/helpers/module-loader/node-module-loader.ts index d9a2fee01e8d5..3283eb00a53f5 100644 --- a/packages/next/src/server/future/helpers/module-loader/node-module-loader.ts +++ b/packages/next/src/server/future/helpers/module-loader/node-module-loader.ts @@ -1,12 +1,13 @@ import { ModuleLoader } from './module-loader' /** - * Loads a module using `require(id)`. + * Loads a module using `await require(id)`. */ export class NodeModuleLoader implements ModuleLoader { - public load(id: string): M { + public async load(id: string): Promise { if (process.env.NEXT_RUNTIME !== 'edge') { - return require(id) + // Need to `await` to cover the case that route is marked ESM modules by ESM escalation. + return await require(id) } throw new Error('NodeModuleLoader is not supported in edge runtime.') diff --git a/packages/next/src/server/future/helpers/module-loader/route-module-loader.ts b/packages/next/src/server/future/helpers/module-loader/route-module-loader.ts index 053ba91427c4e..4f68de2d50c34 100644 --- a/packages/next/src/server/future/helpers/module-loader/route-module-loader.ts +++ b/packages/next/src/server/future/helpers/module-loader/route-module-loader.ts @@ -8,12 +8,12 @@ export interface AppLoaderModule { } export class RouteModuleLoader { - static load( + static async load( id: string, loader: ModuleLoader = new NodeModuleLoader() - ): M { + ): Promise { if (process.env.NEXT_RUNTIME !== 'edge') { - const { routeModule }: AppLoaderModule = loader.load(id) + const { routeModule }: AppLoaderModule = await loader.load(id) return routeModule } diff --git a/packages/next/src/server/future/route-handler-managers/route-handler-manager.ts b/packages/next/src/server/future/route-handler-managers/route-handler-manager.ts index 8a4e17372257c..a15104e07b129 100644 --- a/packages/next/src/server/future/route-handler-managers/route-handler-manager.ts +++ b/packages/next/src/server/future/route-handler-managers/route-handler-manager.ts @@ -27,7 +27,7 @@ export class RouteHandlerManager { context: RouteHandlerManagerContext ): Promise { // The module supports minimal mode, load the minimal module. - const module = RouteModuleLoader.load( + const module = await RouteModuleLoader.load( match.definition.filename, this.moduleLoader )