serverless-nodejs-starter
serverless-nodejs-starter copied to clipboard
Sharp module not found after deploy
Hey there, hoping someone can shed some light on this issue. I have only been dabbling with serverless functions for a couple of days but I find myself stuck, unable to move forward. Hopefully this isn't something silly on my part.
I am getting the following error after deploying my serverless function to AWS:
06:39:59 pm
2020-06-01T17:39:59.203Z 34bf19c1-4431-443d-977b-419a120053b2 ERROR { Error: Cannot find module 'sharp'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:636:15)
at Module._require.o.require (/var/task/serverless_sdk/index.js:9:72748)
at require (internal/modules/cjs/helpers.js:25:18)
at Object.<anonymous> (/var/task/webpack:/external "sharp":1:1)
at __webpack_require__ (/var/task/webpack:/webpack/bootstrap:19:1)
at Module.<anonymous> (/var/task/webpack:/Users/brendanbenoit/Personal/gaffty/image-resizing-microservice/sharp-demo/handler.js:1:15)
at __webpack_require__ (/var/task/webpack:/webpack/bootstrap:19:1)
at exports.getArg (/var/task/webpack:/webpack/bootstrap:83:1)
at Object.<anonymous> (/var/task/handler.js:87:10)
at Module._compile (internal/modules/cjs/loader.js:778:30) code: 'MODULE_NOT_FOUND' }
I looked around for a couple days now, trying to find the reason for this happening.
What I am doing is:
- Create a new serverless project using
serverless-nodejs-starter. - Update the serverless-* packages to their latest versions in
package.json:
"devDependencies": {
"serverless-bundle": "^1.7.0",
"serverless-dotenv-plugin": "^2.4.2",
"serverless-offline": "^6.3.0"
},
- add the sharp package to my dependencies:
"dependencies": {
"sharp": "^0.25.3"
}
- Add the custom bundle rule for sharp to
serverless.ymlto make sure it gets added as a linux package
custom:
bundle:
packagerOptions:
scripts:
- rm -rf node_modules/sharp && npm install --arch=x64 --platform=linux --target=10.15.0 sharp
When I run the function locally, everything works as expected.
When I deploy and try to hit my endpoint via cURL, I am getting the above error message.
The only thing that seems to be making sense to me is that serverless-bundle excludes the sharp module by default, ass suggested in the generated serverless-state.json here:
"custom": {
"bundle": {
"packagerOptions": {
"scripts": [
"rm -rf node_modules/sharp && npm install --arch=x64 --platform=linux --target=10.15.0 sharp"
]
}
},
"webpack": {
"packager": "npm",
"packagerOptions": {
"$ref": "$[\"service\"][\"custom\"][\"bundle\"][\"packagerOptions\"]"
},
"webpackConfig": "node_modules/serverless-bundle/src/webpack.config.js",
"includeModules": {
"forceExclude": [
"aws-sdk",
"knex",
"sharp"
],
"forceInclude": null,
"packagePath": "package.json"
}
}
},
However that seems to be expected based on this comment.
I have also tried to forceInclude Sharp in my config but the issue remains the same.
Any help figuring out why the module is not accessible once the app is deployed would be very much appreciated.
Hmm the steps you are following seems right. The packager options is basically saying, don't use the installed version of sharp, install the one that works with Lambda.
One thing you can check to see if this works, is serverless package this generates a .serverless dir and you can look inside to see if sharp was packaged correctly. Give that a try?
check this please:
https://sharp.pixelplumbing.com/install#aws-lambda