NextJs API - cannot find module 'formidable' - works locally

Netlify site: https://bt-platform.netlify.app/

We have a Nextjs API that accepts a file in the body and parses it via formidable (which is in our
package.json). Everything is working great locally.

Here’s the api code:

import formidable, { File } from 'formidable';
//^ throws error
import type { NextApiRequest, NextApiResponse } from 'next';

export const config = { api: { bodyParser: false } };

export default async function handler(req: NextApiRequest, res: NextApiResponse<any>) {
  console.log("never get's executed");
  const form = new formidable.IncomingForm();
   // ... form parsing code  
}

Our console.logs never get run, so it looks like it’s erroring immediately when the function is invoked.

We get the following error from the netlify logs:

Nov 2, 01:40:13 PM: edc7c92f ERROR Error: Cannot find module 'formidable’Nov 2, 01:40:13 PM: edc7c92f ERROR Require stack:Nov 2, 01:40:13 PM: edc7c92f ERROR - /var/task/.next/server/pages/api/content/[contentId]/files/upload.jsNov 2, 01:40:13 PM: edc7c92f ERROR - /var/task/node_modules/next/dist/server/next-server.jsNov 2, 01:40:13 PM: edc7c92f ERROR - /var/task/.netlify/functions-internal/_api_content_contentId-PARAM_files_fileId-PARAM-handler/handlerUtils.jsNov 2, 01:40:13 PM: edc7c92f ERROR - /var/task/.netlify/functions-internal/_api_content_contentId-PARAM_files_fileId-PARAM-handler/_api_content_contentId-PARAM_files_fileId-PARAM-handler.jsNov 2, 01:40:13 PM: edc7c92f ERROR - /var/task/_api_content_contentId-PARAM_files_fileId-PARAM-handler.jsNov 2, 01:40:13 PM: edc7c92f ERROR - /var/runtime/index.mjsNov 2, 01:40:13 PM: edc7c92f ERROR at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)Nov 2, 01:40:13 PM: edc7c92f ERROR at Function.mod._resolveFilename (/var/task/node_modules/next/dist/build/webpack/require-hook.js:27:32)Nov 2, 01:40:13 PM: edc7c92f ERROR at Function.Module._load (node:internal/modules/cjs/loader:778:27)Nov 2, 01:40:13 PM: edc7c92f ERROR at Module.require (node:internal/modules/cjs/loader:1005:19)Nov 2, 01:40:13 PM: edc7c92f ERROR at require (node:internal/modules/cjs/helpers:102:18)Nov 2, 01:40:13 PM: edc7c92f ERROR at Object.2616 (/var/task/.next/server/pages/api/content/[contentId]/files/upload.js:39:18)Nov 2, 01:40:13 PM: edc7c92f ERROR at webpack_require (/var/task/.next/server/webpack-api-runtime.js:25:42)Nov 2, 01:40:13 PM: edc7c92f ERROR at Object.7455 (/var/task/.next/server/pages/api/content/[contentId]/files/upload.js:72:68)

Have tried the following:

  • empty cache and rebuild on netlify
  • delete yarn.lock and regenerate it via yarn
  • explicitly set our NODE_VERSION to be the same as local

Any ideas? The build process seems to be working and installing okay.

Hi @joe.roddy, kindly try the refactored code below to see if it works. The types import for req and res has been moved to the first line.


import type { NextApiRequest, NextApiResponse } from 'next';

import formidable from 'formidable';

export const config = { api: { bodyParser: false } };

export default async function handler(req: NextApiRequest, res: NextApiResponse<any>) {
  console.log("never get's executed");
  const form = new formidable.IncomingForm();
   // ... form parsing code  
}

If you try the above and it does not work, you can try out the package formidable-serverless to see if it helps.

Let me know the outcome.

Thanks

Hey clarnx!

Thanks a lot for the tips. Reordering imports gave the same error.

Sadly, neither worked, tried the serverless version and it said the same with the new package:
ERROR Error: Cannot find module 'formidable-serverless'

Going to try to import another random 3rd party package to see if its just a generic node_modules issue or something.

This issue only occurs when Netlify deploys your NextJs app as separate cloud functions for each API endpoint.

I’m not sure how it decides when it’s going to bundle them into one api or multiple, but this code works fine when it’s bundled into one api (___netlify_handler), and fails when it’s sharded into different cloud functions for every API endpoint.

Hi @joe.roddy thanks for the added feedback.
If possible can you share a repository of your app/site for me to help with the debugging?

Thanks.

Hey @joe.roddy,

With the release of v4.28.4 of Next.js Runtime, we’ve reverted the change of switching to individual function for each API route. This should solve the problem you’re seeing here as well as in the other thread. Let us know if it works.

Yep, working great now, thanks @hrishikesh .

Just to make sure, this issue is on the teams radar if you were going to redeploy the individual api functions? I don’t have to worry about my feature breaking again in the future, I’m assuming?

Yes, we’ve seen several issues like this with that particular change. So it has been reverted as of now. We would be working to re-enable this separation (as it provides several other benefits), but that would be a much more tested release, I assume - not to say this one wasn’t, but we’re still not sure what exactly has caused this.

1 Like

Awesome! Thanks so much for your help! <3