-
Notifications
You must be signed in to change notification settings - Fork 87
[Bug]: serverless function using "sharp" cannot find "sharp" module #1195
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I was able to fix my issue by using yarn package aliases, like
and then importing |
are there any better ideas on how to fix it? |
I'm also facing this issue. Are there any solutions other than creating an npm / yarn alias? |
So the issue is that What we did:
{
"name": "include_sharp",
"version": "1.0.0",
"main": "index.js"
}
name: include_sharp
module.exports = {
async onBuild({ netlifyConfig }) {
const fn = netlifyConfig.functions['___netlify-handler'];
fn.included_files = fn.included_files.filter(
(f) => f !== '!node_modules/sharp/**/*'
);
},
};
[[plugins]]
package = "@netlify/plugin-nextjs"
[[plugins]]
package = "./include_sharp" This way the sharp module is not excluded from the bundle anymore. This is kind of a hack, but works without additional external packages. |
I tried a variety of things including the |
#1745 |
Summary
If you include "sharp" as a dependency in your package.json in a Next.js serverless function, it will not be bundled into your deployed functions and you will get an error:
I believe this is because of the
DEFAULT_EXCLUDED_MODULES
declaration insrc/helpers/config.ts
.Steps to reproduce
I ran this on Typescript, but I assume the same problem holds for Javascript repos. Note, I am also using Yarn as a multi-package monorepo, if it's relevant.
sharp
is installed in one of the subpackages, not at the monorepo root.Make a file in
src/pages/api/myfail.ts
:A link to a reproduction repository
No response
Plugin version
4.2.4
More information about your build
netlify.toml
)What OS are you using?
Linux
Your netlify.toml file
`netlify.toml`
Your public/_redirects file
`_redirects`
# Paste content of your `_redirects` file here
Your
next.config.js
file`next.config.js`
# Paste content of your `next.config.js` file here. Check there is no private info in there.
Builds logs (or link to your logs)
Build logs
01:54:15 PM: c9f3c7a0 INFO [GET] /pixlies/17?chainId=80001 (SSR)
01:54:15 PM: c9f3c7a0 Duration: 41.14 ms Memory Usage: 145 MB 01:54:16 PM: 62255a1a ERROR Error: Cannot find module 'sharp'
Require stack:
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:902:15)
at Function.Module._load (internal/modules/cjs/loader.js:746:27)
at Module.require (internal/modules/cjs/loader.js:974:19)
at require (internal/modules/cjs/helpers.js:93:18)
at Module.9359 (/var/task/packages/frontend/.next/server/pages/api/pixlies/[tokenId]/png.js:20:40)
at webpack_require (/var/task/packages/frontend/.next/server/webpack-runtime.js:25:43)
at webpack_exec (/var/task/packages/frontend/.next/server/pages/api/pixlies/[tokenId]/png.js:126:39)
at /var/task/packages/frontend/.next/server/pages/api/pixlies/[tokenId]/png.js:127:78
at Function.webpack_require.X (/var/task/packages/frontend/.next/server/webpack-runtime.js:108:21)
at /var/task/packages/frontend/.next/server/pages/api/pixlies/[tokenId]/png.js:127:47 {
code: 'MODULE_NOT_FOUND',
requireStack: [
'/var/task/packages/frontend/.next/server/pages/api/pixlies/[tokenId]/png.js',
'/var/task/node_modules/next/dist/server/next-server.js',
'/var/task/.netlify/functions-internal/___netlify-handler/handlerUtils.js',
'/var/task/.netlify/functions-internal/___netlify-handler/___netlify-handler.js',
'/var/task/___netlify-handler.js',
'/var/runtime/UserFunction.js',
'/var/runtime/index.js'
]
}
Paste file contents here. Please check there isn't any private info in them
You can either build locally, or download the deploy from Netlify by clicking the arrow next to the deploy time.
The text was updated successfully, but these errors were encountered: