AWS Lambda - Runtime.ImportModuleError: Error: Cannot find module 'jmespath'
Solution 1
I hit the same problem Error: Cannot find module 'jmespath'
and solved it.
Do you use aws-sdk
via node_modules? like follows
var aws = require('aws-sdk');
If so you just remove aws-sdk from node_modules.
remove aws-sdk for yarn
yarn remove aws-sdk
remove aws-sdk for npm
npm uninstall aws-sdk
aws-sdk
has been included to lambda since nodejs10.
see: https://docs.aws.amazon.com/lambda/latest/dg/lambda-runtimes.html
Solution 2
I'd also like to add that as a preliminary step before adjusting your path, check and verify your dependencies listed in your package.json(s) files.
I've seen this error "Runtime.ImportModuleError","errorMessage":"Error: Cannot find module 'something' occur in the aws cloudwatch logs.
It happened because my project has multiple subprojects/subfolders with their own package.json files. Ensure that module is properly referenced in the subproject's package.json.
In local dev, you might have the dependency cited in your base/global package.json, and thinking it works -- but when you deploy the lambda the npm install that occurs during build does not include the newly required module because its not referenced in the local subproject's package.json.
Solution 3
Try to put ./
in front of your module name. I changed my name from require("xxx")
to require("./xxx")
and it worked again. In my case, the local module file i wanted to add (xxx.js
) is on the same level as the index.js
file.
Solution 4
In the root serverless project folder
npm i --save <npm module name missing>
Comments
-
gokublack almost 2 years
I am working with aws lambda using serverless framework, I changed the runtime from
nodejs8.10
tonodejs10.x
, then I got an errortrace,{"errorType":"Runtime.ImportModuleError","errorMessage":"Error: Cannot find module 'jmespath'","stack":["Runtime.ImportModuleError: Error: Cannot find module 'jmespath'"," at _loadUserApp (/var/runtime/UserFunction.js:100:13)"," at Object.module.exports.load (/var/runtime/UserFunction.js:140:17)"," at Object.<anonymous> (/var/runtime/index.js:36:30)"," at Module._compile (internal/modules/cjs/loader.js:701:30)"," at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)"," at Module.load (internal/modules/cjs/loader.js:600:32)"," at tryModuleLoad (internal/modules/cjs/loader.js:539:12)"," at Function.Module._load (internal/modules/cjs/loader.js:531:3)"," at Function.Module.runMain (internal/modules/cjs/loader.js:754:12)"," at startup (internal/bootstrap/node.js:283:19)"]}
What is the cause of this issue and how can get it fixed?
-
David Azar over 2 yearsTo others reading this answer: double-check that all your project's dependencies are present in the
package.json
file. Sounds obvious, but in this case, it's possible thatnpm
is usingjmespath
from a parent project, or from the globalnode_modules/
you have in your machine, and they are not getting installed in AWS since they are not present in your project'spackage.json
file. -
Conor over 2 yearsyet another good reason not to
npm install -g
ever!