netlify/next-runtime

[Bug]: Error: Cannot find module 'next/dist/server/future/route-modules/pages/vendored/contexts/router-context'

mainawycliffe opened this issue · 5 comments

Summary

I am getting an error when hitting API endpoints (uses next config for proxy in some cases and has an API catch-all route for others) after upgrading next 14 from 13.4.x

A link to a reproduction repository

Expected Result

Should proxy the API correctly

Actual Result

Error:

Nov 30, 09:18:23 AM: f07e28aa ERROR   ⨯ Error: Cannot find module 'next/dist/server/future/route-modules/pages/vendored/contexts/router-context'
Require stack:
- /var/task/node_modules/next/dist/client/compat/router.js
- /var/task/node_modules/next/compat/router.js
- /var/task/node_modules/@clerk/nextjs/dist/cjs/client-boundary/ClerkProvider.js
- /var/task/node_modules/@clerk/nextjs/dist/cjs/components.client.js
- /var/task/node_modules/@clerk/nextjs/dist/cjs/index.js
- /var/task/.next/server/pages/_app.js
- /var/task/node_modules/next/dist/server/require.js
- /var/task/node_modules/next/dist/server/next-server.js
- /var/task/.netlify/functions-internal/___netlify-handler/___netlify-handler.js
- /var/task/___netlify-handler.js
- /var/runtime/index.mjs
    at Module._resolveFilename (node:internal/modules/cjs/loader:1077:15)
    at /var/task/node_modules/next/dist/server/require-hook.js:54:36
    at Module._load (node:internal/modules/cjs/loader:922:27)
    at Module.require (node:internal/modules/cjs/loader:1143:19)
    at mod.require (/var/task/node_modules/next/dist/server/require-hook.js:62:32)
    at require (node:internal/modules/cjs/helpers:119:18)
    at Object.<anonymous> (/var/task/node_modules/next/dist/client/compat/router.js:12:37)
    at Module._compile (node:internal/modules/cjs/loader:1256:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1310:10)
    at Module.load (node:internal/modules/cjs/loader:1119:32) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    '/var/task/node_modules/next/dist/client/compat/router.js',
    '/var/task/node_modules/next/compat/router.js',
    '/var/task/node_modules/@clerk/nextjs/dist/cjs/client-boundary/ClerkProvider.js',
    '/var/task/node_modules/@clerk/nextjs/dist/cjs/components.client.js',
    '/var/task/node_modules/@clerk/nextjs/dist/cjs/index.js',
    '/var/task/.next/server/pages/_app.js',
    '/var/task/node_modules/next/dist/server/require.js',
    '/var/task/node_modules/next/dist/server/next-server.js',
    '/var/task/.netlify/functions-internal/___netlify-handler/___netlify-handler.js',
    '/var/task/___netlify-handler.js',
    '/var/runtime/index.mjs'
  ]
}

Steps to reproduce

Upgrade to version 14

Next Runtime version

14.0.2

Is your issue related to the app directory?

  • Yes, I am using the app directory

More information about your build

  • I am building using the CLI
  • I am building using file-based configuration (netlify.toml)

What OS are you using?

None

Your netlify.toml file

`netlify.toml`
[build]
  publish = ".next"

[[plugins]]
  package = "@netlify/plugin-nextjs"

[context.deploy-preview.environment]
  NEXT_PUBLIC_WITHOUT_SESSION = "false"
  NODE_ENV = "development"
  ENV = "netlify"

[context.production.environment]
  NEXT_PUBLIC_WITHOUT_SESSION = "false"
  NODE_ENV = "development"

Your public/_redirects file

`_redirects`
# Paste content of your `_redirects` file here

Your next.config.js file

`next.config.js`
/**
 * @type {import('next').NextConfig}
 */
const config = {
  productionBrowserSourceMaps: true,
  env: {
    // make the backend URL available to the frontend
    NEXT_PUBLIC_BACKEND_URL: process.env.BACKEND_URL
  },
  async rewrites() {
    // if clerk is enabled, we will use next API routes to proxy requests to
    // the backend
    if (process.env.NEXT_PUBLIC_AUTH_IS_CLERK === "true") {
      return [];
    }

    // Read at build time. See Dockerfile for deployment related steps.
    const backendURL = process.env.BACKEND_URL || "http://localhost:3000/";
    const isCanary =
      process.env.NEXT_PUBLIC_APP_DEPLOYMENT === "CANARY_CHECKER";
    const canaryPrefix = isCanary ? "" : "/canary";
    const LOCALHOST_ENV_URL_REWRITES = [
      {
        source: "/api/:path*",
        destination: `${backendURL}/api/:path*`
      }
    ];

    const URL_REWRITES = [
      {
        source: "/api/canary/:path*",
        destination: `${backendURL}/${canaryPrefix}/:path*`
      },
      {
        source: "/api/.ory/:path*",
        destination: `${backendURL}/kratos/:path*`
      },
      // All other API requests are proxied to the backend on the same path
      // as the request.
      {
        source: "/api/:path*",
        destination: `${backendURL}/:path*`
      }
    ];
    const rewrites = ["localhost", "netlify"].includes(process.env.ENV)
      ? LOCALHOST_ENV_URL_REWRITES
      : URL_REWRITES;

    return rewrites;
  },
  // https://github.com/vercel/next.js/tree/canary/examples/with-docker#in-existing-projects
  ...(process.env.NEXT_STANDALONE_DEPLOYMENT === "true"
    ? {
        output: "standalone"
      }
    : {}),
  experimental: {
    // increase the default timeout for the proxy from 30s to 10m to allow for
    // long running requests to the backend
    proxyTimeout: 1000 * 60 * 10
  },
  transpilePackages: ["monaco-editor"]
};

module.exports = config;

Builds logs (or link to your logs)

Build logs
# Paste logs here

Function logs

Function logs
Nov 30, 09:18:18 AM: INIT_START Runtime Version: nodejs:18.v18	Runtime Version ARN: arn:aws:lambda:us-east-1::runtime:d949ec0248c1b8fbc8cbc6df1c986e05fb933506b51d53be82d46f9a37125bea
Nov 30, 09:18:19 AM: 76b0e05d ERROR   ⨯ Error: Cannot find module 'next/dist/server/future/route-modules/pages/vendored/contexts/router-context'
Require stack:
- /var/task/node_modules/next/dist/client/compat/router.js
- /var/task/node_modules/next/compat/router.js
- /var/task/node_modules/@clerk/nextjs/dist/cjs/client-boundary/ClerkProvider.js
- /var/task/node_modules/@clerk/nextjs/dist/cjs/components.client.js
- /var/task/node_modules/@clerk/nextjs/dist/cjs/index.js
- /var/task/.next/server/pages/_app.js
- /var/task/node_modules/next/dist/server/require.js
- /var/task/node_modules/next/dist/server/next-server.js
- /var/task/.netlify/functions-internal/___netlify-handler/___netlify-handler.js
- /var/task/___netlify-handler.js
- /var/runtime/index.mjs
    at Module._resolveFilename (node:internal/modules/cjs/loader:1077:15)
    at /var/task/node_modules/next/dist/server/require-hook.js:54:36
    at Module._load (node:internal/modules/cjs/loader:922:27)
    at Module.require (node:internal/modules/cjs/loader:1143:19)
    at mod.require (/var/task/node_modules/next/dist/server/require-hook.js:62:32)
    at require (node:internal/modules/cjs/helpers:119:18)
    at Object.<anonymous> (/var/task/node_modules/next/dist/client/compat/router.js:12:37)
    at Module._compile (node:internal/modules/cjs/loader:1256:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1310:10)
    at Module.load (node:internal/modules/cjs/loader:1119:32) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    '/var/task/node_modules/next/dist/client/compat/router.js',
    '/var/task/node_modules/next/compat/router.js',
    '/var/task/node_modules/@clerk/nextjs/dist/cjs/client-boundary/ClerkProvider.js',
    '/var/task/node_modules/@clerk/nextjs/dist/cjs/components.client.js',
    '/var/task/node_modules/@clerk/nextjs/dist/cjs/index.js',
    '/var/task/.next/server/pages/_app.js',
    '/var/task/node_modules/next/dist/server/require.js',
    '/var/task/node_modules/next/dist/server/next-server.js',
    '/var/task/.netlify/functions-internal/___netlify-handler/___netlify-handler.js',
    '/var/task/___netlify-handler.js',
    '/var/runtime/index.mjs'
  ]
}
Nov 30, 09:18:19 AM: 76b0e05d ERROR   ⨯ Error: Cannot find module 'next/dist/server/future/route-modules/pages/vendored/contexts/router-context'
Require stack:
- /var/task/node_modules/next/dist/client/compat/router.js
- /var/task/node_modules/next/compat/router.js
- /var/task/node_modules/@clerk/nextjs/dist/cjs/client-boundary/ClerkProvider.js
- /var/task/node_modules/@clerk/nextjs/dist/cjs/components.client.js
- /var/task/node_modules/@clerk/nextjs/dist/cjs/index.js
- /var/task/.next/server/pages/_app.js
- /var/task/node_modules/next/dist/server/require.js
- /var/task/node_modules/next/dist/server/next-server.js
- /var/task/.netlify/functions-internal/___netlify-handler/___netlify-handler.js
- /var/task/___netlify-handler.js
- /var/runtime/index.mjs
    at Module._resolveFilename (node:internal/modules/cjs/loader:1077:15)
    at /var/task/node_modules/next/dist/server/require-hook.js:54:36
    at Module._load (node:internal/modules/cjs/loader:922:27)
    at Module.require (node:internal/modules/cjs/loader:1143:19)
    at mod.require (/var/task/node_modules/next/dist/server/require-hook.js:62:32)
    at require (node:internal/modules/cjs/helpers:119:18)
    at Object.<anonymous> (/var/task/node_modules/next/dist/client/compat/router.js:12:37)
    at Module._compile (node:internal/modules/cjs/loader:1256:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1310:10)
    at Module.load (node:internal/modules/cjs/loader:1119:32) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    '/var/task/node_modules/next/dist/client/compat/router.js',
    '/var/task/node_modules/next/compat/router.js',
    '/var/task/node_modules/@clerk/nextjs/dist/cjs/client-boundary/ClerkProvider.js',
    '/var/task/node_modules/@clerk/nextjs/dist/cjs/components.client.js',
    '/var/task/node_modules/@clerk/nextjs/dist/cjs/index.js',
    '/var/task/.next/server/pages/_app.js',
    '/var/task/node_modules/next/dist/server/require.js',
    '/var/task/node_modules/next/dist/server/next-server.js',
    '/var/task/.netlify/functions-internal/___netlify-handler/___netlify-handler.js',
    '/var/task/___netlify-handler.js',
    '/var/runtime/index.mjs'
  ]
}
Nov 30, 09:18:19 AM: 76b0e05d INFO   [GET] /api/auth/whoami (SSR)
Nov 30, 09:18:19 AM: 76b0e05d Duration: 320.01 ms	Memory Usage: 97 MB	Init Duration: 450.80 ms
Nov 30, 09:18:20 AM: 77c95acc ERROR   ⨯ Error: Cannot find module 'next/dist/server/future/route-modules/pages/vendored/contexts/router-context'
Require stack:
- /var/task/node_modules/next/dist/client/compat/router.js
- /var/task/node_modules/next/compat/router.js
- /var/task/node_modules/@clerk/nextjs/dist/cjs/client-boundary/ClerkProvider.js
- /var/task/node_modules/@clerk/nextjs/dist/cjs/components.client.js
- /var/task/node_modules/@clerk/nextjs/dist/cjs/index.js
- /var/task/.next/server/pages/_app.js
- /var/task/node_modules/next/dist/server/require.js
- /var/task/node_modules/next/dist/server/next-server.js
- /var/task/.netlify/functions-internal/___netlify-handler/___netlify-handler.js
- /var/task/___netlify-handler.js
- /var/runtime/index.mjs
    at Module._resolveFilename (node:internal/modules/cjs/loader:1077:15)
    at /var/task/node_modules/next/dist/server/require-hook.js:54:36
    at Module._load (node:internal/modules/cjs/loader:922:27)
    at Module.require (node:internal/modules/cjs/loader:1143:19)
    at mod.require (/var/task/node_modules/next/dist/server/require-hook.js:62:32)
    at require (node:internal/modules/cjs/helpers:119:18)
    at Object.<anonymous> (/var/task/node_modules/next/dist/client/compat/router.js:12:37)
    at Module._compile (node:internal/modules/cjs/loader:1256:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1310:10)
    at Module.load (node:internal/modules/cjs/loader:1119:32) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    '/var/task/node_modules/next/dist/client/compat/router.js',
    '/var/task/node_modules/next/compat/router.js',
    '/var/task/node_modules/@clerk/nextjs/dist/cjs/client-boundary/ClerkProvider.js',
    '/var/task/node_modules/@clerk/nextjs/dist/cjs/components.client.js',
    '/var/task/node_modules/@clerk/nextjs/dist/cjs/index.js',
    '/var/task/.next/server/pages/_app.js',
    '/var/task/node_modules/next/dist/server/require.js',
    '/var/task/node_modules/next/dist/server/next-server.js',
    '/var/task/.netlify/functions-internal/___netlify-handler/___netlify-handler.js',
    '/var/task/___netlify-handler.js',
    '/var/runtime/index.mjs'
  ]
}
Nov 30, 09:18:20 AM: 77c95acc ERROR   ⨯ Error: Cannot find module 'next/dist/server/future/route-modules/pages/vendored/contexts/router-context'
Require stack:
- /var/task/node_modules/next/dist/client/compat/router.js
- /var/task/node_modules/next/compat/router.js
- /var/task/node_modules/@clerk/nextjs/dist/cjs/client-boundary/ClerkProvider.js
- /var/task/node_modules/@clerk/nextjs/dist/cjs/components.client.js
- /var/task/node_modules/@clerk/nextjs/dist/cjs/index.js
- /var/task/.next/server/pages/_app.js
- /var/task/node_modules/next/dist/server/require.js
- /var/task/node_modules/next/dist/server/next-server.js
- /var/task/.netlify/functions-internal/___netlify-handler/___netlify-handler.js
- /var/task/___netlify-handler.js
- /var/runtime/index.mjs
    at Module._resolveFilename (node:internal/modules/cjs/loader:1077:15)
    at /var/task/node_modules/next/dist/server/require-hook.js:54:36
    at Module._load (node:internal/modules/cjs/loader:922:27)
    at Module.require (node:internal/modules/cjs/loader:1143:19)
    at mod.require (/var/task/node_modules/next/dist/server/require-hook.js:62:32)
    at require (node:internal/modules/cjs/helpers:119:18)
    at Object.<anonymous> (/var/task/node_modules/next/dist/client/compat/router.js:12:37)
    at Module._compile (node:internal/modules/cjs/loader:1256:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1310:10)
    at Module.load (node:internal/modules/cjs/loader:1119:32) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    '/var/task/node_modules/next/dist/client/compat/router.js',
    '/var/task/node_modules/next/compat/router.js',
    '/var/task/node_modules/@clerk/nextjs/dist/cjs/client-boundary/ClerkProvider.js',
    '/var/task/node_modules/@clerk/nextjs/dist/cjs/components.client.js',
    '/var/task/node_modules/@clerk/nextjs/dist/cjs/index.js',
    '/var/task/.next/server/pages/_app.js',
    '/var/task/node_modules/next/dist/server/require.js',
    '/var/task/node_modules/next/dist/server/next-server.js',
    '/var/task/.netlify/functions-internal/___netlify-handler/___netlify-handler.js',
    '/var/task/___netlify-handler.js',
    '/var/runtime/index.mjs'
  ]
}
Nov 30, 09:18:20 AM: 77c95acc INFO   [GET] /api/auth/whoami (SSR)
Nov 30, 09:18:20 AM: 77c95acc Duration: 140.97 ms	Memory Usage: 101 MB
Nov 30, 09:18:23 AM: f07e28aa ERROR   ⨯ Error: Cannot find module 'next/dist/server/future/route-modules/pages/vendored/contexts/router-context'
Require stack:
- /var/task/node_modules/next/dist/client/compat/router.js
- /var/task/node_modules/next/compat/router.js
- /var/task/node_modules/@clerk/nextjs/dist/cjs/client-boundary/ClerkProvider.js
- /var/task/node_modules/@clerk/nextjs/dist/cjs/components.client.js
- /var/task/node_modules/@clerk/nextjs/dist/cjs/index.js
- /var/task/.next/server/pages/_app.js
- /var/task/node_modules/next/dist/server/require.js
- /var/task/node_modules/next/dist/server/next-server.js
- /var/task/.netlify/functions-internal/___netlify-handler/___netlify-handler.js
- /var/task/___netlify-handler.js
- /var/runtime/index.mjs
    at Module._resolveFilename (node:internal/modules/cjs/loader:1077:15)
    at /var/task/node_modules/next/dist/server/require-hook.js:54:36
    at Module._load (node:internal/modules/cjs/loader:922:27)
    at Module.require (node:internal/modules/cjs/loader:1143:19)
    at mod.require (/var/task/node_modules/next/dist/server/require-hook.js:62:32)
    at require (node:internal/modules/cjs/helpers:119:18)
    at Object.<anonymous> (/var/task/node_modules/next/dist/client/compat/router.js:12:37)
    at Module._compile (node:internal/modules/cjs/loader:1256:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1310:10)
    at Module.load (node:internal/modules/cjs/loader:1119:32) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    '/var/task/node_modules/next/dist/client/compat/router.js',
    '/var/task/node_modules/next/compat/router.js',
    '/var/task/node_modules/@clerk/nextjs/dist/cjs/client-boundary/ClerkProvider.js',
    '/var/task/node_modules/@clerk/nextjs/dist/cjs/components.client.js',
    '/var/task/node_modules/@clerk/nextjs/dist/cjs/index.js',
    '/var/task/.next/server/pages/_app.js',
    '/var/task/node_modules/next/dist/server/require.js',
    '/var/task/node_modules/next/dist/server/next-server.js',
    '/var/task/.netlify/functions-internal/___netlify-handler/___netlify-handler.js',
    '/var/task/___netlify-handler.js',
    '/var/runtime/index.mjs'
  ]
}
Nov 30, 09:18:23 AM: f07e28aa ERROR   ⨯ Error: Cannot find module 'next/dist/server/future/route-modules/pages/vendored/contexts/router-context'
Require stack:
- /var/task/node_modules/next/dist/client/compat/router.js
- /var/task/node_modules/next/compat/router.js
- /var/task/node_modules/@clerk/nextjs/dist/cjs/client-boundary/ClerkProvider.js
- /var/task/node_modules/@clerk/nextjs/dist/cjs/components.client.js
- /var/task/node_modules/@clerk/nextjs/dist/cjs/index.js
- /var/task/.next/server/pages/_app.js
- /var/task/node_modules/next/dist/server/require.js
- /var/task/node_modules/next/dist/server/next-server.js
- /var/task/.netlify/functions-internal/___netlify-handler/___netlify-handler.js
- /var/task/___netlify-handler.js
- /var/runtime/index.mjs
    at Module._resolveFilename (node:internal/modules/cjs/loader:1077:15)
    at /var/task/node_modules/next/dist/server/require-hook.js:54:36
    at Module._load (node:internal/modules/cjs/loader:922:27)
    at Module.require (node:internal/modules/cjs/loader:1143:19)
    at mod.require (/var/task/node_modules/next/dist/server/require-hook.js:62:32)
    at require (node:internal/modules/cjs/helpers:119:18)
    at Object.<anonymous> (/var/task/node_modules/next/dist/client/compat/router.js:12:37)
    at Module._compile (node:internal/modules/cjs/loader:1256:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1310:10)
    at Module.load (node:internal/modules/cjs/loader:1119:32) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    '/var/task/node_modules/next/dist/client/compat/router.js',
    '/var/task/node_modules/next/compat/router.js',
    '/var/task/node_modules/@clerk/nextjs/dist/cjs/client-boundary/ClerkProvider.js',
    '/var/task/node_modules/@clerk/nextjs/dist/cjs/components.client.js',
    '/var/task/node_modules/@clerk/nextjs/dist/cjs/index.js',
    '/var/task/.next/server/pages/_app.js',
    '/var/task/node_modules/next/dist/server/require.js',
    '/var/task/node_modules/next/dist/server/next-server.js',
    '/var/task/.netlify/functions-internal/___netlify-handler/___netlify-handler.js',
    '/var/task/___netlify-handler.js',
    '/var/runtime/index.mjs'
  ]
}
Nov 30, 09:18:23 AM: f07e28aa INFO   [GET] /api/auth/whoami (SSR)
Nov 30, 09:18:23 AM: f07e28aa Duration: 23.19 ms	Memory Usage: 101 MB```

</details>


### .next JSON files

<details>
  <summary>generated .next JSON files</summary>

Paste file contents here. Please check there isn't any private info in them

You can either build locally, or download the deploy from Netlify by clicking the arrow next to the deploy time.


</details>
smyja commented

I am having the same issue

Based on this comment #2329 (comment)

[functions]
included_files = [
  "node_modules/next/dist/server/future/route-modules/pages/vendored/contexts/router-context*",
  "node_modules/next/dist/server/future/route-modules/pages/vendored/contexts/amp-context*",
  "node_modules/next/dist/server/future/route-modules/pages/vendored/contexts/head-manager-context*",
]

this should do the trick for you

@dimitrisnl Thanks, works very well.