Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[bug] Aedes is undefined in Typescript #927

Closed
benbai123 opened this issue Jan 8, 2024 · 7 comments
Closed

[bug] Aedes is undefined in Typescript #927

benbai123 opened this issue Jan 8, 2024 · 7 comments
Labels

Comments

@benbai123
Copy link

System Information

  • Aedes: 0.50.1
  • NodeJS: 16.13.1
  • Typescript: 5.3.3
  • OS: Mac (M1)

Describe the bug
Run official example, Aedes is undefined and got error TypeError: aedes_1.default is not a constructor

To Reproduce
Steps to reproduce the behavior:

create 3 files below and run shell npm install && npm run start

  • package.json
{
  "scripts": {
    "start": "npx tsc --p ./tsconfig.json && node dist/main.js"
  },
  "devDependencies": {
    "typescript": "^5.3.3"
  },
  "dependencies": {
    "@types/node": "^20.10.7",
    "aedes": "^0.50.1"
  }
}
  • tsconfig.json
{
  "compilerOptions": {
    "rootDir": "src",
    "module": "commonjs",
    "declaration": true,
    "removeComments": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "allowSyntheticDefaultImports": true,
    "target": "es2017",
    "sourceMap": true,
    "outDir": "./dist",
    "baseUrl": "./",
    "incremental": true,
    "skipLibCheck": true,
    "strictNullChecks": false,
    "noImplicitAny": false,
    "strictBindCallApply": false,
    "forceConsistentCasingInFileNames": false,
    "noFallthroughCasesInSwitch": false
  },
  "include": ["src/"],
}

  • /src/main.ts
import Aedes from 'aedes'
import { createServer } from 'net'

const port = 1883

const aedes = new Aedes()
const server = createServer(aedes.handle)

server.listen(port, function () {
  console.log('server started and listening on port ', port)
})

Expected behavior
Should start a broker

Additional context

  • The compiled file main.js
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const aedes_1 = require("aedes");
const net_1 = require("net");
const port = 1883;
const aedes = new aedes_1.default();
const server = (0, net_1.createServer)(aedes.handle);
server.listen(port, function () {
    console.log('server started and listening on port ', port);
});
//# sourceMappingURL=main.js.map
@benbai123 benbai123 added the bug label Jan 8, 2024
@robertsLando
Copy link
Member

robertsLando commented Jan 8, 2024

Does it works with 0.50.0? May be a regression introduced by #918 .

cc @hjdhjd

@benbai123
Copy link
Author

nope, tried 0.49.0 and 0.50.0, same error

@robertsLando
Copy link
Member

Could you submit a PR to fix the issue?

@hjdhjd
Copy link
Contributor

hjdhjd commented Jan 8, 2024

I don't think there is a PR needed here - the issue is with the compilation options the user is using, not the code itself.

Try using:

tsconfig.json:

{
  "compilerOptions": {
    "allowSyntheticDefaultImports": true,
    "declaration": true,
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "lib": [
      "DOM",
      "ES2022"
    ],
    "module": "ES2022",
    "moduleResolution":"node",
    "outDir": "dist",
    "rootDir": "src",
    "sourceMap": true,
    "strict": true,
    "target": "ES2022"
  },
  "include": [
    "src"
  ]
}

Ensure package.json has "type": "module" in there as well.

@robertsLando I can confirm that everything is working as it should be with the existing release.

@benbai123
Copy link
Author

confirmed, need "type": "module" and 2 key compiler options.

  • 2 key compiler options
{
  // ...
  "module": "ES2022",
  "moduleResolution":"node",
}

btw, the compiler options I used before are copied from NestJS

@benbai123
Copy link
Author

benbai123 commented Jan 9, 2024

tried run Aedes with NestJS, failed as expected.

workaround for NestJS :

import * as Aedes from 'aedes'
import { createServer } from 'net'

const port = 1883

const aedes = Aedes.createBroker()
const server = createServer(aedes.handle)

server.listen(port, function () {
  console.log('server started and listening on port ', port)
})

found another ts compiler option "esModuleInterop": true working with NestJS, but probably will break some other packages like jwks-rsa raw-body

@robertsLando
Copy link
Member

Ensure package.json has "type": "module" in there as well.

@hjdhjd So it only works with ESM now? Does it always require type module??

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants