Skip to content

issue: unable to connect new device using QR or paring code sometime only #2293

Description

@tls-jil

Hey, can anyone help me with this issue

Image

I’m facing an issue while connecting a new WhatsApp device using QR code or 8-digit pairing code with Baileys., this issue face sometime only , if i restart the server then it's working file or if i create the on connection folder creds.json file then if i again request then it's working fine sometime

When the connection starts and Baileys tries to save credentials, it throws an ENOENT error saying creds.json does not exist.
This happens on first-time device connection, before creds.json is created.

I am using useMultiFileAuthState with a dynamic folder per connection.

::Environment::

Baileys version: @whiskeysockets/baileys@^7.0.0-rc.6
Node.js: v22.16.0
OS: Linux
Auth method: useMultiFileAuthState
Connection type: QR Code / 8-digit Pairing Code

CODE:

const tokenStorePath = path.join(__dirname, "../", "tokens");

const common_balieys_config = {
  printQRInTerminal: false,
  logger: P({ level: "error" }),
  browser: Browsers.ubuntu("Chrome"),
  markOnlineOnConnect: false,
  linkPreviewImageThumbnailWidth: 32,
  generateHighQualityLinkPreview: true,
  keepAliveIntervalMs: 60000,
  emitOwnEvents: true,
  syncFullHistory: true,
};

const connectDevice = async (msg, ws, connectionId) => {
  try {
    const { state, saveCreds } = await useMultiFileAuthState(
      `${tokenStorePath}/${connectionId}`
    );

    const socket = makeWASocket({
      auth: state,
      ...common_balieys_config,
    });

    socket.ev.on("connection.update", async update => {
      const { connection, qr, lastDisconnect } = update;

      if (qr) {
        // handle QR manually
      }

      if (connection === "close") {
        const shouldReconnect =
          lastDisconnect?.error?.output?.statusCode !==
          DisconnectReason.loggedOut;

        if (shouldReconnect) {
          connectDevice(msg, ws, connectionId);
        }
      }

      if (connection === "open") {
        // device connected
      }
    });

    socket.ev.on("creds.update", saveCreds);
  } catch (error) {
    console.error("Error connecting to device:", error);
  }
};


// i handle unhandle rejection 


process.on("uncaughtException", (err) => {
  console.error("\n🔥 UNCAUGHT EXCEPTION");
  console.error("Time:", new Date().toISOString());

  console.error("Name:", err.name);
  console.error("Message:", err.message);
  console.error("Stack:\n", err.stack);

  if (err.cause) {
    console.error("Cause:", err.cause);
  }

  console.error("🔥 END UNCAUGHT EXCEPTION\n");
});

process.on("unhandledRejection", (reason, promise) => {
  console.error("\n🔥 UNHANDLED REJECTION");
  console.error("Time:", new Date().toISOString());

  if (reason instanceof Error) {
    console.error("Name:", reason.name);
    console.error("Message:", reason.message);
    console.error("Stack:\n", reason.stack);

    if (reason.cause) {
      console.error("Cause:", reason.cause);
    }
  } else {
    console.error("Reason (non-error):", reason);
  }

  console.error("Promise:", promise);
  console.error("🔥 END UNHANDLED REJECTION\n");
});


Error Log (Sanitized)

Error: ENOENT: no such file or directory, open
'/path/to/project/tokens/<connection_id>/creds.json'

at async open (node:internal/fs/promises:633:25)
at async writeFile (node:internal/fs/promises:1207:14)
at async use-multi-file-auth-state.js:35:17

errno: -2
code: 'ENOENT'
syscall: 'open'

🔥 UNHANDLED REJECTION
Error: ENOENT: no such file or directory, open '.../creds.json'

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions