Skip to content

Exporting Elysia from file causes server to run twice #550

Closed as not planned
@EvHaus

Description

@EvHaus

What version of Elysia.JS is running?

1.0.0

What platform is your computer?

Linux 5.15.133.1-microsoft-standard-WSL2 x86_64 x86_64

What steps can reproduce the bug?

Create a basic Elysia server like this:

import { Elysia } from 'elysia';
const app = new Elysia()
  .get('/', () => 'Landing')
  .listen(8080)

console.log(
	`🦊 API is running at ${app.server?.hostname}:${app.server?.port}`,
);

export default app;

Then run the server via bun index.ts.

What is the expected behavior?

Server should run with no errors.

What do you see instead?

Server crashes with:

bun run index.ts
 6 |       if(typeof entryNamespace?.default?.fetch === 'function')  {
 7 |         Bun.serve(entryNamespace.default);
 8 |       }
 9 |    }, reportError);
10 | } else if (typeof entryNamespace?.default?.fetch === 'function') {
11 |    Bun.serve(entryNamespace.default);
     ^
EADDRINUSE: Failed to start server. Is port 8080 in use?
 syscall: "listen"

      at bun:main:11:4

The problem seems to be that doing export default app in Elysia 1.0.0 causes a separate listen call to be executed.

Removing export default app makes the issue go away, but this means you can't import the file for the purpose of unit testing as shown here.

Removing listen(8080) also makes the issue go away, but causes app.server not to be defined, so the console message prints: API is running at undefined:undefined instead.

I can also remove export default app and change const app = ... to export const app = ... to fix this, and I suppose this is what you recommend to do, but this behavior was not expected (or documented) so I thought I'd log it.

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions