node-ytdl-core icon indicating copy to clipboard operation
node-ytdl-core copied to clipboard

SyntaxError: Invalid or unexpected token

Open matthieuEv opened this issue 2 years ago • 19 comments

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: [email protected]

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.

matthieuEv avatar Nov 14 '23 22:11 matthieuEv

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!

juliand665 avatar Nov 14 '23 22:11 juliand665

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!

ftrbnd avatar Nov 14 '23 22:11 ftrbnd

same error

Exorcismus avatar Nov 14 '23 22:11 Exorcismus

I have the same error :(

akrom123 avatar Nov 14 '23 23:11 akrom123

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)

avallois avatar Nov 14 '23 23:11 avallois

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

Exorcismus avatar Nov 14 '23 23:11 Exorcismus

I'm also facing this problem...

LucasSantos14 avatar Nov 15 '23 00:11 LucasSantos14

Same issue: Invalid or unexpected token

juanfjk avatar Nov 15 '23 01:11 juanfjk

+1
Screenshot 2023-11-15 at 01 08 15

DerekCounihan avatar Nov 15 '23 01:11 DerekCounihan

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

Try it. Work for me.

Zuz111 avatar Nov 15 '23 02:11 Zuz111

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

duykhanh09103 avatar Nov 15 '23 04:11 duykhanh09103

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"

timfong888 avatar Nov 15 '23 05:11 timfong888

Seeing this issue as well without any changes to code.

libhide avatar Nov 15 '23 10:11 libhide

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)

MelleNooijen avatar Nov 15 '23 12:11 MelleNooijen

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

fpsone avatar Nov 15 '23 12:11 fpsone

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

LucasSantos14 avatar Nov 15 '23 13:11 LucasSantos14

You can try this solution: #1252 (comment)

This worked for me, thanks a lot

avallois avatar Nov 15 '23 20:11 avallois

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

itJunky avatar Nov 16 '23 01:11 itJunky

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

"ytdl-core": "git+ssh://[email protected]:khlevon/node-ytdl-core.git#v4.11.4-patch.2"

johanbry avatar Nov 20 '23 09:11 johanbry