Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS Lambda - Runtime.ImportModuleError: Error: Cannot find module 'jmespath'

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?

like image 309
gokublack Avatar asked Jul 18 '19 06:07

gokublack


4 Answers

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

like image 104
kon_yu Avatar answered Oct 20 '22 02:10

kon_yu


In the root serverless project folder

npm i --save <npm module name missing>
like image 33
Conor Avatar answered Oct 20 '22 01:10

Conor


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.

like image 21
user2782993 Avatar answered Oct 20 '22 03:10

user2782993


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.

like image 32
AM_FM Avatar answered Oct 20 '22 02:10

AM_FM