Skip to content

Default-exported classes anywhere under server/modules break with "Class constructor … cannot be invoked without 'new'" #3821

@escral

Description

@escral

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    documentationImprovements or additions to documentationv2

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions