fent / node-ytdl-core

YouTube video downloader in javascript.
MIT License
4.54k stars 803 forks source link

SyntaxError: Invalid or unexpected token #1261

Open matthieuEv opened 1 year ago

matthieuEv commented 1 year ago

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.

juliand665 commented 1 year ago

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!

ftrbnd commented 1 year ago

Yeah I also just started getting this error today. My app was working fine yesterday, and one of my users just notified me that one my features that uses ytdl-core wasn't working. Hope it gets fixed soon!

Exorcismus commented 1 year ago

same error

akrom123 commented 1 year ago

I have the same error :(

avallois commented 1 year ago

Same ! Showing like this for me:

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:93:7)
    at Object.exports.decipherFormats (/usr/src/app/node_modules/ytdl-core/lib/sig.js:116:51)
    at processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async Promise.all (index 0)
    at async exports.getInfo (/usr/src/app/node_modules/ytdl-core/lib/info.js:401:17)
Exorcismus commented 1 year ago

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))

LucasSantos14 commented 1 year ago

I'm also facing this problem...

juanfjk commented 1 year ago

Same issue: Invalid or unexpected token

DerekCounihan commented 1 year ago

+1\

Screenshot 2023-11-15 at 01 08 15
Zuz111 commented 1 year ago

https://github.com/fent/node-ytdl-core/issues/1251#issuecomment-1709425519

Try it. Work for me.

duykhanh09103 commented 1 year ago

+1 i got it today too without changing any of the code

timfong888 commented 1 year ago

I got a similar error without changing any code as well:

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);"
severity: "ERROR"
textPayload: "SyntaxError: Invalid or unexpected token
    at new Script (node:vm:94:7)
    at exports.decipherFormats (/workspace/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 (/workspace/node_modules/ytdl-core/lib/info.js:401:17)"
timestamp: "2023-11-14T21:57:40.661609Z"
trace: "projects/sophia-db784/traces/d776aacfe766abe87cba7cc4def33496"
libhide commented 1 year ago

Seeing this issue as well without any changes to code.

MelleNooijen commented 1 year ago

I get the following error. Same with @khlevon 's PR sadly.

Error occurred in handler for 'main:getVideoInfo': 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:100:7)
    at Object.exports.decipherFormats (/Users/melle/Git/JYTDownload/node_modules/ytdl-core/lib/sig.js:116:51)
    at processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async Promise.all (index 0)
    at async exports.getInfo (/Users/melle/Git/JYTDownload/node_modules/ytdl-core/lib/info.js:401:17)
fpsone commented 1 year ago

You can try this solution: https://github.com/fent/node-ytdl-core/issues/1252#issuecomment-1734429635

LucasSantos14 commented 1 year ago

This way you solved my problem! https://github.com/fent/node-ytdl-core/issues/1252#issuecomment-1734429635

avallois commented 1 year ago

You can try this solution: #1252 (comment)

This worked for me, thanks a lot

itJunky commented 1 year ago

Same error, and solution with npm i ytdl-core@npm:@distube/ytdl-core doesn't help me (

johanbry commented 1 year ago

Same problem, but using this patched version worked for me, from PR #1217.

"ytdl-core": "git+ssh://git@github.com:khlevon/node-ytdl-core.git#v4.11.4-patch.2"