4

I'm converting some of my JS code to typescript and when deploying using serverless framework to a lambda on AWS and running it i'm getting the below error. At first I thought one of my imports was bad but it looks like everything is as expected.

{ "errorType": "Runtime.ImportModuleError", "errorMessage": "Error: Cannot find module 'tslib'\nRequire stack:\n- /var/task/handlers/Create.js\n- >/var/runtime/UserFunction.js\n- /var/runtime/index.js", "trace": [ "Runtime.ImportModuleError: Error: Cannot find module 'tslib'", "Require stack:", "- /var/task/handlers/Create.js", "- /var/runtime/UserFunction.js", "- /var/runtime/index.js", " at _loadUserApp (/var/runtime/UserFunction.js:100:13)", " at Object.module.exports.load (/var/runtime/UserFunction.js:140:17)", " at Object. (/var/runtime/index.js:43:30)", " at Module._compile (internal/modules/cjs/loader.js:956:30)", " at Object.Module._extensions..js (internal/modules/cjs/loader.js:973:10)", " at Module.load (internal/modules/cjs/loader.js:812:32)", " at Function.Module._load (internal/modules/cjs/loader.js:724:14)", " at Function.Module.runMain (internal/modules/cjs/loader.js:1025:10)", " at internal/main/run_main_module.js:17:11" ] }

Serverless:

functions:
  createFamily:
    name: ${self:custom.createFamilyName}
    handler: handlers/Create.createFamily
    description: Lambda for Creating a family in the family service
    timeout: 30
    events:
      - http:
          path: /family
          method: post
          private: true

Handler:

import { APIGatewayEvent, Context, ProxyResult } from 'aws-lambda';
import { utilities } from '../handlers/utilities';
import { familyService } from '../services/FamilyService';
import { IFamily } from '../interfaces/IFamily';

let familyData: IFamily;
let serverReturn: IFamily;

export const createFamily = async (
    event: APIGatewayEvent,
    context: Context
): Promise<ProxyResult> => {
    try {
        if (!event.body) {
            return utilities.BuildResponse(400, JSON.stringify('Object to create was not provided'));
        }
        familyData = JSON.parse(event.body);
        serverReturn = await familyService.createFamily(familyData);

        if (!serverReturn) {
            return utilities.BuildResponse(404, JSON.stringify('Failed to create Family'));
        }

        return utilities.BuildResponse(201, JSON.stringify(serverReturn));
    } catch(err) {
        console.error('Family Service Create a family error: ', err);
        return utilities.BuildResponse(500, JSON.stringify('Family Service internal server error'));
    }
}

2 Answers 2

4

You need to compile your typescript down to javascript and reference the transpiled output in the template.

Sign up to request clarification or add additional context in comments.

3 Comments

You are correct. The problem was is that i was using the serverless-typescript plugin WITH the serverless webpack plugin and i believe they were conflicting. when i removed serverless-typescript and just let webpack do its thing, everything worked
After transpiling, though you may want to check your folder tree properly. 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. Hope this helps!
Can anyone help me am also facing the same?. I am using sam and after ts compile the output js files moved to target location and their references also mapped. But facing this issue.
0

To elaborate the above answer,

By defaut, Lambda expects an index.js file at the root directory for mapping with the server functions.

This can be configured from Edit runtime settings section.

So, if you are using typescript, you need to include the compiled output in the root index.js file.

For example:

Root index.js file contents:

const app = require("./dist/index.js");
if (process.env.ENVIRONMENT === "LAMBDA_ENV_VARIABLE") {
    module.exports.handler = serverless(app.default);
}
else {
    app.default.listen(3000, () => console.log(`Listening on: 3000`));
}

dist/index.js file contents:

// ... all other file contents
exports.default = app; // last line

Root index.ts file content:

// .. all other file contents
export default app;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.