elysiajs / elysia

Ergonomic Framework for Humans
https://elysiajs.com
MIT License
10.44k stars 223 forks source link

`TypeError: Elysia is not a constructor` when bundling for node (with polyfills) with esbuild with esm #399

Closed BLucky-gh closed 2 months ago

BLucky-gh commented 10 months ago

since the node entry in package.json points to the cjs build, that's what's imported, but it seems like for some reason it results in the default export of the Elysia class going under exported.module.default.default, and when marking elysia as external in esbuild, esbuild correctly inserts import Elysia from "elysia" in the resulting bundle, but the actual class still gets exported under Elysia.default, so new Elysia errors out, but editing the bundle manually to do new Elysia.default makes it work properly.

You can find some more info under https://github.com/evanw/esbuild/issues/3580

Importing Elysia as a named import rather than the default import with import { Elysia } from "elysia" works, but the @bogeychan/elysia-logger plugin imports elysia via default import and I have no control of that

dodas commented 9 months ago

Relevant: https://github.com/elysiajs/elysia/issues/396

SaltyAom commented 2 months ago

Elysia now exports esm and cjs separately as follows:

{
    "exports": {
        "./package.json": "./package.json",
        ".": {
            "types": "./dist/index.d.ts",
            "bun": "./dist/bun/index.js",
            "import": "./dist/index.mjs",
            "require": "./dist/cjs/index.js"
        }
        }
}

This should have fixed the problem with import and require separately.