whatsapp-web.js icon indicating copy to clipboard operation
whatsapp-web.js copied to clipboard

client.logout() error

Open ibrahim0132 opened this issue 1 year ago • 7 comments

Is there an existing issue for this?

  • [X] I have searched the existing issues

Describe the bug

When i am using the client.logout() then showing the below error

node:fs:1780 handleErrorFromBinding(ctx); ^

Error: EPERM: operation not permitted, unlink '\?\C:\Users\ibrah\OneDrive\Desktop\Whatsapp_bot_example\server.wwebjs_auth\session\Default\Cache\Cache_Data\data_0' at unlinkSync (node:fs:1780:3) at _unlinkSync (node:internal/fs/rimraf:214:14) at fixWinEPERMSync (node:internal/fs/rimraf:306:5) at rimrafSync (node:internal/fs/rimraf:200:14) at node:internal/fs/rimraf:253:9 at Array.forEach () at _rmdirSync (node:internal/fs/rimraf:250:7) at fixWinEPERMSync (node:internal/fs/rimraf:304:5) at rimrafSync (node:internal/fs/rimraf:200:14) at node:internal/fs/rimraf:253:9 at Array.forEach () at _rmdirSync (node:internal/fs/rimraf:250:7) at fixWinEPERMSync (node:internal/fs/rimraf:304:5) at rimrafSync (node:internal/fs/rimraf:200:14) at node:internal/fs/rimraf:253:9 at Array.forEach () { errno: -4048, syscall: 'unlink', code: 'EPERM', path: '\\?\C:\Users\ibrah\OneDrive\Desktop\Whatsapp_bot_example\server\.wwebjs_auth\session\Default\Cache\Cache_Data\data_0' }

Expected behavior

na

Steps to Reproduce the Bug or Issue

na

Relevant Code

No response

Browser Type

Chromium

WhatsApp Account Type

WhatsApp Business

Does your WhatsApp account have multidevice enabled?

No, I am not using Multi Device

Environment

Node.js v18.12.0

Additional context

na

ibrahim0132 avatar Mar 31 '23 22:03 ibrahim0132

Try have your project & session folder on another folder? Not sure if Onedrive folder has some sort of special permission restrictions

LTY526 avatar Apr 01 '23 01:04 LTY526

image I am also having the same issue,

location to session: D:\Ajay\Work\Electron JS\sessions location to project: D:\Ajay\Work\Electron JS\evr\whatsapp_automation_frontend

I am making compressed build of whatsapp-web.js using @vercel/ncc. I am using whatsapp-web.js as child process using nodeJs fork in the main process of my electron-react project. Everything works fine except when I try to logout the user which was logged in by using the QR(LocalAuth strategy). logout ends with the error attached in the image.

OS & Environment OS: Windows 10 Node: v18.12.1 Yarn: 1.22.19

developer-choice124 avatar Apr 04 '23 10:04 developer-choice124

I have found the solution

  1. Go to the file: "node_modules/whatsapp-web.js/src/authStrategies/LocalAuth.js"

  2. Find the function end of the file: async logout() { if (this.userDataDir) { return (fs.rmSync ? fs.rmSync : fs.rmdirSync).call(this, this.userDataDir, { recursive: true }); } }

  3. Now Edit the function like this:

async logout() { if (this.userDataDir) { try{ return (fs.rmSync ? fs.rmSync : fs.rmdirSync).call(this, this.userDataDir, { recursive: true }); } catch{} } }

  1. Now the problem solved!
  2. Done!

ibrahim0132 avatar Apr 04 '23 13:04 ibrahim0132

I have found the solution

  1. Go to the file: "node_modules/whatsapp-web.js/src/authStrategies/LocalAuth.js"
  2. Find the function end of the file: async logout() { if (this.userDataDir) { return (fs.rmSync ? fs.rmSync : fs.rmdirSync).call(this, this.userDataDir, { recursive: true }); } }
  3. Now Edit the function like this:

async logout() { if (this.userDataDir) { try{ return (fs.rmSync ? fs.rmSync : fs.rmdirSync).call(this, this.userDataDir, { recursive: true }); } catch{} } }

  1. Now the problem solved!
  2. Done!

This solution just avoid throwing error but issue still persists. Are we cleaning the session data good enough so that next try can generate QR properly? I am curious if it can do even this much

developer-choice124 avatar May 03 '23 13:05 developer-choice124

i am having the same problem please fix the logout function

qazgal1 avatar Jul 08 '23 23:07 qazgal1

Please provide your code and WhatsApp Web version

shirser121 avatar Aug 04 '23 15:08 shirser121

Recently I had the same problem. My fix was:

1 go to: node_modules/whatsapp-web.js/src/authStrategies/LocalAuth.js 2 change the logout async :D

async logout() {
    try {
        if (!this.userDataDir) {
            throw new Error('No user data directory specified.');
        }

        const dirExists = await fs.promises.access(this.userDataDir)
            .then(() => true)
            .catch(() => false);

        if (dirExists) {
            const dirContents = await fs.promises.readdir(this.userDataDir);

            if (dirContents.length > 0) {
                console.log('Directory ${this.userDataDir} is not empty. Deleting contents...');

                // Delete all files and subdirectories inside userDataDir
                await Promise.all(dirContents.map(file => fs.promises.rm(path.join(this.userDataDir, file), { recursive: true, force: true })));
            }

            // Now remove the userDataDir itself
            await fs.promises.rm(this.userDataDir, { recursive: true, force: true });
            console.log('Directory ${this.userDataDir} has been removed');
        } else {
            console.warn('Directory ${this.userDataDir} does not exist.');
        }
    } catch (e) {
        console.error('Failed to remove directory ${this.userDataDir}:', e);
        throw new Error(e);
    }
}

juntusbr avatar Jun 13 '24 14:06 juntusbr