Search code examples
amazon-web-servicestypescriptaws-lambdaclaudiajsclaudia.js

Claudia.js create cannot find module lambda in typescript project


After searching for hours I can't figure out how to properly run claudia create in my project.

Following this turoial, I created a group and a user the AWS console, then I added the keys to my .aws/credentials file.

Then I ran this command that correctly produced the lambda.js file :

claudia --source dist generate-serverless-express-proxy --express-module app

My lambda.js

'use strict'
const awsServerlessExpress = require('aws-serverless-express')
const app = require('./app')
const binaryMimeTypes = [
    'application/octet-stream',
    'font/eot',
    'font/opentype',
    'font/otf',
    'image/jpeg',
    'image/png',
    'image/svg+xml'
]
const server = awsServerlessExpress.createServer(app, null, binaryMimeTypes);
exports.handler = (event, context) => awsServerlessExpress.proxy(server, event, context)

Then I tried to deploy the lambda function on AWS :

claudia create --source dist --profile myprofile --handler lambda.handler --deploy-proxy-api --region eu-west-1

But I got this error :

validating package TypeError: "listener" argument must be a function at _addListener (events.js:239:11) at Server.addListener (events.js:297:10) at new Server (_http_server.js:269:10) at Object.createServer (http.js:34:10) at Object.createServer (/tmp/IiRPif/my-project-1.0.0-1Yh6Wb/package/node_modules/aws-serverless-express/index.js:155:25) at Object. (/tmp/IiRPif/my-project-1.0.0-1Yh6Wb/package/lambda.js:13:37) at Module._compile (module.js:652:30) at Object.Module._extensions..js (module.js:663:10) at Module.load (module.js:565:32) at tryModuleLoad (module.js:505:12) at Function.Module._load (module.js:497:3) at Module.require (module.js:596:17) at require (internal/module.js:11:18) at validatePackage (/usr/local/lib/node_modules/claudia/src/tasks/validate-package.js:16:15) at initEnvVarsFromOptions.then.then.then.then.then.then.then.dir (/usr/local/lib/node_modules/claudia/src/commands/create.js:342:10) at cannot require ./lambda after clean installation. Check your dependencies.

What am I doing wrong here ?

My package.json

{ 
  "name": "...",
  "version": "1.0.0",
  "scripts": {
    "build": "tsc -p tsconfig.json & cp \"package.json\" \"dist/package.json\"
  },
  "repository": {
    "type": "git",
    "url": "..."
  },
  "homepage": "...",
  "dependencies": {
    "body-parser": "^1.18.3",
    "express": "^4.16.3",
    "mongoose": "^5.2.7",
    "nodemon": "^1.18.3"
  },
  "devDependencies": {
    "typescript": "^3.0.1"
  }
}

My app.ts

import * as express from "express";
import * as bodyParser from "body-parser";
import * as mongoose from "mongoose";
import { Routes } from "./routes/routes";

class App {

    public app: express.Application;
    public routes: Routes = new Routes();

    constructor() {
        this.app = express();

        // Parser setup
        this.app.use(bodyParser.json());
        this.app.use(bodyParser.urlencoded({ extended: true }));

        this.routes.routes(this.app);            
    }
}

exports.default = new App().app;

Solution

  • After a discussion on Claudia's github, this appears to be a compatibility issue between es6 and claudia/lambda.

    All I needed to do was to change this line in my app.js file from :

    exports.default = new App().app;
    

    To :

    module.exports = new App().app;