0
votes

I'm trying to build some lambda's in node.js and I am using TypeORM with MySql. I have installed all required node modules but when I deploy lambda (via serverless) and test it, I get errors as follows. Using gulp to build and package .zip.

Log output from test lambda:

START RequestId: 671bed10-81b0-11e7-af34-251688cfdd Version: $LATEST
2017-08-15T11:53:50.594Z    671bed10-81b0-11e7-af34-251688cfdd  
(node:1) UnhandledPromiseRejectionWarning: Unhandled promise rejection 
(rejection id: 2): DriverPackageNotInstalledError: Mysql package has 
not been found installed. Try to install it: npm install mysql --save
2017-08-15T11:53:50.596Z    671bed10-81b0-11e7-af34-251688cfdd  Method:  
"GET"
2017-08-15T11:53:50.596Z    671bed10-81b0-11e7-af34-251688cfdd  Started 
....
2017-08-15T11:53:50.596Z    671bed10-81b0-11e7-af34-251688cfdd  
Finished ....
2017-08-15T11:53:50.596Z    671bed10-81b0-11e7-af34-251688cfdd  
(node:1) PromiseRejectionHandledWarning: Promise rejection was handled 
asynchronously (rejection id: 2)
2017-08-15T11:53:50.597Z    671bed10-81b0-11e7-af34-251688cfddb2    
Console error:  { DriverPackageNotInstalledError: Mysql package has not 
been found installed. Try to install it: npm install mysql --save
at t.Error (native)
at new t (/var/task/index.js:1:48637)
at e.loadDependencies (/var/task/index.js:1:314938)
at new e (/var/task/index.js:1:311241)
at e.createDriver (/var/task/index.js:1:111709)
at e.create (/var/task/index.js:1:104246)
at e.<anonymous> (/var/task/index.js:1:110994)
at r (/var/task/index.js:1:103453)
at Object.next (/var/task/index.js:1:102788)
at /var/task/index.js:1:102697
name: 'DriverPackageNotInstalledError',
message: 'Mysql package has not been found installed. Try to install 
it: npm install mysql --save' }
END RequestId: 671bed10-81b0-11e7-af34-251688cfdd
REPORT RequestId: 671bed10-81b0-11e7-af34-251688cfdd    Duration: 8.71 
ms  Billed Duration: 100 ms     Memory Size: 1024 MB    Max Memory 
Used: 39 MB 

My research has led me to believe that it won't package correctly because its a native module. But i'm not sure. If so is there anyway to get native node modules to work with lambda's?? Any help would be much appreciated, thanks.


EDIT

Folder Structure:

deploy
-- dev
---- serverless.yml
---- PROJECT.zip
-- test
---- serverless.yml
---- PROJECT.zip
-- prod
---- serverless.yml
---- PROJECT.zip

node_modules
-- *
---- *

src
-- api
---- User
------ index.ts
-- data
---- User.ts
-- models
---- User.ts

gulpfile.js
index.ts
package.json
tsconfig.json
tslint.json

Gulp file calls a function from aws-lambda-typescript module that has numerous gulp tasks.

const gulp = require('gulp');
const awsLambdaTypescript = require('aws-lambda-typescript');

awsLambdaTypescript.registerBuildGulpTasks(gulp, __dirname);

Gulp packages .zip in a dist folder and then i copy to deploy>dev and then serverless deploy.

Thanks for your help!

2
Did you ever get TypeORM to work with AWS Lambda? If so please put answer in SO for those of us trying to figure this out :-) - Preston
@Preston No never got it working. I ended up using sequelize docs.sequelizejs.com Let me know how you go. - urang18

2 Answers

0
votes

Most likely something is wrong with your packaging process. I'd check the following items:

  1. Check your package.json if mysql is there.
  2. Check the zip file inside your .serverless directory to see if it includes your node_modules.
  3. Download the lambda package that you uploaded and again check if node_modules are there.
0
votes

I had a similar issue while testing the lambdas using the Serverless framework. I was using TypeORM and Postgres. So instead of mysql package I used the pg package, but got similar error: pg package has not been found installed.

After investigating for sometime I found that while building the code it was not including the pg package. The pg package was used internally and there was no reference of it in my code. So I thought this could be a reason why it is not being included in the build. To test this I added a reference to the pg package in my code by simply adding a console log.

import * as pg from 'pg';
console.log('This is just to add the reference : ', pg);

And it worked.

I think while packaging, the pg/mysql package is being excluded due to tree shaking or some dead code elimination mechanism. So just try to add a reference of the package in your code and see if it works or not.

import * as mysql from 'mysql';
console.log('This is just to add the reference : ', mysql);

I know it's late to answer but I hope this helps someone in future.