Default-exported classes anywhere under `server/modules` break with "Class constructor … cannot be invoked without 'new'"
Environment
[email protected] [email protected] or [email protected]
Reproducible in a fresh project
Reproduction
https://codesandbox.io/p/github/nitrojs/nitro/csb-8kjns6/draft/nifty-leftpad
Add default class export to server/modules/**/{SomeClass}.ts
Describe the bug
When a class is default-exported from a file located in the server/modules directory, Nitro fails during build/runtime with the following error:
Class constructor SomeEntity cannot be invoked without 'new'
Problem disappears as soon as file is moved outside of server/modules.
This reproduces regardless of folder structure, e.g.:
- server/modules/example.ts
- server/modules/domain/example.ts
- server/modules/domain/some-domain/entities/example.ts
Additional context
My guess is that Nitro’s loader for server/modules treats default exports in a special way (e.g. as “Nitro modules” to be executed).
If server/modules is intended strictly for Nitro “modules” and not arbitrary server code, a clearer error or documentation note would be helpful. Otherwise, it seems like a bug in how default exports are handled in that directory.
Logs
pnpm run dev
> nitro-app@ dev /var/www/test
> nitro dev
ERROR Class constructor SomeEntity cannot be invoked without 'new'
at installModules (node_modules/nitropack/dist/core/index.mjs:823:15)
at async createNitro (node_modules/nitropack/dist/core/index.mjs:1130:3)
at async reload (node_modules/nitropack/dist/cli/dev.mjs:33:15)
at async Object.run (node_modules/nitropack/dist/cli/dev.mjs:62:5)
at async runCommand (node_modules/citty/dist/index.mjs:316:16)
at async runCommand (node_modules/citty/dist/index.mjs:307:11)
at async runMain (node_modules/citty/dist/index.mjs:445:7)
ERROR Class constructor SomeEntity cannot be invoked without 'new'
ELIFECYCLE Command failed with exit code 1.
Modules in nitro v3 should not be auto-scanned from server/modules anymore (related issue https://github.com/nitrojs/nitro/issues/2691)
We don't have any section in v2 docs re modules. Ideas welcome where would be best to document this.