typescriptnestjs

Always throw an Exception "Exception has occurred: TypeError: xxxx.default is not a constructor"


Using TypeScript, I's using EventEmitter to listen the events exposed by node:events.

import EventEmitter from 'events';
const eventEmitter = new EventEmitter();

Here is my tsconfig.json

{
  "compilerOptions": {
    "module": "commonjs",
    "moduleResolution": "node",
    "declaration": true,
    "removeComments": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "allowSyntheticDefaultImports": true,
    "target": "ES2021",
    "sourceMap": true,
    "outDir": "./dist",
    "baseUrl": "./",
    "incremental": true,
    "skipLibCheck": true,
    "strictNullChecks": false,
    "noImplicitAny": false,
    "strictBindCallApply": false,
    "forceConsistentCasingInFileNames": false,
    "noFallthroughCasesInSwitch": false,
    "noEmitOnError": true,
  }
}

So, how can I find out the exactly reason?


Solution

  • Add the setting "esModuleInterop": true in tsconfig.json file, then everything worked nicely.

    {
      "compilerOptions": {
        "module": "commonjs",
        "moduleResolution": "node",
        "esModuleInterop": true,
        ...
      }
    }