node-ytdl-core: SyntaxError: Invalid or unexpected token

I was working on a project, when suddently, ytdl-core stoped working. So i created a new project to try a simple code:

const fs = require('fs');
const ytdl = require('ytdl-core');

const url = 'https://www.youtube.com/watch?v=dQw4w9WgXcQ';

ytdl(url)
  .pipe(fs.createWriteStream('video.mp4'));

I have the latest package of ytdl: ytdl-core@4.11.5

But when i run the code: node index.js, i get the error:

evalmachine.<anonymous>:426
koa=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,JB(d),h;}a=c.getResponseHeader("Content-Type")||"";f&&0<=a.indexOf("json")&&(")]};gma(ncode);
                                                                                                                                                                                                                             ^^^^^^^^^^^^^^^^

SyntaxError: Invalid or unexpected token
    at new Script (node:vm:94:7)
    at exports.decipherFormats (/tmp/test/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 (/tmp/test/node_modules/ytdl-core/lib/info.js:401:17)
Emitted 'error' event on PassThrough instance at:
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

Node.js v18.17.1

I’m not sure what appened, but it was working just fine, and suddently, this appear out of knowhere, without modifying anything.

About this issue

  • Original URL
  • State: open
  • Created 8 months ago
  • Reactions: 20
  • Comments: 19

Most upvoted comments

I’m getting the same error in my project without changing anything. Presumably YouTube changed something up, but I certainly didn’t expect that to produce an error like this!

as a quick investigation, I beleive the error is related to funcion extractNCode in sig.js

oa=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,JB(d),h;}a=c.getResponseHeader("Content-Type")||"";f&&0<=a.indexOf("json")&&(")]}'\n"===f.substring(0,5)&&(f=f.substring(5)),e=JSON.parse(f));break;case "XML":if(a=(a=c.responseXML)?moa(a):null)e={},g.cc(a.getElementsByTagName("*"),function(h){e[h.tagName]=noa(h)})}d&&ooa(e);

is being mis-parsed

could be related to function utils.cutAfterJS failing to parse (")]}'\n"===f.substring(0,5)&&(f=f.substring(5)),e=JSON.parse(f))