typescript compiler (tsc) command not working with tsconfig

48,199

Solution 1

this is the problem:

C:\Program Files (x86)\Microsoft SDKs\TypeScript\1.0\tsc.exe
C:\Program Files (x86)\Microsoft SDKs\TypeScript\1.0\tsc.js

uninstall-update-remove-get-rid-off: Visual Studio outdated extensions...

or remove it from the path ...

or rename the folder to confirm the problem ... then nuke it :)

check what happens if you do:

md x
cd x
tsc --init
npm init -y
npm link typescript
echo console.log('it works') > index.ts
tsc -p .
node .

should output

it works

also. I'll need install typescript local to the project if
a module you depend on, depends on it
you need to use a compiler feature in "your" code
you need to use a different version than the installed globally

to init:

tsc --init

to compile

a 'project' (based on tsconfig.json):

tsc -p .

where . means here

to compile 'other' project

tsc -p other/tsconfig.json

More help

Solution 2

The issue I had was that I wasn't getting any errors but I also wasn't getting any output.

I realised that I had "noEmit": true in my tsconfig.json file.

When I removed that it worked as expected :)

Solution 3

What I did to adjust Typescript version of tsc command on my Windows system was:

Editing system environment PATH variable

Removing the Typescript 1.0 path here. (Start button-> Type : environment variables, click on the option (English version of Windows here) and choosing the system environment variable PATH).

Afterwards I entered:

npm link typescript

And then I used the refreshenv command of Chocolatey to refresh the system PATH environment variable I adjusted.

refreshenv

After then running the command: tsc -v I got: Version 2.2.1

The current version of Typescript is 3.5+, but I globally installed Typescript 2.2.1 because I am following a Typescript course using that version.

Share:
48,199

Related videos on Youtube

bnsaa
Author by

bnsaa

Updated on August 08, 2022

Comments

  • bnsaa
    bnsaa 10 months

    I installed typescript globally ( npm install typescript -g )

    Then created a folder, ran npm --init, then npm intall typescript --save-dev - it installed [email protected]

    In the folder , I create 'helloworld.ts`

    var msg = 'Hello World';
    console.log (msg);
    

    ran tsc command with file option - tsc helloworld.ts and see it compiled to helloworld.js.

    Next, I want to use tsconfig.json, so I run tsc --init - this doesn't work, says Unknown option 'init'

    i say alright, let me try adding tsconfig.json manually and add it in the folder root as below:

    {
        "compilerOptions": {
            "target": "es5"
        },
        "files": [
            "helloworld.ts"
        ]
    }
    

    and I run tsc on command prompt, but it won't work and outputs me the syntax, example and options of how to use tsc Syntax: tsc [options] [file] ...

    whats wrong?

    where tsc gives below:

    C:\Program Files (x86)\Microsoft SDKs\TypeScript\1.0\tsc.exe
    C:\Program Files (x86)\Microsoft SDKs\TypeScript\1.0\tsc.js
    C:\Users\Kap\AppData\Roaming\npm\tsc
    C:\Users\Kap\AppData\Roaming\npm\tsc.cmd
    
  • bnsaa
    bnsaa over 6 years
    thanks Dan, but it doesn't work for me: tsc init yields ` error TS5007: Cannot resolve referenced file: 'init' ` and tsc -p . gives me ` error TS5023: Unknown option 'p' `. I have a feeling that I may have some old version incompatible version of tsc with [email protected], but not sure how to fix it, tried uninstalling / reinstalling typescript but no luck so far
  • Dan
    Dan over 6 years
    sorry ..my bad , don't know what I was thinking. its "tsc --init", Ill edit the answer,
  • Dan
    Dan over 6 years
    @bnsaa : I see I was thinking ...npm --init is not a flag ;)
  • bnsaa
    bnsaa over 6 years
    yup, that was the issue, funny thing , I did removed that from PATH yesterday but still it was not working, so at the end I shut it down and went for sleep, this morning when I started, its working, so I guess it needed a restart :), anyways, thanks Dan
  • bnsaa
    bnsaa over 6 years
    for those who may encounter same issue, this is what worked for me - remove/uninstall 'Typescript tools for Visual Studio' from "Windows - Add/Remove Programs". Then remove from the PATH env variable as Dan mentioned above, uninstall typescript npm uninstall typescript -g, restart the system and reinstall typescript t npm install typescript -g. After this tsc is working all good
  • Drenai
    Drenai about 6 years
    Same problem as original question. I removed the env variable from Path: Microsoft SDKs/Typescript/1.0 - and after 10 attempts remembered that I had to close/reopen my Powershell/command prompt!! But then it worked fine. Thanks for the answer!
  • Loi Nguyen Huynh
    Loi Nguyen Huynh over 1 year
    Saved my day, I mean my evening (dinner)