node-ytdl-core: getInfo giving me 'Invalid or unexpected token' error

Trying to get the video information from ytdl.getInfo() but I get SyntaxError: Invalid or unexpected token instead. Yes, I am using the latest version of ytdl-core.

Getting data via this code:

const videoData = await ytdl.getInfo(url); // url is: https://www.youtube.com/watch?v=SPzumRExEqw

Error that I am getting:

rma=function(a,b,c,d){var e=null;switch(b){case "JSON":try{var f=c.responseText}catch(h){throw d=Error("Error reading responseText"),d.params=a,bu(d),h;}a=c.getResponseHeader("Content-Type")||"";f&&0<=a.indexOf("json")&&(")]};Yka(ncode);
                                                                                                                                                                                                                             ^^^^^^^^^^^^^^^^

SyntaxError: Invalid or unexpected token
    at new Script (node:vm:100:7)
    at Object.exports.decipherFormats (C:\Users\null\Desktop\yt-clips\node_modules\ytdl-core\lib\sig.js:116:51)
    at runMicrotasks (<anonymous>)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async Promise.all (index 0)
    at async exports.getInfo (C:\Users\null\Desktop\yt-clips\node_modules\ytdl-core\lib\info.js:401:17)

It happened after getting data of the url approx 20 times, am I getting ratelimited? Thank you.

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 8
  • Comments: 37 (2 by maintainers)

Commits related to this issue

Most upvoted comments

https://github.com/fent/node-ytdl-core/blob/11103be3ab373551bddd0938bc0bbaea033acb8e/lib/sig.js#L63-L67

if (ndx >= 0) {
    const end = body.indexOf('.join("")};', ndx);
    const subBody = body.slice(ndx, end);

    const functionBody = `${subBody}.join("")};${functionName}(ncode);`;
    functions.push(functionBody);
}

A temporary fix until someone finds a better solution.

downgrading to 4.9.1 fixes for me

Version 4.11.0 is working for me again

i’m having trouble with this problem

`Ama=function(a,b,c,d){var e=null;switch(b){case “JSON”:try{var f=c.responseText}catch(h){throw d=Error(“Error reading responseText”),d.params=a,oC(d),h;}a=c.getResponseHeader(“Content-Type”)||“”;f&&0<=a.indexOf(“json”)&&(")]};Ila(ncode); ^^^^^^^^^^^^^^^^

SyntaxError: Invalid or unexpected token at new Script (node:vm💯7) at Object.exports.decipherFormats (F:\www\revutool\node_modules\ytdl-core\lib\sig.js:116:51) at runMicrotasks (<anonymous>) at processTicksAndRejections (node:internal/process/task_queues:96:5) at async Promise.all (index 0) at exports.getInfo (F:\www\example\node_modules\ytdl-core\lib\info.js:401:17)`

Here’s a proper patch from @WaqasIbrahim’s changes:

--- a/lib/sig.js
+++ b/lib/sig.js
@@ -63,5 +63,7 @@
       if (ndx >= 0) {
-        const subBody = body.slice(ndx + functionStart.length);
-        const functionBody = `var ${functionStart}${utils.cutAfterJSON(subBody)};${functionName}(ncode);`;
+        const end = body.indexOf('.join("")};', ndx);
+        const subBody = body.slice(ndx, end);
+        
+        const functionBody = `${subBody}.join("")};${functionName}(ncode);`;
         functions.push(functionBody);
       }

This worked for me, applied inside the node_modules/ytdl-core folder.

Go to node_modules/@distube delete ytdl-core folder then open cmd and type this command npm i ytdl-core@4.9.1. Go to node_modules and copy ytdl-core file and paste it into node_modules/@distube

It works 100%.

Throttled to about 10mb a minute but its a good temporary fix. Much appreciated!

I seem to still getting the issue with latest and downgraded issues. Its not giving a stack trace, just throwing an error “SyntaxError: Invalid or unexpected token”

Ama=function(a,b,c,d){var e=null;switch(b){case "JSON":try{var f=c.responseText}catch(h){throw d=Error("Error reading responseText"),d.params=a,oC(d),h;}a=c.getResponseHeader("Content-Type")||"";f&&0<=a.indexOf("json")&&(")]};Ila(ncode);
                                                                                                                                                                                                                             ^^^^^^^^^^^^^^^^

SyntaxError: Invalid or unexpected token
    at new Script (node:vm:100:7)
    at exports.decipherFormats (/Users/michaeldale/Documents/GitHub/lambda-yt-mrss-ingest/node_modules/ytdl-core/lib/sig.js:116:51)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async Promise.all (index 0)
    at async exports.getInfo (/Users/michaeldale/Documents/GitHub/lambda-yt-mrss-ingest/node_modules/ytdl-core/lib/info.js:401:17)```

I am also just getting this issue out of the blue. I did not even update my ytdl-core. This seems like a major issue.

I had the error, then it went away… today it’s same stack trace but different error

SyntaxError: Missing catch or finally after try
    at new Script (node:vm:100:7)
    at Object.exports.decipherFormats (C:\Users\Yoduh\Desktop\coding\bloop-bot\node_modules\ytdl-core\lib\sig.js:116:51)
    at runMicrotasks (<anonymous>)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async Promise.all (index 0)
    at async exports.getInfo (C:\Users\Yoduh\Desktop\coding\bloop-bot\node_modules\ytdl-core\lib\info.js:401:17)

4.11.0 btw

downgrading to v 4.10.0 worked for me

Thank you!

Same issue on v.4.11.5. Downgrading didn’t help Patches above didn’t help. Did youtube change smth or what happened?

image

(update)

this fixed it for me for now https://github.com/fent/node-ytdl-core/issues/1252#issuecomment-1711107133

image (into your package.json)

@WaqasIbrahim Thanks for looking into this, FYI you’ve got a typo on line 3 of your fix, index should be ndx.