Error: Cannot find module Netlify functions using Nextjs

Hello I recently migrated my app from a create-react-app to nextjs. I have installed the Essential Next.js by netlify plugin for my project. However I’m unsure if I have structured my project correctly.

I have tried deploying the app today but am seeing an error when I navigate to the root page. There are no build errors but the error on the deployed app is:

{
"errorType": "Runtime.ImportModuleError",
"errorMessage": "Error: Cannot find module 'request'\nRequire stack:\n- /var/task/src/netlify/functions/next_index/nextPage/index.js\n- /var/task/src/netlify/functions/next_index/renderNextPage.js\n- /var/task/src/netlify/functions/next_index/next_index.js\n- /var/task/next_index.js\n- /var/runtime/UserFunction.js\n- /var/runtime/index.js",
"trace": [
"Runtime.ImportModuleError: Error: Cannot find module 'request'",
"Require stack:",
"- /var/task/src/netlify/functions/next_index/nextPage/index.js",
"- /var/task/src/netlify/functions/next_index/renderNextPage.js",
"- /var/task/src/netlify/functions/next_index/next_index.js",
"- /var/task/next_index.js",
"- /var/runtime/UserFunction.js",
"- /var/runtime/index.js",
"    at _loadUserApp (/var/runtime/UserFunction.js:100:13)",
"    at Object.module.exports.load (/var/runtime/UserFunction.js:140:17)",
"    at Object.<anonymous> (/var/runtime/index.js:43:30)",
"    at Module._compile (internal/modules/cjs/loader.js:999:30)",
"    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)",
"    at Module.load (internal/modules/cjs/loader.js:863:32)",
"    at Function.Module._load (internal/modules/cjs/loader.js:708:14)",
"    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12)",
"    at internal/main/run_main_module.js:17:47"
]
}

My package.json for react and Next is:

"next": "^10.1.3",
"react": "^16.14.0"

Any help with this would be greatly appreciated.

Thanks.

Hey there, @Huweey :wave:

Welcome to the Netlify Forums :netliconfetti: Thanks for sharing these details.

Before we dive in further, can you share your Netlify site id with us, as well as your github repo if it is public? Additionally, have you taken any other debugging steps since posting this four days ago?

Hi Hillary,

I have deployed a branch of my repo specifically for this issue which can be found at: https://607b2c721e586f0007d9ee7c--festive-pasteur-500d8c.netlify.app/

The repo is not public but I’m happy to share whatever config or other details you might need.

My package.json looks like:


“next”: “^10.0.6”,
“react”: “^16.14.0”

I have a Netlify.toml that looks like

[build]
command = “npm run build”
publish = “out”

[[plugins]]
package = “@netlify/plugin-nextjs”

[context.production.environment]
NEXT_SERVERLESS = “true”
NODE_ENV = “production”

and a next.config.js:

module.exports = {
// Target must be serverless
target: “serverless”,
};

Howdy @Huweey,
That Next setup looks reasonable to me! The error mentions the request module- does that exist in your package.json?

1 Like

Hi Jen, No it isn’t in my package.json, the only reference I can find of it in the project is in my package-lock.json as a dependency of the microsoft-signalr package.

@microsoft/signalr”: {
“version”: “3.1.7”,
“resolved”: “https://registry.npmjs.org/@microsoft/signalr/-/signalr-3.1.7.tgz”,
“integrity”: “sha512-CC0sQom5SanAy7K6ueAmuWfsWcIv+mIoqUO3qXguPK7YUGx1O2geHgv567Gb4T52yWEXo5UQ1TCeDJecEnbSEg==”,
“requires”: {
“eventsource”: “^1.0.7”,
“request”: “^2.88.0”,
“ws”: “^6.0.0”
},

Please tell me if that’s incorrect but I didn’t think I needed dependencies directly in my package.json.

Edit: If it’s of any use, I import the @microsoft/signalr package in my /pages/index.js file and connect to a hub via WebSockets in a useEffect React hook.

Kind regards,
Huw

Hey @Huweey,
Ah, we use the package.json to know what dependencies to install in the build container for your build. Given the error you’re seeing, I’d try adding request to the package.json, triggering another build, and seeing what happens- I’ll be curious if you then get similar errors about missing the ws module so please let us know how it goes.

Hi Jen, thanks for the response.

I’ve added request to the package.json (“request”: “^2.88.2”)

Unfortunately the error message appears to be the same. You can see my latest build here: https://6081ed3d800102000733b441--festive-pasteur-500d8c.netlify.app/

Hi @Huweey :wave:

Thanks for sharing those details.

I have taken this to some of our Nextjs experts, and there are two next steps here.

  1. We will need a public repo we can follow so that we can reproduce this. You can create a new repo and share it in this thread. Would you be open to this?
  2. You can try esbuild via
    [functions]
    node_bundler = "esbuild"

Thank you!
Hillary

Hey there, is this problem got solved ? I am stuck to same problem.

Hey there, @rautNitesh :wave:

Sorry to hear you are encountering obstacles. Can you share your site URL and your repo please? Additionally, can you share what debugging steps you have taken?

Same here!

This error just happens in the Netlify build.

In machine (develop mode/localhost) and in other alternatives to online deploy like Vercel it doesn’t happen.

I’m trying some resolutions and will share that here.

Thank you!

edit: it looks to be an issue of incompatibility of next 11.0.0 (or older) with new versions of netlify plugins.
you can read more in this github issue: [Bug]: Updating plugin to v4 causes 502 error on some routes · Issue #1103 · netlify/netlify-plugin-nextjs · GitHub

And this is another discussion of an error like yours here:

hey there,

before we dig in, is updating to a newer version of next a possibility at all?

Hey, @perry!

Yeah, this is a possibility.

But like, my app is a little big right now, I’ve already tried to just update the version on the package.json (from 11.0.0 to 12.0.0) but it seems to cause a lot of bugs in the app, so it’s something that needs to be updated in others parts of the code (read the next update documentation etc)…
Well… It’s need time and not all kind of application can have this update… So maybe I will wait for an resolution of netlify engineers <3

Thankss!

Hey @vitorregisrr,

I think a more relevant GitHub issue is this one:

You can subscribe to that to keep up with the progress in that one.