Package cannot be found when doing API request

Netlify Site: clever-panda-a35c8f.netlify.app

I deployed a Nuxt 3 application that consist of a simple application where authenticated users will be able to see “Content Lessons”. In this content Lessons, I call an API route if the user started watching a video. This API route uses Nuxt’s api folder, so the route can be reached under the path /api/saveProgress.

When doing this request on my deployed application, I receive the following error:

Cannot find package '@sanity/client' imported from /var/task/chunks/saveProgress.post.mjs

Checking the functions logs, I also see something similar

Aug 30, 12:53:32 AM: 6acd0ab8 ERROR  [nuxt] [request error] [unhandled] [500] Cannot find package '@sanity/client' imported from /var/task/chunks/saveProgress.post.mjs
Aug 30, 12:53:32 AM: 6acd0ab8 ERROR    at new NodeError (node:internal/errors:387:5)
Aug 30, 12:53:32 AM: 6acd0ab8 ERROR    at packageResolve (node:internal/modules/esm/resolve:852:9)
Aug 30, 12:53:32 AM: 6acd0ab8 ERROR    at moduleResolve (node:internal/modules/esm/resolve:901:20)
Aug 30, 12:53:32 AM: 6acd0ab8 ERROR    at defaultResolve (node:internal/modules/esm/resolve:1115:11)
Aug 30, 12:53:32 AM: 6acd0ab8 ERROR    at nextResolve (node:internal/modules/esm/loader:163:28)
Aug 30, 12:53:32 AM: 6acd0ab8 ERROR    at ESMLoader.resolve (node:internal/modules/esm/loader:841:30)
Aug 30, 12:53:32 AM: 6acd0ab8 ERROR    at ESMLoader.getModuleJob (node:internal/modules/esm/loader:424:18)
Aug 30, 12:53:32 AM: 6acd0ab8 ERROR    at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:76:40)
Aug 30, 12:53:32 AM: 6acd0ab8 ERROR    at link (node:internal/modules/esm/module_job:75:36)

I understand that Netlify bundles the dependencies for the functions, if they are imports of the function itself. This API has the characteristic that uses @nuxtjs/sanity package, which is a wrapper of Sanity for Nuxt, and exposes a hook called useSanity() which is already exposed on Nuxt APIs (not explicit require needed).

If my understanding is correct, then the issue is because Netlify can’t detect the dependency, and is not bundled with the function. But I want to see from your perspective if there is anything I’m missing that I should add for this to work.

Appreciate your help.

If none of your function files are explicitly importing @sanity/client, it won’t be bundled. You can solve that by adding:

[functions]
  external_node_modules = [
    "@sanity/client"
  ]

to your netlify.toml.

Hey @hrishikesh. Thanks for your response.

I had already tried the solution you proposed, but is not working.
My netlify.toml looks like this:

[functions]
  external_node_modules = ["@sanity/client"]

This error is also happening when loading specific routes. For example, my app has 3 routes:

  • / (home)
  • /lecciones (dashboard)
  • /lecciones/:id (lesson)

If I go from home to /lecciones:id, I’m able to see the page. But if I try reloading the lesson page, it fails with a similar error:

Without reloading

Reloading
image

That’s weird. What happens if you change your toml to:

[functions]
  included_files = [
    "./node_modules/@sanity/client/**/*"
  ]

Hey @hrishikesh .

Thanks, that fixed the issue :smile: . What could be the reason that this one worked but the external_node_modules didn’t?