Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: ee.on is not a function #1832

Open
Stankman opened this issue Oct 31, 2024 · 8 comments
Open

TypeError: ee.on is not a function #1832

Stankman opened this issue Oct 31, 2024 · 8 comments

Comments

@Stankman
Copy link

Bug Report

Current Behavior

When running sls offline for a Serverless application using serverless-offline with the configuration below, an error occurs: TypeError: ee.on is not a function. This seems to prevent the expected startup of the local server.

Sample Code

  • file: serverless.yml
service: sls-test

provider:
  name: aws
  runtime: nodejs20.x

functions:
  api:
    handler: dist/lambda.handler
    events:
      - http:
          path: /
          method: ANY
      - http:
          path: '{proxy+}'
          method: ANY

package:
  exclude:
    - node_modules/**

plugins:
  - serverless-offline
  • file: handler.js
import serverlessExpress from '@codegenie/serverless-express';
import { NestFactory } from "@nestjs/core";
import { AppModule } from "./app.module";
import { Callback, Context, Handler } from 'aws-lambda';

let server : Handler;

export const handler = async (event: any, context: Context, callback: Callback) => {
    if(!server) {
        const app = await NestFactory.create(AppModule);
        await app.init();

        const expressApp = app.getHttpAdapter().getInstance();
        server = serverlessExpress({
            app: expressApp
        });
    }

    return server(event, context, callback);
}

Expected behavior/code

The application should start without errors and expose the API endpoints locally using serverless-offline.

Environment

  • serverless version: 4.4.7
  • serverless-offline version: 14.3.3
  • node.js version: v20.18.0
  • OS: macOS 15.0.1

optional, if you are using any of the following frameworks to invoke handlers

  • nestJS version: 10.4.5

Possible Solution

It could be helpful to check for compatibility issues with serverless-offline or dependencies like ee-first in conjunction with the current node.js version.

Additional context/Screenshots

Output when running sls offline:

[Error Logs...]
✖ TypeError: ee.on is not a function
...
@lAndresul
Copy link

Same issue here

@MehtaManan07
Copy link

Same issue happening for me as well, did anyone find a solution??

@DorianMazur
Copy link
Collaborator

DorianMazur commented Nov 6, 2024

Hi @Stankman !
Can you post full error with the stacktrace?

@DanielMaranhao
Copy link

Hi, I'm facing a similar issue, I believe it is mostly similar to the issue of Stankman. It occurs when trying to access http://localhost:3000/dev. In my case, the stack trace would be the following, but please feel free to post yours if it differs somehow.

TypeError: ee.on is not a function
    at first (C:\Workspaces\ws-nest\serverless-project\node_modules\ee-first\index.js:43:10)
    at onSocket (C:\Workspaces\ws-nest\serverless-project\node_modules\on-finished\index.js:115:16)
    at attachFinishedListener (C:\Workspaces\ws-nest\serverless-project\node_modules\on-finished\index.js:120:5)
    at attachListener (C:\Workspaces\ws-nest\serverless-project\node_modules\on-finished\index.js:147:5)
    at onFinished (C:\Workspaces\ws-nest\serverless-project\node_modules\on-finished\index.js:53:3)
    at send (C:\Workspaces\ws-nest\serverless-project\node_modules\finalhandler\index.js:319:3)
    at C:\Workspaces\ws-nest\serverless-project\node_modules\finalhandler\index.js:135:5
    at C:\Workspaces\ws-nest\serverless-project\node_modules\express\lib\router\index.js:646:15
    at next (C:\Workspaces\ws-nest\serverless-project\node_modules\express\lib\router\index.js:216:14)
    at Function.handle (C:\Workspaces\ws-nest\serverless-project\node_modules\express\lib\router\index.js:175:3)

@scryan7371
Copy link

Same Issue here.

✖ TypeError: ee.on is not a function
at first (/Users/scottryan/GIT_WORKING/joinourplans/joinourplans-server/node_modules/ee-first/index.js:43:10)
at onSocket (/Users/scottryan/GIT_WORKING/joinourplans/joinourplans-server/node_modules/on-finished/index.js:115:16)
at attachFinishedListener (/Users/scottryan/GIT_WORKING/joinourplans/joinourplans-server/node_modules/on-finished/index.js:120:5)
at attachListener (/Users/scottryan/GIT_WORKING/joinourplans/joinourplans-server/node_modules/on-finished/index.js:147:5)
at onFinished (/Users/scottryan/GIT_WORKING/joinourplans/joinourplans-server/node_modules/on-finished/index.js:53:3)
at send (/Users/scottryan/GIT_WORKING/joinourplans/joinourplans-server/node_modules/finalhandler/index.js:319:3)
at /Users/scottryan/GIT_WORKING/joinourplans/joinourplans-server/node_modules/finalhandler/index.js:135:5
at /Users/scottryan/GIT_WORKING/joinourplans/joinourplans-server/node_modules/express/lib/router/index.js:646:15
at next (/Users/scottryan/GIT_WORKING/joinourplans/joinourplans-server/node_modules/express/lib/router/index.js:216:14)
at Function.handle (/Users/scottryan/GIT_WORKING/joinourplans/joinourplans-server/node_modules/express/lib/router/index.js:175:3)

@Mujtaba52
Copy link

yup same issue here :/

@Mujtaba52
Copy link

Guys I encountered the same issue. It seems the Lambda function doesn't work for the base route (/). To troubleshoot, try clearing the cache and create a test route, like /hello, then access it via http://localhost:3000/dev/hello. This worked for me, while accessing http://localhost:3000/dev still fails. I'm not certain of the exact cause, but this workaround might help!

@leandromatos
Copy link

The way that I found to handle this problem was to parse the path before returning the server instance:

if (event.path === '' || event.path === undefined) event.path = '/';

The entire code:

import { NestFactory } from '@nestjs/core';
import serverlessExpress from '@vendia/serverless-express';
import { Callback, Context, Handler } from 'aws-lambda';
import { ReplaySubject, firstValueFrom } from 'rxjs';
import { FooModule } from './foo.module';

// Declare a ReplaySubject to store the serverlessExpress instance.
const serverSubject = new ReplaySubject<Handler>();

async function bootstrap(): Promise<Handler> {
  console.log('COLD START: Initializing Nest');

  const app = await NestFactory.create(FooModule);

  await app.init();

  const expressApp = app.getHttpAdapter().getInstance();
  return serverlessExpress({ app: expressApp });
}

// Do not wait for lambdaHandler to be called before bootstraping Nest.
// Pass the result of bootstrap() into the ReplaySubject
bootstrap().then((server) => serverSubject.next(server));

type EventPayload = {
  [key: string]: any;
};

export const handler: Handler = async (
  event: EventPayload,
  context: Context,
  callback: Callback,
) => {
  // Handle edge cases for root path
  if (event.path === '' || event.path === undefined) event.path = '/';

  // Convert the ReplaySubject to a Promise.
  // Wait for bootstrap to finish, then start handling requests.
  const server = await firstValueFrom(serverSubject);
  return server(event, context, callback);
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants