Aws lambda nodejs - error loading zip file compression using GULP

I use Gulp to compress a zip file and then upload it to AWS Lambda. The boot zip file is executed manually. Only the compression process is handled by Gulp.

Here is my gulpfile.js

var gulp = require('gulp'); var zip = require('gulp-zip'); var del = require('del'); var install = require('gulp-install'); var runSequence = require('run-sequence'); var awsLambda = require("node-aws-lambda"); gulp.task('clean', function() { return del(['./dist', './dist.zip']); }); gulp.task('js', function() { return gulp.src('index.js') .pipe(gulp.dest('dist/')); }); gulp.task('npm', function() { return gulp.src('./package.json') .pipe(gulp.dest('dist/')) .pipe(install({production: true})); }); gulp.task('zip', function() { return gulp.src(['dist/**/*', '!dist/package.json']) .pipe(zip('dist.zip')) .pipe(gulp.dest('./')); }); gulp.task('deploy', function(callback) { return runSequence( ['clean'], ['js', 'npm'], ['zip'], callback ); }); 

After starting the deployment task, a zip folder is created with the name dist.zip consisting of the index.js file and the node_modules folder. The node_modules folder contains only the lodash library.

This is index.js

 var _ = require('lodash'); console.log('Loading function'); exports.handler = (event, context, callback) => { //console.log('Received event:', JSON.stringify(event, null, 2)); var b = _.chunk(['a', 'b', 'c', 'd', 'e'], 3); console.log(b); callback(null, event.key1); // Echo back the first key value //callback('Something went wrong'); }; 

After using the AWS lambda console to load the dist.zip folder. An error occurred indicating that the lodash library could not be found

 { "errorMessage": "Cannot find module 'lodash'", "errorType": "Error", "stackTrace": [ "Function.Module._load (module.js:276:25)", "Module.require (module.js:353:17)", "require (internal/module.js:12:17)", "Object.<anonymous> (/var/task/index.js:1:71)", "Module._compile (module.js:409:26)", "Object.Module._extensions..js (module.js:416:10)", "Module.load (module.js:343:32)", "Function.Module._load (module.js:300:12)", "Module.require (module.js:353:17)" ] } 

But in the zip folder there is a node_modules directory containing lodash lib.

 dist.zip |---node_modules |--- lodash |---index.js 

When I zip the node_modules directory and the index.js file manually, it works fine.

Can anyone understand what errors are? Maybe during compression using Gulp it is incorrectly configured for the lib path?

+5
source share
2 answers

I had the same problem a few days ago.
Everyone pointed to gulp zip, however this is not a problem with gulp zip.

Below worked fine:

 gulp .src(['sourceDir/**'], {nodir: true, dot: true} ) .pipe(zip('target.zip')) .pipe(gulp.dest('build/')); 

That is, pay attention to the src paragraph below in the second paragraph:

 {nodir: true, dot: true} 

That is, we must include point files for zip (ex: .config, .abc, etc.)
So, include above in .src from gulp, otherwise all others, such as copy, zip, etc., will be incorrect.

+5
source

The gulp -zip package is massively popular (4.3k downloads per day), and there seems to be no substitute for Gulp. The problem is certainly related to the relative paths and the way gulp -zip processes them. Even when using the base path option in the gulp.src function (example below), gulp -zip finds a way to mess it up.

 gulp.task("default", ["build-pre-zip"], function () { return gulp.src([ "dist/**/*" ], { base: "dist/" }) .pipe(debug()) .pipe(zip("dist.zip")) .pipe(gulp.dest("./dist/")); }); 

Since there is no good Gulp solution as of 1/4/2017, I suggest a workaround. I use Gulp to populate the dist folder first, exactly as I need it with the corresponding node_modules folder. Then it's time to properly mount the dist folder with the relative file paths saved. To do this, as well as update Lambda, I use the batch file (Windows) of the command line options to do the job. Here is the upload.bat file that I created to replace the gulp -zip task:

 start /wait cmd /c "gulp default" start /wait cmd /c "C:\Program Files\WinRAR\WinRAR.exe" a -r -ep1 dist\dist.zip dist\*.* aws lambda update-function-code --zip-file fileb://dist/dist.zip --function-name your-fn-name-here 

If you use WinRAR, you will find your documents here on the command line , for WinZip go here. This .bat file assumes that you are using the AWS command line interface, which is a find; enter it here .

If you want this answer to point you to a 100% Gulp solution, I say, "You and I both!" Good luck.

+1
source

Source: https://habr.com/ru/post/1258736/


All Articles