Import module error when Lambda is deployed - Node

29 Views Asked by At

I am getting an ImportModuleError after the Lambda is deployed. The function works fine locally (I tried with serverless-offline and it works fine), but when trying to invoke the deployed function, it is giving the following error. reflect-metadata is correctly added among the dependancies in package.json

2024-03-26T02:52:13.927Z undefined ERROR Uncaught Exception { "errorType": "Runtime.ImportModuleError", "errorMessage": "Error: Cannot find module 'reflect-metadata'\nRequire stack:\n- /var/task/dist/functions/OrderHandler.js\n- /var/runtime/index.mjs", "stack": [ "Runtime.ImportModuleError: Error: Cannot find module 'reflect-metadata'", "Require stack:", "- /var/task/dist/functions/OrderHandler.js", "- /var/runtime/index.mjs", at _loadUserApp (file:///var/runtime/index.mjs:1087:17)", at async UserFunction.js.module.exports.load (file:///var/runtime/index.mjs:1119:21)", at async start (file:///var/runtime/index.mjs:1282:23)", at async file:///var/runtime/index.mjs:1288:1" ] }


import 'reflect-metadata';
import { APIGatewayProxyEvent, APIGatewayProxyResult } from 'aws-lambda';
import { OrdersController } from './OrdersController';

export const handler = async (
  event: APIGatewayProxyEvent,
): Promise<APIGatewayProxyResult> => {
  try {
    const path = event.path;
    const method = event.httpMethod;

    container.registerSingleton<OrderRepository>(
      'OrderRepository',
      OrderRepository,
    );
    const OrdersController = new OrdersController();

    console.log(path);
    // Route handling
    switch (true) {
      case path === '/orders' && method === 'GET':
        return OrdersController.handler(event);
      default:
        return {
          statusCode: 404,
          body: JSON.stringify({ message: 'Route not found' }),
        };
    }
  } catch (error) {
    console.error('Error:', error);
    return {
      statusCode: 500,
      body: JSON.stringify({ message: 'Internal server error' }),
    };
  }
};

Can anyone please advice on what could be potentially wrong?

I tried to invoke the lambda function, but it failed with the above error.

0

There are 0 best solutions below