Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"tsc" command showing, "SyntaxError: Unexpected token ?"

I'm new in typescript, I tried to convert typescript file into javascript file, by running command tsc filename.ts. But It's showing error.

C:\Program Files (x86)\Nodist\bin\node_modules\typescript\lib\tsc.js:93
  for (let i = startIndex ?? 0; i < array.length; i++) {
                           ^

SyntaxError: Unexpected token ?
    at Module._compile (internal/modules/cjs/loader.js:749:23)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:816:10)
    at Module.load (internal/modules/cjs/loader.js:672:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:612:12)
    at Function.Module._load (internal/modules/cjs/loader.js:604:3)
    at Module.require (internal/modules/cjs/loader.js:711:19)
    at require (internal/modules/cjs/helpers.js:14:16)
    at Object.<anonymous> (C:\Program Files (x86)\Nodist\bin\node_modules\typescript\bin\tsc:2:1)
    at Module._compile (internal/modules/cjs/loader.js:805:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:816:10)

I tried to install node to it's latest version.

like image 561
Vedant Yede Avatar asked Sep 10 '25 19:09

Vedant Yede


2 Answers

Indeed, Veerendra is right, typescript now requires node 14.17 as "Minimum Runtime Requirements".

To fix this, I uninstalled nodejs, npm, typescript, ... completely from my system (Ubuntu 22) and then installed nvm. Go directly to this line in the readme to see the commands to install nvm. Now, with nvm you can choose any version of nodejs you want to install; run the command

$ nvm ls-remote

to list all versions available. At the time of writing, I chose to install v18.17.0:

$ nvm install v18.17.0

Installing any version of nodejs also gets you npm. Then, I installed all the packages using npm as usual, and after that the command tsc worked fine.

like image 155
Lluís Alemany-Puig Avatar answered Sep 12 '25 08:09

Lluís Alemany-Puig


I faced the same issue with TypeScript 5.1.6 and Node 12.x version.

Because of this combination you get this issue. I downgraded my TypeScript to 4.9.5 and it worked for me fine.

Please check the image below for reference:

https://www.typescriptlang.org/docs/handbook/release-notes/typescript-5-1.html

Screenshot of Change Log

like image 42
Veerendra Kumar Nagella Avatar answered Sep 12 '25 08:09

Veerendra Kumar Nagella