Skip to content
This repository has been archived by the owner on Jan 28, 2025. It is now read-only.

Error: Cannot find module 'next/dist/next-server/server/node-polyfill-fetch' #440

Closed
standingdreams opened this issue Jun 12, 2020 · 34 comments

Comments

@standingdreams
Copy link

While deploying a Next.js app to AWS using Serverless, I get a 503 error. When I drill down into the logs, I find the error I'm recieving is Error: Cannot find module 'next/dist/next-server/server/node-polyfill-fetch'.

I've tried uninstalling and reinstalling. I was using the latest version of Next. I tried to downgrade to 9.3.4. I've tried to downgrade Serverless Next.js. I've tried downgrading Node from 12 to 10 in Cloudfront. Any help would be greatly appreciated.

Please let me know if there is any other information needed to help with this issue.

"serverless-next.js": "^1.13.0"
"next": "^9.3.4"

Full error message

{
    "errorType": "Error",
    "errorMessage": "Cannot find module 'next/dist/next-server/server/node-polyfill-fetch'\nRequire stack:\n- /var/task/pages/_error.js\n- /var/task/index.js\n- /var/runtime/UserFunction.js\n- /var/runtime/index.js",
    "code": "MODULE_NOT_FOUND",
    "requireStack": [
        "/var/task/pages/_error.js",
        "/var/task/index.js",
        "/var/runtime/UserFunction.js",
        "/var/runtime/index.js"
    ],
    "stack": [
        "Error: Cannot find module 'next/dist/next-server/server/node-polyfill-fetch'",
        "Require stack:",
        "- /var/task/pages/_error.js",
        "- /var/task/index.js",
        "- /var/runtime/UserFunction.js",
        "- /var/runtime/index.js",
        "    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:957:15)",
        "    at Function.Module._load (internal/modules/cjs/loader.js:840:27)",
        "    at Module.require (internal/modules/cjs/loader.js:1019:19)",
        "    at require (internal/modules/cjs/helpers.js:77:18)",
        "    at Object.mctB (/var/task/pages/_error.js:269:18)",
        "    at __webpack_require__ (/var/task/pages/_error.js:23:31)",
        "    at Module.rMRU (/var/task/pages/_error.js:321:106)",
        "    at __webpack_require__ (/var/task/pages/_error.js:23:31)",
        "    at /var/task/pages/_error.js:91:18",
        "    at Object.<anonymous> (/var/task/pages/_error.js:94:10)"
    ]
}
@danielcondemarin
Copy link
Contributor

Hi could you post your serverless.yml as well?

@standingdreams
Copy link
Author

@danielcondemarin Hello. Please see below:

serverless.yml

tweenshot:
  component: "serverless-next.js"

@lone-cloud
Copy link
Contributor

This sounds like the new fetch polyfill they shipped with 9.4: https://nextjs.org/blog/next-9-4#improved-built-in-fetch-support. I am not sure why it's breaking for you now, but I imagine #431 could fix your issue. Note that it hasn't been released yet. You can test it by cloning this repo locally, building it like is described in CONTRIBUTING.md then pointing to its local path in your serverless.yml like component: '../serverless-next.js/packages/serverless-component'

@standingdreams
Copy link
Author

@lone-cloud Am I correct in assuming that version 1.14.0-alpha.0 has the branch you mention above in it?

@lone-cloud
Copy link
Contributor

Yes, I'm fairly confident (I didn't create the release) that's the case.

@standingdreams
Copy link
Author

@lone-cloud I'm getting the same error with the release and with the branch.

@danielcondemarin
Copy link
Contributor

@standingdreams Could you try using useServerlessTraceTarget documented in the inputs?
Also could you post your next.config.js

@standingdreams
Copy link
Author

@danielcondemarin: please see below.

next.config.js

require("dotenv").config({
  path: `.env`,
})

const webpack = require('webpack')
const nodeExternals = require('webpack-node-externals');

module.exports = {
  target: "serverless",
  webpack(config, options) {
    config.externals = config.externals || [];
    config.module.rules.push({
      test: /\.(png|jpg|gif|svg|eot|ttf|woff|woff2)$/,
      use: {
        loader: 'url-loader',
        options: {
          limit: 100000
        }
      }
    });
    config.externals.push(nodeExternals());
    config.plugins.push(
      new webpack.EnvironmentPlugin(process.env)
    )
    return config;
  },
  publicRuntimeConfig: {
    BASEURL: process.env.BASEURL,
    GA_TRACKING_ID: process.env.GA_TRACKING_ID,
  }
};

@standingdreams
Copy link
Author

standingdreams commented Jun 13, 2020

@danielcondemarin When I add useServerlessTraceTarget, i get the following error:

  InvalidRequest: S3 Transfer Acceleration is not configured on this bucket
    at Request.extractError (/Users/standingdreams/.serverless/components/registry/npm/serverless-next.js@1.14.0-alpha.0/node_modules/aws-sdk/lib/services/s3.js:831:35)
    at Request.callListeners (/Users/standingdreams/.serverless/components/registry/npm/serverless-next.js@1.14.0-alpha.0/node_modules/aws-sdk/lib/sequential_executor.js:106:20)
    at Request.emit (/Users/standingdreams/.serverless/components/registry/npm/serverless-next.js@1.14.0-alpha.0/node_modules/aws-sdk/lib/sequential_executor.js:78:10)
    at Request.emit (/Users/standingdreams/.serverless/components/registry/npm/serverless-next.js@1.14.0-alpha.0/node_modules/aws-sdk/lib/request.js:683:14)
    at Request.transition (/Users/standingdreams/.serverless/components/registry/npm/serverless-next.js@1.14.0-alpha.0/node_modules/aws-sdk/lib/request.js:22:10)
    at AcceptorStateMachine.runTo (/Users/standingdreams/.serverless/components/registry/npm/serverless-next.js@1.14.0-alpha.0/node_modules/aws-sdk/lib/state_machine.js:14:12)
    at /Users/standingdreams/.serverless/components/registry/npm/serverless-next.js@1.14.0-alpha.0/node_modules/aws-sdk/lib/state_machine.js:26:10
    at Request.<anonymous> (/Users/standingdreams/.serverless/components/registry/npm/serverless-next.js@1.14.0-alpha.0/node_modules/aws-sdk/lib/request.js:38:9)
    at Request.<anonymous> (/Users/standingdreams/.serverless/components/registry/npm/serverless-next.js@1.14.0-alpha.0/node_modules/aws-sdk/lib/request.js:685:12)
    at Request.callListeners (/Users/standingdreams/.serverless/components/registry/npm/serverless-next.js@1.14.0-alpha.0/node_modules/aws-sdk/lib/sequential_executor.js:116:18) {
  code: 'InvalidRequest',
  region: null,
  time: 2020-06-12T23:07:54.442Z,
  requestId: '6A356F94AA8A9008',
  extendedRequestId: 'K+ltn4evuhy4pqYkX5z/LVmJ8jSnCMk6zr+F+BxxeExPj7mlXax04OPWjAW1D1czydmMa+EsRtc=',
  cfId: '8ituGD5bXwKExzEr5boG-IU6Q28X-xryUXfKdwsAju2Yelz1A-ELyA==',
  statusCode: 400,
  retryable: false,
  retryDelay: 30.274555259984126
}

When I remove that line from serverless.yml, it deploys again and I go back to having the old error.

@danielcondemarin
Copy link
Contributor

I've had intermittent reports of InvalidRequest: S3 Transfer Acceleration is not configured on this bucket before but haven't got to the bottom of it. Under your bucket Properties can you see acceleration enabled?
image

@standingdreams
Copy link
Author

@danielcondemarin Yes. It's enabled.

@eenagy
Copy link

eenagy commented Jun 16, 2020

@standingdreams are you using yarn workspace?

@standingdreams
Copy link
Author

@eenagy No, just NPM. Do you think Yarn would produce different results?

@eenagy
Copy link

eenagy commented Jun 16, 2020

@standingdreams no, it's not related to yarn, but having using workspace on my side, I have a similar issue when node_modules is not deploying correctly.

@standingdreams
Copy link
Author

@danielcondemarin @lone-cloud @eenagy
I've isolated the issue to when getInitialProps or getServerSideProps are being used on a page. I've tried both and if I remove either from my homepage, the site loads with no issue. I've tested it returning hard coded values and it still does not properly load the homepage. I have not attempted to use getStaticProps but don't need that.

@lone-cloud
Copy link
Contributor

Yes, that makes sense as the polyfill in your stack trace would only be loaded on the server. If you don't define those functions, your code will not be rendered on the server.
I suspect you might be able to solve your issue by deleting your _error.js. The error seems to originate from there, but _error.js does not get used post-deploy (create a 404.js instead).
I don't believe anyone will be able to help you further without a way to reproduce the issue.

@andrewgadziksonos
Copy link
Collaborator

I'm also using yarn workspaces and was able to reproduce this error on dynamic routes. The fix was to add this to my package.json file in specific nextjs package in my monorepo

"workspaces": {
    "nohoist": [
      "next"
    ]
  },

@ivanpetrushev
Copy link

ivanpetrushev commented Jul 27, 2020

Hey, guys. I am getting the same with:
"next": "^9.4.4",
"@sls-next/serverless-component@1.15.0-alpha.5"

Cannot find module 'next/dist/next-server/server/node-polyfill-fetch

workspaces.nohoist doesn't help much. If I open the deployed Lambda function code, visually it looks the same as before.
The only file in /node_modules is /node_modules/@sls-next/next-aws-cloudfront/index.js

I am doing yarn insall before deploying with serverless --debug

serverless.yml:

prj:
  component: "@sls-next/serverless-component@1.15.0-alpha.5"
  inputs:
    bucketName: prj-xxx-dev
    bucketRegion: eu-central-1
    useServerlessTraceTarget: true
    nextConfigDir: ../../
    build:
      cwd: ../../
      env:
        NEXT_PUBLIC_PORTAL: prj-xxx
        NEXT_PUBLIC_HIDE_EXTRA_OUTPUT: true
        NEXT_PUBLIC_STORYBLOK_DEVMODE: false

@teemuniiranen
Copy link

I confirmed this one. You can also copy the configuration files to root before running npx serverless on CI/CD. This makes nextConfigDir & userServerlessTraceTarget unusable together. Please fix! :)

@DeerajTheepshi
Copy link

DeerajTheepshi commented Jun 26, 2021

Is there a solution for this thread yet?
I'm facing similar issues.
I'm facing the issues with @sls-next/serverless-component@1.18.0

@dphang
Copy link
Collaborator

dphang commented Jun 26, 2021

@DeerajTheepshi sorry I had not looked at this issue since it was older (need to clean up more old issues some time...). It sounds like a bundling issue, is it a problem on Vercel as well?

What Next.js version are you using? Is it reproducible on the latest version of Next.js? I know they changed how they bundle things somewhere around 10.2, so it might have been fixed in later versions? If you could provide a repro I can take a look, maybe we can just try to include the node-polyfill-fetch package in this component. Also, note you are using a slightly older component version, although I don't believe anything was done in this component to help mitigate the issue, so it probably does not matter.

@DeerajTheepshi
Copy link

We are using Next version : 10.2, and I'm not able to share the repo as it's private and belongs to a work organisation. I will try to make a minimal version of it.

As mentioned above the issue happens whenever we try to run any kind of server code. Will try upgrading next to a higher version.

@DeerajTheepshi
Copy link

DeerajTheepshi commented Jun 28, 2021

After going through cloudwatch logs, I tried removing the dependency line from the file where the error was thrown. The dependency was unused, so I tried it. But after that it threw "Module Not Found" error on a different package, namely : next/dist/next-server/lib/utils.js.

On upgrading next to 11.0.1, I got the following error:
Cannot find module '../chunks/251.js

I also forgot to add one important note, that this happens for an ISR page. The error logs are from the SQS triggered lambda.

Any idea on what may be happening?

@dphang
Copy link
Collaborator

dphang commented Jun 28, 2021

@DeerajTheepshi please confirm you are using the latest component version? The missing chunks issue should have been solved as in some versions they were not included incorrectly, I think some patch version of 10.2 started splitting chunks differently. So we had to start including those chunks as well.

I am not sure why there are next/dist/next-server/lib/utils.js or next/dist/next-server/lib/node-polyfill-fetch errors... guessing they are not bundled correctly into the page JS or chunks JS files by Next.js (could be bug on their end?), if it's hitting the error in a page JS file, then it shouldn't be caused by this component. But if you have a minimal repro you can share, it will be helpful to understand why it is happening.

One possible workaround is you can try to manually copy the next/dist/next-server files from your node_modules into the Lambda default-lambda using the postBuildCommands input. Although ideally it should already be bundled into the server-side chunks by Next.js..

@DeerajTheepshi
Copy link

DeerajTheepshi commented Jun 28, 2021

I can confirm from Template.json file that the version we are using is 3.1.0.

The development build and behavior of ISR pages are as expected in the local deployment. That's why I believe that the error may be from the component.

Here's what I observed, the SQS triggered lambda doesn't contain a node_modules/chunks folder at all, whereas the default_lambda has all the files as expected and that's probably why SSR works and only ISR fails.

Should I move this to #1098

@DeerajTheepshi
Copy link

So things worked from me when I added postBuildCommands to move node_modules and chunks folder from default_lambda folder to regeneration_lambda folder.

ISR is working fine as expected. My only doubt is, is this the expected setup ? Shouldn't the component do this ?

@dphang
Copy link
Collaborator

dphang commented Jun 28, 2021

@DeerajTheepshi yep, it is a bug you found in ISR, the component should handle if it's part of standard Next.js build.

I will try to reproduce it but from seeing the code here (

async buildRegenerationHandler(
) it seems to be copying same things as for the default lambda. If you could share a min repro it will be faster to debug. Thanks!

@dphang
Copy link
Collaborator

dphang commented Jun 28, 2021

@DeerajTheepshi from linked issue #1317 looks like there could be a similar root cause. I guess we need to check if there are some files not copied from .next but need to be.

@dphang
Copy link
Collaborator

dphang commented Jun 28, 2021

@DeerajTheepshi actually after doing some more testing on 10.2.0 I did notice regeneration lambda does not seem to copy the traced node modules correctly (although there is a call here

this.copyTraces(buildManifest),
), so that is definitely a bug on our end.

If you are using 10.2.0 Next.js hadn't changed the chunking yet, so serverless-trace is still supposed to use node_modules instead of bundling into chunks. Assuming you are using serverless trace target and it is failing?

I think if you can upgrade to 10.2.3 at least, its new chunking should solve these problems as well, although I am fine fixing here as well, since it should be a small fix and wasn't working correctly in the first place.

Have fixed copying node_modules for earlier versions of Next.js where serverless trace is possible: https://github.com/serverless-nextjs/serverless-next.js/pull/1319/checks?check_run_id=2936339320

If there is still an issue after this is published, let me know.

@DeerajTheepshi
Copy link

DeerajTheepshi commented Jun 29, 2021

@dphang , thanks for the quick fixes. Sorry I couldn’t reply earlier.

Yes we were using Serverless trace target. One thing to note is the missing chunks error happened when we upgraded to 11.

Will update once I test out the next release. Thanks

@afenton90
Copy link
Contributor

I came across this issue.

However, after upgrading to the following this bug has gone and Incremental Static Regeneration (ISR) is working:

  • @sls-next/cdk-construct@3.2.0-alpha.12
  • @sls-next/lambda-at-edge@3.2.0-alpha.12
  • aws-cdk@1.100.0
  • next@11.0.1

@dphang
Copy link
Collaborator

dphang commented Jul 7, 2021

Thanks for confirming, I think these sorts of issues should now be fixed (especially in latest versions where bundling from Next.js itself was improved, and also we should be including the node_modules correctly in the serverless trace mode). So I'll resolve this.

If it resurfaces, please open a new issue.

@dphang dphang closed this as completed Jul 7, 2021
@AsrarJarvis
Copy link

Hi, all facing similar issue when deploying my app to AWS it's throwing similar issue : Type error: Cannot find module 'next/dist/next-server/server/router' or its corresponding type declarations
Since I am here very new here can anyone help me please with this.
What causing this and how to fix it
Screenshot (106)
?

@dotsimplify
Copy link

Hi, all facing similar issue when deploying my app to AWS it's throwing similar issue : Type error: Cannot find module 'next/dist/next-server/server/router' or its corresponding type declarations Since I am here very new here can anyone help me please with this. What causing this and how to fix it Screenshot (106) ?

i got same error which i found after 4 hours of googling i get to know my IDE had imported by mistake , must be i pressed some keys , it was in my /pages/sitemap , error message was unclear so it took so long time to know exact reason of bug

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests