Baileys icon indicating copy to clipboard operation
Baileys copied to clipboard

[BUG] TypeScript error - Cannot find module '@adiwajshing/baileys' or its corresponding type declarations.

Open allburov opened this issue 1 year ago • 13 comments

Describe the bug On the import line it gives me an error - Cannot find module '@adiwajshing/baileys' or its corresponding type declarations.

import makeWASocket, {DisconnectReason, makeInMemoryStore, isJidGroup, useMultiFileAuthState} from '@adiwajshing/baileys'

The same line worked fine with the previous repo, adiwajshing/baileys

To Reproduce Steps to reproduce the behavior:

  1. Install the library as dependency in TS project - yarn add github:WhiskeySockets/Baileys
  2. Run npm run build (I'm using nest for that)
  3. Observe the error.

Expected behavior It works :)

Environment (please complete the following information):

  • node 18
  • yarn 1.22
  • typescript 4.7.4
  • What else could help here?

Additional context The same code with adiwajshing/baileys dependency from node_modules works fine. No changes made.

allburov avatar Apr 24 '23 03:04 allburov

If I suppress it with // @ts-ignore it shows

Error: Cannot find module '/home/allburov/git/project/node_modules/@adiwajshing/baileys/lib/index.js'. Please verify that the package.json has a valid "main" entry

allburov avatar Apr 24 '23 03:04 allburov

uses npm, using yarn you need to run tsc manually.

w3nder avatar Apr 24 '23 13:04 w3nder

If I suppress it with // @ts-ignore it shows

Error: Cannot find module '/home/allburov/git/project/node_modules/@adiwajshing/baileys/lib/index.js'. Please verify that the package.json has a valid "main" entry

I am facing the same error after tweaking for this git. Captura de Tela 2023-04-24 às 12 12 10

exallhost avatar Apr 24 '23 15:04 exallhost

image

npm i https://github.com/WhiskeySockets/Baileys --force

Marcoant007 avatar Apr 25 '23 02:04 Marcoant007

It'd be good if I can use npm instead of yarn, but it doesn't seem possible - npm gives me too much errors and with yarn project (the rest) works perfectly :(

For now I cloned the git repo, installed, compiled and added it to my package with yarn add file:../Baileys :+1:

I hope you'll be able to deploy the library in https://www.npmjs.com/ soon!

allburov avatar Apr 26 '23 09:04 allburov

image

npm i https://github.com/WhiskeySockets/Baileys --force

I was unsuccessful in the attempt.

exallhost avatar Apr 26 '23 17:04 exallhost

OK, do the following.

1- delete the node modules

2- delete the package.lock

3- run -> yarn install.

4 - verify https ->"@adiwajshing/baileys": https://github.com/WhiskeySockets/Baileys

5- clear npm cache

I had the same error, when I did that it started working again and recognizing normally

image

Marcoant007 avatar Apr 27 '23 00:04 Marcoant007

OK, do the following.

1- delete the node modules

2- delete the package.lock

3- run -> yarn install.

4 - verify https ->"@adiwajshing/baileys": https://github.com/WhiskeySockets/Baileys

I had the same error, when I did that it started working again and recognizing normally

image

What is "verify https" meant? What is that?

FIlham avatar Apr 29 '23 17:04 FIlham

I meant that it is necessary in dependencies of package.json that have https to reference the repository.

well... that's what worked for me.

Marcoant007 avatar Apr 29 '23 23:04 Marcoant007

uses npm, using yarn you need to run tsc manually.

You need to also tsc manually in npm

Teamolduser avatar Apr 30 '23 08:04 Teamolduser

OK, do the following.

1- delete the node modules

2- delete the package.lock

3- run -> yarn install.

4 - verify https ->"@adiwajshing/baileys": https://github.com/WhiskeySockets/Baileys

I had the same error, when I did that it started working again and recognizing normally

image

Use "@adiwajshing/baileys": "github: WhiskeySockets/Baileys",

Teamolduser avatar Apr 30 '23 08:04 Teamolduser

I have this same issue too

yanuaraditia avatar May 05 '23 10:05 yanuaraditia

I just trying to change the import path and show this error

import makeWASocket from './Socket'
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at Object.compileFunction (node:vm:360:18)
    at wrapSafe (node:internal/modules/cjs/loader:1055:15)
    at Module._compile (node:internal/modules/cjs/loader:1090:27)

There's my script

import makeWASocket, { DisconnectReason, UserFacingSocketConfig } from '@adiwajshing/baileys/src'
import { Boom } from '@hapi/boom'

async function connectToWhatsApp () {
    const sock = makeWASocket({
        // can provide additional config here
        printQRInTerminal: true
    } as UserFacingSocketConfig)
    sock.ev.on('connection.update', (update: any) => {
        const { connection, lastDisconnect } = update
        if(connection === 'close') {
            const shouldReconnect = (lastDisconnect.error as Boom)?.output?.statusCode !== DisconnectReason.loggedOut
            console.log('connection closed due to ', lastDisconnect.error, ', reconnecting ', shouldReconnect)
            // reconnect if not logged out
            if(shouldReconnect) {
                connectToWhatsApp()
            }
        } else if(connection === 'open') {
            console.log('opened connection')
        }
    })
    sock.ev.on('messages.upsert', async (m: any) => {
        console.log(JSON.stringify(m, undefined, 2))

        console.log('replying to', m.messages[0].key.remoteJid)
        await sock.sendMessage(m.messages[0].key.remoteJid!, { text: 'Hello there!' })
    })
}
// run in main file
connectToWhatsApp()

yanuaraditia avatar May 05 '23 10:05 yanuaraditia

I had the same problem, I just cleared the cache and it worked

yarn cache clean

filipeloose avatar May 10 '23 03:05 filipeloose

Now you can install using npm install @whiskeysockets/baileys or yarn add @whiskeysockets/baileys - https://whiskeysockets.github.io/docs/intro

You can use the git version using NPM or Yarn V2, the Yarn v1 (classic) will not work.

edgardmessias avatar May 24 '23 01:05 edgardmessias

You can use the git version using NPM or Yarn V2, the Yarn v1 (classic) will not work.

For those who (as me) didn't know about that - you must run the command below to set V2 mode

yarn set version berry

after that yarn add github:WhiskeySockets/Baileys works fine!

allburov avatar Jun 18 '23 10:06 allburov