whatsapp-web.js, How can I fix TypeError: Cannot read properties of null (reading '1')
Asked Answered
C

12

8

const version = indexHtml.match(/manifest-([\d.]+).json/)[1]; ^

TypeError: Cannot read properties of null (reading '1') at LocalWebCache.persist (/Users/abc/Desktop/haba/node_modules/whatsapp-web.js/src/webCache/LocalWebCache.js:29:69) at /Users/abc/Desktop/haba/node_modules/whatsapp-web.js/src/Client.js:744:36 at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

Node.js v20.11.1

once the code is run the second time, it always brings this error.

i was using nodemon so I did: npx nodemon index.js smooth running

Cilka answered 3/4, 2024 at 7:15 Comment(0)
C
24

Add this argument to your Client initialization:

webVersionCache: {
    type: 'remote',
    remotePath: 'https://raw.githubusercontent.com/wppconnect-team/wa-version/main/html/2.2412.54.html',
    }

It should end up looking like this:

const client = new Client({
  webVersionCache: {
    type: "remote",
    remotePath:
      "https://raw.githubusercontent.com/wppconnect-team/wa-version/main/html/2.2412.54.html",
  },
});
Cilka answered 3/4, 2024 at 7:18 Comment(4)
It's failing again with "Evaluation failed: TypeError: Cannot read properties of undefined (reading 'default')"...do we have to update the remotePath with more recent version of "2.2412.54.html"?Perdition
Same problem hereHalfsole
"raw.githubusercontent.com/wppconnect-team/wa-version/main/html/…", is broken.Desimone
someone found the solution?Neumeyer
M
4

Edit LocalWebCache.js persist function adding the condition:

if (indexHtml.match(/manifest-([\d\\.]+)\.json/) != null)
async persist(indexHtml) {
  // extract version from index (e.g. manifest-2.2206.9.json -> 2.2206.9)
  if (indexHtml.match(/manifest-([\d\\.]+)\.json/) != null) {
    const version = indexHtml.match(/manifest-([\d\\.]+)\.json/)[1];
    if (!version) return;
    
    const filePath = path.join(this.path, `${version}.html`);
    fs.mkdirSync(this.path, { recursive: true });
    fs.writeFileSync(filePath, indexHtml);
  }
}
Maurya answered 4/4, 2024 at 4:5 Comment(0)
B
2

I am facing same issue since last two days temporary I fixed issue with editing LocalWebcache.js I changed:

const version = indexHtml.match(/manifest-([\d\\.]+)\.json/)[1];

To:

const version = '2.24.7.72'

This is the current WhatsApp version. But it's temporary solution not permanent.

Bottommost answered 4/4, 2024 at 4:20 Comment(0)
N
2

tried all the possibilities explained in this post, but am getting the below error:

throw new Error('Evaluation failed: ' + helper_js_1.helper.getExceptionMessage(exceptionDetails)); ^

Error: Evaluation failed: TypeError: Cannot read properties of undefined (reading 'default') at puppeteer_evaluation_script:5:95 at ExecutionContext._evaluateInternal (D:\nodejs\CTCWhatsappDigital\node_modules\puppeteer\lib\cjs\puppeteer\common\ExecutionContext.js:221:19)

Nosegay answered 24/4, 2024 at 9:4 Comment(1)
This does not provide an answer to the question, this would be more appropriate as a comment. Once you have sufficient reputation you will be able to comment on any post.Elum
O
1

Navigate to the file:

Users/abc/Desktop/haba/node_modules/whatsapp-web.js/src/webCache/LocalWebCache.js

Replace this line (at line: 29):

const version = indexHtml.match(/manifest-([\d.]+).json/)[1];

With this line:

const version = indexHtml.match(/manifest-([\d.]+).json/)?.[1];
Ossified answered 1/5, 2024 at 15:6 Comment(0)
D
1

used this version "https://raw.githubusercontent.com/wppconnect-team/wa-version/main/html/2.2413.51-beta.html" worked for me for now!

Disregardful answered 29/6, 2024 at 23:57 Comment(3)
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From ReviewDalhousie
Looks like we have to update the file every time there's a new one...quite annoying, but thanks!Perdition
its broken the link doesnot workDesimone
S
1

WhatsApp released 2.3000x, a version that does not use Webpack anymore. WWebJS had to change the Store system to no longer use moduleRaid. Instability issues are caused by relying on selectors & DOM Observers for login, as well as increased performance issues.

You should immediately migrate to the up-to-date version and cease using any other fork or version. Your system will work regardless if you are using a web cache, local cache, no cache.. LocalAuth, RemoteAuth, NoAuth... 2.24 and 2.3000.

This will fix the issue.

Check this out.

https://github.com/pedroslopez/whatsapp-web.js/pull/2816#issuecomment-2204732956

Scrivens answered 10/7, 2024 at 0:53 Comment(0)
T
0

This code tries to get the version if it fails it uses a default version which currently is the latest

async persist(indexHtml) {
  
    let version = indexHtml.match(/manifest-([\d\\.]+)\.json/);
    version = version ? version[1] : '2.24.7.72';

    if (!version) return;

    const filePath = path.join(this.path, `${version}.html`);
    fs.mkdirSync(this.path, { recursive: true });
    fs.writeFileSync(filePath, indexHtml);
}
Tat answered 22/5, 2024 at 18:17 Comment(0)
L
0

Try this :

Create a file named classes.js & and paste this and try again !

const { Client, WhatsWebURL } = require("whatsapp-web.js");
const {
  WebCache,
  VersionResolveError,
} = require("whatsapp-web.js/src/webCache/WebCache");
const path = require("path");
const fs = require("fs");
const {
  createWebCache,
} = require("whatsapp-web.js/src/webCache/WebCacheFactory");

class WhatsAppWebClient extends Client {
  async initWebVersionCache() {
    const { type: webCacheType, ...webCacheOptions } =
      this.options.webVersionCache;
    let webCache;
    if (webCacheType === "local") {
      webCache = new LocalWebCache(webCacheOptions);
    } else {
      webCache = createWebCache(webCacheType, webCacheOptions);
    }

    const requestedVersion = this.options.webVersion;
    const versionContent = await webCache.resolve(requestedVersion);

    if (versionContent) {
      await this.pupPage.setRequestInterception(true);
      this.pupPage.on("request", async (req) => {
        if (req.url() === WhatsWebURL) {
          req.respond({
            status: 200,
            contentType: "text/html",
            body: versionContent,
          });
        } else {
          req.continue();
        }
      });
    } else {
      this.pupPage.on("response", async (res) => {
        if (res.ok() && res.url() === WhatsWebURL) {
          await webCache.persist(await res.text());
        }
      });
    }
  }
}

/**
 * LocalWebCache - Fetches a WhatsApp Web version from a local file store
 * @param {object} options - options
 * @param {string} options.path - Path to the directory where cached versions are saved, default is: "./.wwebjs_cache/"
 * @param {boolean} options.strict - If true, will throw an error if the requested version can't be fetched. If false, will resolve to the latest version.
 */
class LocalWebCache extends WebCache {
  constructor(options = {}) {
    super();

    this.path = options.path || "./.wwebjs_cache/";
    this.strict = options.strict || false;
  }

  async resolve(version) {
    const filePath = path.join(this.path, `${version}.html`);

    try {
      return fs.readFileSync(filePath, "utf-8");
    } catch (err) {
      if (this.strict)
        throw new VersionResolveError(
          `Couldn't load version ${version} from the cache`
        );
      return null;
    }
  }

  // L'erreur
  //   async persist(indexHtml) {
  //     // extract version from index (e.g. manifest-2.2206.9.json -> 2.2206.9)
  //     const version = indexHtml.match(/manifest-([\d\\.]+)\.json/)[1];
  //     if (!version) return;

  //     const filePath = path.join(this.path, `${version}.html`);
  //     fs.mkdirSync(this.path, { recursive: true });
  //     fs.writeFileSync(filePath, indexHtml);
  //   }

  // La correction
  async persist(indexHtml) {
    // extract version from index (e.g. manifest-2.2206.9.json -> 2.2206.9)
    if (indexHtml.match(/manifest-([\d\\.]+)\.json/) != null) {
      const version = indexHtml.match(/manifest-([\d\\.]+)\.json/)[1];
      if (!version) return;

      const filePath = path.join(this.path, `${version}.html`);
      fs.mkdirSync(this.path, { recursive: true });
      fs.writeFileSync(filePath, indexHtml);
    }
  }
}

module.exports = {
  WhatsAppWebClient,
};
Lyall answered 15/6, 2024 at 17:35 Comment(0)
D
0

con node 20.14.0

const { Client, MessageMedia } = require("whatsapp-web.js");

//const client = new Client(puppeteerOptions);
const client = new Client({
  webVersionCache: {
    type: "remote",
    remotePath:
      "https://raw.githubusercontent.com/wppconnect-team/wa-version/main/html/2.2412.54.html",
  },
  puppeteer: {
    args: [
      '--no-sandbox',
      '--disable-setuid-sandbox'
    ],
    //authStrategy: // what ever authStrategy you are using
  },
});
Disjunctive answered 19/6, 2024 at 23:55 Comment(0)
C
0

replace the persist(indexHtml) code with following code

async persist(indexHtml) {
    // extract version from index (e.g. manifest-2.2206.9.json -> 2.2206.9)
    
    if (indexHtml.match(/manifest-([\d\\.]+)\.json/) ==  null) {
        return;
    }
    
    const version = indexHtml.match(/manifest-([\d\\.]+)\.json/)[1];
    if(!version) return;

    const filePath = path.join(this.path, `${version}.html`);
    fs.mkdirSync(this.path, { recursive: true });
    fs.writeFileSync(filePath, indexHtml);
}
Confidence answered 22/7, 2024 at 8:19 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Toledo
M
-2

replace

'const version = indexHtml.match(/manifest-([\d\.]+).json/)[1];'

to

'const version = indexHtml.match(/manifest-([\d\.]+).json/);'

Mev answered 12/4, 2024 at 5:28 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.