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

error TS2307: Cannot find module 'whatsapp-api-js/messages' #113

Closed
50c10 opened this issue Mar 27, 2023 · 11 comments · Fixed by #122
Closed

error TS2307: Cannot find module 'whatsapp-api-js/messages' #113

50c10 opened this issue Mar 27, 2023 · 11 comments · Fixed by #122

Comments

@50c10
Copy link

50c10 commented Mar 27, 2023

when i try to import whatsapp-api-js/messages i get the error message. this only happens wen import some message

import WhatsAppAPI from "whatsapp-api-js"
import { Text } from "whatsapp-api-js/messages";

@Secreto31126
Copy link
Owner

Hi! Which is your whatsapp-api-js version? Keep in mind the modular import syntax is for the 1.0.0 version

@50c10
Copy link
Author

50c10 commented Mar 27, 2023

i try with 1.0.0-beta.5

@Secreto31126
Copy link
Owner

Great, let me know if it works so I can mark the issue as complete

@arivanbastos
Copy link

arivanbastos commented Apr 5, 2023

Same problem here. 1.0.0-beta.5 / commonjs
Some workaround? Cant run the library in my ts project.
@50c10 , have you overcome this?

@Secreto31126
Copy link
Owner

Secreto31126 commented Apr 5, 2023

I managed to replicate the issue, I'm 100% sure it's a configuration issue with the exports at package.json.

I'm reading the documentation to understand how's the "types" property meant to be used. Tomorrow I have a 2 and a half hour flight, so I might be able to find a solution on the go.

No workaround other than @ts-ignore the issue unfortunately, as it's only a types problem :/

@Secreto31126
Copy link
Owner

Secreto31126 commented Apr 5, 2023

I finally came across the GitHub issue at TS saying why this specific error isn't "supported":

microsoft/TypeScript#33079

Unfortunately, this unsupported feature was how I supposed exports were meant to be used with TypeScript.

Under the hood possible fixes:

  1. NPM workspaces is the recommended solution at the GitHub issue (I think), but I can't implement it right now.

  2. Build all the types into a single file, although I'm not even sure that's possible

  3. Creating duplicated types definitions for module and commonjs folders, removing the need to point to a different folder (not ideal, but the easiest to implement)

Unfortunately, until I settle down which solution to use, the only fix is ts-ignore

Thanks for opening and commenting on the issue!

Further reading: https://github.com/andrewbranch/example-subpath-exports-ts-compat

@50c10
Copy link
Author

50c10 commented Apr 6, 2023

@arivanbastos no, i still have the problem

Secreto31126 added a commit that referenced this issue Apr 6, 2023
@Secreto31126
Copy link
Owner

Hi! I finally fixed the issue with minimal changes to the package.json, so there are no breaking changes.

Check out the PR, I won't be able to publish a new release from my phone in at least a few hours, but it's easy to update locally:

  1. Copy the new package.json from the pull request

  2. Replace your_code/node_modules/whatsapp-api-js/package.json with the new file version

Let me know if this fixes the issue for both of you, and I will merge the PR

Thanks for the issue!

@arivanbastos
Copy link

arivanbastos commented Apr 6, 2023

@Secreto31126 thnk you for reply.
The error remains here, even after replacing node_modules/whatsapp-api-js/package.json by the one present in PR. But It can be a local problem cause I've changed some files to make it work and I'm not 100% sure I undo all these changes. Want to hear @50c10 feedback.

@50c10
If the PR package.json solution doesnt work I was able to bypass typescript type errors by:

  1. Add the following to the end of node_modules/whatapp-api-js/lib/types/index.d.ts
    export { GetParams, ClientMessage } from "./types.js";

  2. Create a class that implements ClientMessage (this is a bit dumb solution cause this class is equals to the Text lib class...).

import { ClientMessage } from "whatsapp-api-js";
export class TextMessage implements ClientMessage {
    body: string;
    preview_url: boolean;
    
    get _type(): string {
        return "text";
    }

    constructor(body: string, preview_url: boolean) {
        if (body.length > 4096) {
            throw new Error("Text body must be less than 4096 characters");
        }

        this.body = body;
        this.preview_url = preview_url;
    }

    _build() {
        return JSON.stringify(this);
    }
}
  1. Use the class created at step 3 to send messages;
whatsappAPI.sendMessage(phoneID, phoneNumber, new TextMessage(messageText, true))
    .then((res)=>{}) 
    .catch((err)=>{});

I had to import GetParams type also, cause I got typescript type errors when using the whatsappAPI.get() method (for webhook validation proccess).

      let params: GetParams = {
          'hub.mode': 'subscribe',
          'hub.verify_token': (urlRequestParams['hub.verify_token'] as string) || '',
          'hub.challenge': (urlRequestParams['hub.challenge'] as string) || '',
      };
      whatsappAPI.get(params);

@50c10
Copy link
Author

50c10 commented Apr 11, 2023

@arivanbastos. I tried the solution and it worked fine.
Thanks @Secreto31126

@Secreto31126
Copy link
Owner

@arivanbastos. I tried the solution and it worked fine.
Thanks @Secreto31126

Thanks for the confirmation! I will be merging the branch in a few hours and pushing a release to npm.

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

Successfully merging a pull request may close this issue.

3 participants