Skip to content

process.cwd() for assets path (unexpected breakage in production) #58

@diaslui

Description

@diaslui

When registering the static plugin in an Elysia application like this:

.use(
  staticPlugin({
    assets: "public",
  })
);

The natural and expected behavior for most developers is that the assets path is resolved relative to the file where the server is defined, not relative to process.cwd().

During development, this usually works because developers tend to run the server from the project root:
bun run src/index.ts

In this scenario, process.cwd() matches the project root, so "public" resolves correctly.

However, this behavior breaks unexpectedly in production, especially in real-world setups such as:

  • Docker containers
  • Monorepos
  • systemd / PM2
  • Running the app from a parent directory

In those environments, process.cwd() is often not the project root, which causes static assets to fail with ENOENT errors.
Your application will run perfectly in development, but in production it will break on the first deployment, and you'll wonder why. Here's the answer.

diaslui@diaslui:~/code/diaslui.com$ bun run src/index.ts
running at http://localhost:3000
^C
diaslui@diaslui:~/code/diaslui.com$ cd ..
diaslui@diaslui:~/code$ bun run diaslui.com/src/index.ts
running at http://localhost:3000
ENOENT: no such file or directory, open '/home/diaslui/code/public'
    path: "/home/diaslui/code/public\u0000",
 syscall: "open",
   errno: -2,
    code: "ENOENT"

Development:

Image

Prod:

Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions