Secreto31126 / whatsapp-api-js

A TypeScript server agnostic Whatsapp's Official API framework
MIT License
128 stars 31 forks source link

TypeError: Class extends value #<Object> is not a constructor or null #306

Closed JoseZamora97 closed 4 months ago

JoseZamora97 commented 4 months ago

Describe the bug

Getting TypeError: Class extends value # is not a constructor or null When importing a WhatsAppAPI.

To Reproduce

Importing the whatsappClient created:

import whatsappClient from '../integrations/whatsapp/whatsapp.client';

import WhatsAppAPI from 'whatsapp-api-js/middleware/express';

const whatsappClient = new WhatsAppAPI({
  token: env.WHATSAPP_ACCESS_TOKEN,
  webhookVerifyToken: env.WHATSAPP_WEBHOOK_VERIFY_TOKEN,
  appSecret: env.WHATSAPP_APP_SECRET,
  v: env.WHATSAPP_API_VERSION,
});

Error:

TypeError: Class extends value #<Object> is not a constructor or null
    at Object.<anonymous> (C:\Users\josez\Desktop\Blink\ana-nlp\node_modules\.pnpm\whatsapp-api-js@2.5.1\node_modules\whatsapp-api-js\lib\cjs\middleware\globals.js:1:1158)
    at Module._compile (node:internal/modules/cjs/loader:1241:14)
    at Object.F (C:\Users\josez\Desktop\Blink\ana-nlp\node_modules\.pnpm\tsx@4.1.2\node_modules\tsx\dist\cjs\index.cjs:1:1393)
    at Module.load (node:internal/modules/cjs/loader:1091:32)
    at Module._load (node:internal/modules/cjs/loader:938:12)
    at Module.require (node:internal/modules/cjs/loader:1115:19)
    at require (node:internal/modules/helpers:130:18)
    at Object.<anonymous> (C:\Users\josez\Desktop\Blink\ana-nlp\node_modules\.pnpm\whatsapp-api-js@2.5.1\node_modules\whatsapp-api-js\lib\cjs\middleware\express.js:1:804)
    at Module._compile (node:internal/modules/cjs/loader:1241:14)
    at Object.F (C:\Users\josez\Desktop\Blink\ana-nlp\node_modules\.pnpm\tsx@4.1.2\node_modules\tsx\dist\cjs\index.cjs:1:1393)

Expected behavior

Import the object witout errors.

Desktop (please complete the following information):

  • OS: [e.g. Windows] Windows 11
  • Runtime [e.g. node@19, deno@1.33.4] node@20.9
  • Library Version [e.g. 1.0.0] 2.6.0
Secreto31126 commented 4 months ago

Hi, thanks for reporting!

I had received a similar report in https://github.com/Secreto31126/whatsapp-api-js/issues/300#issuecomment-1932130229, ~however, I'm seriously struggling to replicate the issue.~

While writting the comment I had a revelation, the bug is on the cjs side.

May I ask, are you using any compiler for your code, such as vite or esbuild?

Secreto31126 commented 4 months ago

I spent the last few hours reducing the possibilities and I managed to get a super small bug reproduction in a StackBlitz. Once I got the words to describe the problem I went to the esbuild repo and searched for similar issues:

https://github.com/evanw/esbuild/issues/2623

In other words, JS is broken and export default has interoperability issues.

Option 1: Replace all default exports with named exports, requiring a breaking update for the library

Option 2: As mentioned in the issue, changing "type": "module" before and after compiling should patch the problem

The first one hurts me because I really love the default syntax and I don't want to let it go. However, the other option is painfully dumb and bug prone.

I will think it through this night and work on it tomorrow. Maybe it's time to start working on v3, I have a bunch of ideas in the workshop which I couldn't release because it required breaking changes :)