Reputation: 1929
I'm trying to create a lambda in nodejs. Following the documentation, I have created a zip file with lambda function, node_modules and package.json. The structure of my lambda zip file is as follows:
my-lambda-function/
|
|---util/
| |
| |--util-1.js
| |--util-2.js
|---api/
| |
| |--api-call-1.js
| |--api-call-2.js
|
|---config
| |
| |--env/
| | |--env-file-1.js
| |
| |--config-file-1.js
| |--config-file-2.js
|
|---node_modules/
| |
| |--module-1/
| |--module-2/
|---index.js
|---package.json
For creating the this zip file I'm using a gulp task,
gulp.task('zip', ['test'], () => {
const buildArtifact = ['index.js', 'package.json', 'util/**',
'config/**', 'api/**'];
Object.keys(pjson.dependencies).forEach((dep) => {
buildArtifact.push(`node_modules/${dep}/**/*`);
});
const zipFile = `${pjson.name}.zip`;
return gulp.src(buildArtifact, { base: '.' })
.pipe(zip(zipFile))
.pipe(gulp.dest('build'));
});
This lambda is running locally with lambda-local. But while testing this lambda is throwing error
Unable to import module 'index': Error
at Function.Module._resolveFilename (module.js:547:15)
at Function.Module._load (module.js:474:25)
at Module.require (module.js:596:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (/var/task/node_modules/ioredis/built/utils/lodash.js:2:19)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
Current error is pointing to ioredis module (which is available in node_modues folder), but if I remove the usage of ioredis from the code (by removing the imports for ioredis) then the error changes to another node module.
It seems my lambda function is unable to load/find the node_modules. Is it due to lambda is failing to install npm?
Any pointers to solve this would be helpful as we are struggling with this for a few days
Thanks
Changing the zip task to:
gulp.task('npm-install', () => gulp.src('./package.json')
.pipe(gulp.dest('build/'))
.pipe(install({ production: true })));
worked for me.
Upvotes: 1
Views: 16278
Reputation: 5133
Also got this error when creating an AWS::Serverless::Function using CloudFormation, and specifying the code with InlineCode
, where the code referenced a dependency that was not bundled in the selected Runtime.
Note that some packages are included in the runtime (eg. nodejs8.10 seems to have at least aws-sdk and util).
Upvotes: 0
Reputation: 13055
Unable to import module 'index': Error
It looks like some of your dependencies are installed / not packaged correctly. Delete the node_modules directory and re-install the module with npm install
should fix the issue.
Make sure your function get packaged as mentioned in the doc https://docs.aws.amazon.com/lambda/latest/dg/nodejs-create-deployment-pkg.html
If your bundle missed a node_module, then it will result in this error.
EDIT1:
You got an issue with gulp packaging.
The following example has the complete documentation for packaging lambda with gulp.
https://medium.com/@AdamRNeary/a-gulp-workflow-for-amazon-lambda-61c2afd723b6
Hope it helps.
Upvotes: 5