Netlify functions "Cannot find module "

I have deployed my repo on other Netlify sites and the Netlify functions worked quite well. But now when I’m deploying into a new site, I’m getting that it cannot find a module which it was able to during previous builds.

If you seethe logs of the locked deploy of the site sleepy-roentgen-3eb28f, the Netlify function is deployed successfully but using it in site hackmum, its failing…Even on the Netlify on my dev.

Update: I had forked the upstream and deployed it gracious-easley-96bc3f
and it works.

And then I pulled the forked clone repo locally and ran netlify dev and I get the same error:
Error during invocation: {
errorMessage: “Cannot find module ‘phin’\n” +
‘Require stack:\n’ +
‘- /…/testNetlify/functions/ga.js\n’ +

Hi @indcoder,

This error means that testNetlify/functions/ga.js is doing a require('phin'), but the phin dependency cannot be found. This could happen if you forgot to add phin to your package.json dependencies. Another reason could be that you might be using a package.json inside your functions directory, where a single top-level package.json should be used instead.

I hope this solves your problem.

You are right, there’s a top level package.json for the eleventy blog. But my contention is the inconsistency:

  1. The original git repo has the same two package.json [one at blog level, other at the function folder level] which is successfully deployed [Netlify site: gracious-easley-96bc3]

  2. My previous deploys were successful but it started failing two days back. [Netlify site : sleepy-roentgen-3eb28f]

  3. Even when I move that dependency on to top package.json, while there are no Ntelify deployment errors, the function is not available. [detected via Browser console errors]

Hey @indcoder,
Wanted to check in and see if you are still having trouble getting your function to build?

Hi Jen,

Thanks for checking in. Its working after putting those packages in the top level package.json

But the major points I raised in my OP were inconsistency. i.e. a codebase that gets deployed when going through Netlify website while on Netlify Dev Cli that same codebase errors out. And that same codebase erroring out the same way when deployed on another site through the Netlify website.

Ah yes, I hear you on that. Consistency across all the many ways of interfacing with Netlify is something we’re always striving for, but there are sometimes gaps. If you wouldn’t mind sharing the Netlify Dev command you used to deploy, that would help us understand this discrepancy better!

Please check the GitHub URL…I’m using a netlify.toml file for my deploys and on the local machine, I’m validating it with Netlify CLI

❯ netlify dev
◈ Netlify Dev ◈
◈ Functions server is listening on 39979
◈ Starting Netlify Dev with eleventy

Hi,

The error Cannot find module ‘phin’ is pretty specific to your function not finding the dependency. I do show that you’ve add the ‘phin’ package in the main package.json. Not sure what else changed but I do see that your more recent builds are succeeding. Did you figure things out?

The package.json at the root of my codebase is for a static website, so I don’t want to bundle server side packages in there.

But it seems you are saying this is a requirement for my functions to bundle the packages?

In Netlify’s function-deploy-test example, it shows functions having their own package.json files. I’ve emulated this approach, finding the functions work fine locally yet are missing when deployed to Netlify.

Hopefully I’m misunderstanding something, and I don’t need to put a bunch of server dependencies into my static website. Please confirm, thanks.

(Edit: I’ve just looked at the package.json in the root of that example and see the dependencies are included there also. Is this redundancy necessary?)

(Edit 2: I tried adding the missing dependency to the root package.json too and it is still missing. SO I’m guessing I’m on the wrong track here and there’s another reason the package (nodemailer) is not bundling).

Bumping this up… I’m having similar issues.

message: “FetchError: Cannot find module ‘./0.js’\n” +
‘Require stack:\n’ +
‘- /var/task/.js\n’ +
‘- /var/runtime/index.mjs’,
details: ‘’,
hint: ‘’,
code: ‘MODULE_NOT_FOUND’

This runs and builds fine locally. Was previously working on the deployed version. Suddenly stopped.

Hey there, @rubyswapteam :wave:

Thanks for reaching out about this! We will need a few more details in order to look into this further. Can you please share your site, your function name, and where you are seeing this error message? Thank you.