65

Just installed Typescript extension to VS2012 and followed Install TypeScript for Visual Studio 2012 and then the tutorial to call the compiler:

> tsc greeter.ts

But when i try to compile .ts file where should i type: tsc greeter.ts? Tried it in VS command line and in windows console, always get the message that tsc is not recognized as command(Command "tsc" is not valid.).

Fenton
  • 241,084
  • 71
  • 387
  • 401
0x49D1
  • 8,505
  • 11
  • 76
  • 127
  • how do you try to compile? at the commandline or via the GUI? – hakre Oct 02 '12 at 07:41
  • possible duplicate of: [javac is not recognized as an internal or external command, operable program or batch file](http://stackoverflow.com/questions/7709041/javac-is-not-recognized-as-an-internal-or-external-command-operable-program-or) – hakre Oct 03 '12 at 13:55
  • 3
    @hakre, how can this be a duplicate?..Answer is about .NET SDK! – 0x49D1 Oct 03 '12 at 14:23
  • To call a command on a system, you either need to specify the full path of it or the commands hosting directory is part of the path variable. This is actually identical, only the command differs. However I'd say that this mental action of correlation should be possible for a common programmer. – hakre Oct 03 '12 at 17:56
  • @user11201714 sorry? The question is 6 years old, also it's NOT about installing typescript. – 0x49D1 Mar 14 '19 at 09:00
  • @hakre just revised the question, I've to apologize: sure you were right, this one was the same problem, just for other executable. Still here is a valuable expanded answer for new ones: https://stackoverflow.com/a/40616601/47672 so probably this question+answer has some value. – 0x49D1 Nov 08 '22 at 12:10

14 Answers14

98

If you're using tsc as a node module, make sure you've installed it with

npm install -g typescript

Then it should be available globally in your node command prompt

lhk
  • 27,458
  • 30
  • 122
  • 201
  • I didn't install the node/npm module and can access it. Currently I'm not sure whether it comes with VS 2012 or the TypeScript plugin. – jl. Aug 28 '13 at 10:17
  • 1
    Visual Studio 2012 has no out-of-the-box support for typescript, but the plugin installs the compiler. This download page https://typescript.codeplex.com/releases/view/111080 says that you can install typescript with "An MSI containing tools for Visual Studio, plus the compiler" . If you can access the compiler from your cmd but haven't installed the node module globally, then the Typescript plugin has installed the compiler and added it to your path. – lhk Aug 30 '13 at 09:31
  • If you have installed tsc as a node module, you can also use `npm list -global --depth=0` to check, if it was installed globally and successfully. – pzaenger Sep 03 '16 at 17:05
  • 3
    This is a much better answer than the accepted one. It should solve the same issue regardless of platform or editor software. – claywhipkey Nov 03 '16 at 20:45
  • Where is tsc supposed to be? I installed as indicated, I restarted the command prompt but tsc isn't recognized. Am I missing a path entry or am I missing tsc.exe (or similar) – Florian F Nov 04 '16 at 17:07
  • If on windows, there is a special node command prompt. Could you try that one ? – lhk Nov 17 '16 at 09:37
  • Why would we want to install globally? Then how do we know it's using the right version? – pabrams Jul 24 '18 at 14:39
  • Yes and no: this is NOT the best answer as it answers the case when NPM is installed (the answer even states that "if your're using tsc as node module..."). Sure there are people without NPM installed. The problem was that prompt could not find the tsc executable. Editing the PATH env sure helps even if you just copy tsc (or any other executable you have similar problem with) without doing any additional installation registrations and that's it. The marked as answer answer is sure NOT the best too, but historically it was the first one that resolved the problem in my case. – 0x49D1 Nov 08 '22 at 12:17
47

Ensure you have,

C:\Program Files (x86)\Microsoft SDKs\TypeScript\0.8.0.0

or,

C:\Program Files\Microsoft SDKs\TypeScript\0.8.0.0

on your path. If not, try restarting CMD.EXE and see if shows up with a fresh copy. If that fails, try adding one of the above manually to your path.

chuckj
  • 27,773
  • 7
  • 53
  • 49
  • Nice! Ive the path (x86), tho its x64 windows..Thank you! – 0x49D1 Oct 02 '12 at 07:41
  • 1
    How do I add it to Path? I tried it myself, but it still doesn't work. Maybe I did it wrong? I added "tsc" and "C:\Program Files\Microsoft SDKs\TypeScript\0.8.0.0" to PATH but VS CMD still won't see it as being there. Also, VS CMD doesn't recognise **anything**. – Arrow Oct 25 '12 at 10:20
  • Try changing the name of the path variable to PATH : Or create another with the uppercase name, instead of renaming) – Moulde Dec 15 '12 at 02:54
  • http://windowsitpro.com/systems-management/how-can-i-add-new-folder-my-system-path – Manish Jain Sep 02 '16 at 05:48
  • 4
    I have installed typescript using 'npm install -g typescript' command but I don't find above path's in my Windows 8.1 Pro machine to set the path. – Seshu Vuggina Sep 26 '16 at 07:14
  • In Windows 10, search for *Edit the System Environment Variables* in Windows search and press the *Environment Variables...* button. Under *System Variables* select *Path* and press *Edit*. Use *New* to add a new path, and delete any old typescript entries (if applicable). – Taraz Apr 10 '18 at 22:46
  • Good lord! life saver! – unruledboy Jan 11 '19 at 06:49
  • Was having this problem in my IDE terminal from which I had just run `npm install typescript` Restarting the terminal updated the path for me and all worked. – elgaz Jun 25 '21 at 10:19
16

For folks on Windows with Visual Studio Code, who don't want to install full Visual Studio just for tsc.exe, I can suggest to simply download it from here: https://www.microsoft.com/en-us/download/details.aspx?id=48593.

This is a shame that this link is missing from the TypeScript download page.

Although the installer is called TypeScript for Visual Studio 2015, it works with Visual Studio Code as well.

After you downloaded and installed TypeScript, you should manually add its installation directory (C:\Program Files (x86)\Microsoft SDKs\TypeScript\2.0\ in my case) to the %PATH% environment variable.

Open environment variables editor:

Win + S; e, n, v, i, r, o; click Edit environment variables for your account.

In the window opened find Path user variable (note that it is not named %PATH% here, but still this is it). You probably have some path set there already. You just have to type ;, append the TypeScript install location and add one more ; in the end.

Screenshot for your reference:

enter image description here

After this is done, open Command Prompt and type in tsc -v. If tsc.exe's version is getting displayed, you're done. For this to work, restart Command Prompt and VS Code after making the change to the %PATH%.

P.S. If you get "error TS5057: Cannot find a tsconfig.json file at the specified directory: '.'", just create tsconfig.json file in the document root (that's probably where your .ts files are) with simple contents: {}. This means "an empty JSON file <...>. This will be sufficient for most people." (source).

Neurotransmitter
  • 6,289
  • 2
  • 51
  • 38
  • 1
    "For this to work, restart Command Prompt" Glad you added that point. Thats why it didnt work for me. – RollingInTheDeep Nov 01 '18 at 13:02
  • go to edit environment variable for your account and add following Before "C:\Program Files (x86)\nodejs\" add => "%AppData%\npm". Close command prompt, vs code and re-open again – Vaibhav Jul 26 '19 at 07:36
  • @Vaibhav this implies you have `npm` installed. My answer is more generic and doesn't require any dependencies apart from the `tsc.exe` itself. – Neurotransmitter Feb 05 '20 at 10:39
  • 1
    This is a lifesaver answer, @Neurotransmitter - thank you. I've spent all evening trying to get Visual Studio Code to "see" and run `tsc` and of all the things that I've tried, this is the _first_ process that has worked. (In my case, the installation folder was `C:\Program Files (x86)\Microsoft SDKs\TypeScript\3.2\ ` but, apart from that, I followed your detailed and excellent instructions to the letter - thank you!) – Rounin Nov 20 '20 at 00:34
  • 2
    Update: It seems what I wanted to achieve - installing Visual Studio Code and TypeScript but _without_ having to install Node.js 13.4.0 (the last Node.js compatible with Windows 7) - wasn't possible in the end. Nevertheless, thanks, @Neurotransmitter, for saving me from tearing all of my hair out at a point in my VSC+TS discovery process where literally nothing seemed to be working. – Rounin Nov 20 '20 at 17:39
  • I installed it using the given link, but there does not seem to be a tsc.exe included in the installation. – Gonen I May 13 '21 at 10:34
  • @GonenI tsc.exe should be installed at ```C:\Program Files (x86)\Microsoft SDKs\TypeScript\*```. – Neurotransmitter May 13 '21 at 23:12
  • @Neurotransmitter It wasn't in my case. While the directory and its subfolders were created and contain many .js files and dlls, there was no tsc.exe to befound. Perhaps they decided to no longer include it in the installations. – Gonen I May 14 '21 at 09:23
4

If you have installed typescript for a specific folder/project i.e. not globally,then you can should use tsc command with npx e.g

npx tsc myfile.ts

npx is used to excute a package .

3

Sometimes closing and reopening the command prompt solves the issue.

This is due to that fact that the location of the binary (in this case tsc) is not yet available in the PATH, hence tsc doesn't link to /usr/bin/tsc.

Laszlo
  • 2,225
  • 19
  • 22
2

Check both of these folders to find out what TypeScript version you have:

C:\Program Files (x86)\Microsoft SDKs\TypeScript\
C:\Program Files\Microsoft SDKs\TypeScript\

Right-click the folder showing the version number then 'copy as path'.

Paste this into your system path (quick access - Windows key then type 'env'). Then open a new command prompt console as administrator and 'tsc' should work.

Chris Halcrow
  • 28,994
  • 18
  • 176
  • 206
  • Is there a better way to get the path to `tsc.exe` instead of assuming it's `%PROGRAMFILES(X86)%\Microsoft SDKs\TypeScript\{version}\tsc.exe`? And how do we determine what the version number is? Is there some registry value we can use? – Dai Feb 19 '19 at 07:02
1

As new path variable, don't use the displayed tsc version number but the TypeScript Folder Name which is different.

Sample :

tsc -v display 2.4.1 

but TypeScript directory is

C:\Program Files (x86)\Microsoft SDKs\TypeScript\2.4

BartoszKP
  • 34,786
  • 15
  • 102
  • 130
Vinamis
  • 11
  • 2
1

Just delete AppData\Roaming\npm\tsc.ps1 file and the issue will be fixed.

1

In my case, I did install typescript globally by using -g flag but tsc was not recognized. It turns out that below directory was not included in path environment variable:

C:\Users\Jitendra\AppData\Roaming\npm

Adding this directory to path variable worked for me.

Jitendra Kumar
  • 503
  • 1
  • 6
  • 13
1

Ensure you have on your system's path's

C:\Users\"Your username"\AppData\Local\Programs\MicrosoftVsCode\bin

Change "your username" with your actual username

0

In CMD type where tsc if it doesn't show a path like this C:\Program Files (x86)\Microsoft SDKs\TypeScript\typescript version you installed\ it means your tsc is not running from here.You need to change the environment path manually by following @TranslucentCloud 's answer .

0

You should add the following path C:\Users\<user>\AppData\Roaming\npm to the PATH variable. Notice that you should change the to your windows user. This will work like a charm. Cheers!!

aRvi
  • 2,203
  • 1
  • 14
  • 30
  • Yes, but not quite right: the question is not about npm, it's about typescript compiler. So the right answer is: there should be ```tsc``` executable's directory path in PATH env variable. And ```tsc``` can be not in NPM-s directory. I can have TypeScript without NPM and still use the command line. See the answers above they are right about that. Also: try not to answer already answered questions with the "same" answer (yeah you mentioned NPM instead of ts, but the "core" is same: PATH env), you will just create more confusion for the person who is reading about the problem. – 0x49D1 Nov 08 '22 at 12:02
0

Try

npx tsc greeter.ts

I was getting the same error until i tried this.

Arunjith
  • 1
  • 2
0

In windows and npm new version, If tsc --v is error 'tsc' is not recognized as an internal or external command, operable program or batch file. should be add npm path to env windows as follows:

1- open env (Edit the system environment variables).

2- Clicking the Advance tab.

3- Environment Variables button click.

4- In System variables click Path in its list.

5- Clicking New button and app C:\Users\<user>\AppData\Roaming\npm, that you should change the to your windows user.

6- Clicking OK, and clicking OK buttons of other windows.

Mahdi Afzal
  • 729
  • 10
  • 14