18

I am working with aws lambda using serverless framework, I changed the runtime from nodejs8.10 to nodejs10.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?

2
  • Did you find a solution, running into the same problem... Commented Jan 6, 2020 at 18:29
  • I was running into this where the lambda would invoke locally (on Windows) but report a failure to find the handler module once deployed. My serverless.yml was locating the handler function with a backslash in the path name. functions\myHandler.myFunction. It worked after changing to a forward slash. Commented Mar 15, 2022 at 19:04

7 Answers 7

13

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

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

1

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.

Comments

1

In the root serverless project folder

npm i --save <npm module name missing>

2 Comments

To 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 that npm is using jmespath from a parent project, or from the global node_modules/ you have in your machine, and they are not getting installed in AWS since they are not present in your project's package.json file.
yet another good reason not to npm install -g ever!
0

Well encountered the same issue in our project too. It was issue in the same of file import. Our file name xabc.js and in imported as Xabc.js VScode was not complaining and was showing proper import

Comments

0

For me the problem was that this jmespath library was actually missing(and some others, too) from uploaded node_modules folder.
Verified this by downloading zip(as the source was too big) and did not found there jmespath nor aws-sdk libraries.
Solved it by installing dependencies with npm i and re-uploading lambda with updated node_modules folder.

Comments

0

If 'nodejs' folder is not there in Lambda Layer, create 'nodejs' and any file so that while executing the function, lambda will create zip file with entire node_modules packages inside 'nodejs' folder of Layer. If it is empty it will not execute. I hope it will resolve the issue. In my case it is working fine.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.