"Runtime.ImportModuleError" trying to access npm package in an AWS lambda function using layers
Solution 1
Oh, I can't believe it's just this!
The top-level directory for the .zip file must LITERALLY be named "nodejs
"! I was using a different name, and only changed it back to "nodejs" in the text of this post to be more generic, but the directory name was the real problem all along.
Sigh.
Solution 2
Usually, it's got to do with the name of the folder/files inside. And if those files are referred elsewhere, it's gonna percolate and complain there as well. Just check the folder structure thoroughly, you will be able to catch the thief. I struggled for a day to figure out, it was a silly typo.
kshetline
Apparently, this user doesn't prefer to keep an air of mystery about them.
Updated on January 07, 2022Comments
-
kshetline over 2 years
I'd like to use the npm package "request" in an AWS lambda function.
I'm trying to follow the procedure outlined in this article here: https://medium.com/@anjanava.biswas/nodejs-runtime-environment-with-aws-lambda-layers-f3914613e20e
I've created a directory structure like this:
nodejs │ package-lock.json │ package.json └───node_modules
My package.json looks like this:
{ "name": "my-package-name", "version": "1.0.0", "description": "whatever", "author": "My Name", "license": "MIT", "dependencies": { "request": "^2.88.0" } }
As far as I can tell from the article, all I should have to do with the above is run
npm i
, zip up the directory, upload it as a layer, and add the layer to my lambda function.I've done all of that, but all that I get when I try to test my function is this:
{ "errorType": "Runtime.ImportModuleError", "errorMessage": "Error: Cannot find module 'request'\nRequire stack:\n- /var/task/index.js\n- /var/runtime/UserFunction.js\n- /var/runtime/index.js", "trace": [ "Runtime.ImportModuleError: Error: Cannot find module 'request'", "Require stack:", ...
...as if the layer had never been added. The error is exactly the same whether the layer is added or not. If there's some sort of permissions issue that needs to be resolved, there's nothing in the article that indicates that.
I've tried a few different things, like whether or not my .zip file contains the top-level directory "nodejs" or just its contents. I've tried adding
"main": "index.js",
to mypackage.json
, with anindex.js
file like this:export.modules.request = require('request');
...all to no avail.
What am I missing?
-
JasonMHirst about 4 yearsSame as you, been struggling with this for hours and that worked 100%. Feel such a fool :) Thank you!
-
joeCarpenter almost 4 yearsWait, what exactly did you do?
-
kshetline almost 4 years@joeCarpenter, I just changed the the name of the top-level directory inside my .zip file to literally be
nodejs
, just like I'd seen in sample code, rather than the other name I'd choose. In my original post here, it saysnodejs
not because that's what I had really been using, but because I was trying to make my personal code look more generic... not realizing that I was stumbling onto the real fix needed. -
mikemaccana over 2 yearsThis is more of a comment than an answer.
-
Ghyath Serhal about 2 years@joeCarpenter, Is it possible to elaborate more or show me the sample code. I am still struggling with it.
-
bongoSLAP about 2 years@GhyathSerhal renaming my zip file to nodejs and then uploading it to AWS via the lambda console seemed to work for me, I think this is what this answer is talking about.
-
Guillaume almost 2 yearsNo you need to have the folder
nodejs
inside your zip, not thenode_modules
directly. So you should end up withnodejs/node_modules/...
. I was having same issue and now fixed (with python you also needpython/...
so that makes sense)