111

I updated from VSCode 0.10.6 to 0.10.8, and tried using Typescript for the first time. Unfortunately I when I tell VSCode to build, I get the error:

tsc is not a recognized as an internal or external command...

Here are the relevant details:

  • I created a fresh "HelloWorld" project according to VS Code instructions. This included:
    • I ran npm init for a new package.json
    • I ran npm i --save-dev typescript because I want a local install, rather than a global install.
    • I created a launch.json to define a node.js project.
    • I created the tasks.json file, with prescribed settings for tsc.
  • I have made a settings.json file, as shown here. It did not help.
  • I do have Visual Studio 2015 Community installed, but I have not installed a Typescript extension of any kind. When I type "where tsc" at a developer command prompt, it replies "could not find". I assume this is a good thing.

I have restarted VSCode (several times). What am I missing? What more must be done?

Update

I tried the solution offered by @zlumer. It succeeded in making the typescript compiler run, but then it caused thousands of errors to appear. To fix that, I also had to adjust my tsconfig.json to exclude the node_modules folder:

"exclude": [
    "node_modules"
]
Community
  • 1
  • 1
Brent Arias
  • 29,277
  • 40
  • 133
  • 234

18 Answers18

165

There might be a reason that Typescript is not installed globally, so install it

npm install -g typescript // installs typescript globally

If you want to convert .ts files into .js, do this as per your need

tsc path/file.ts // file.ts will be converted to file.js
tsc              // all .ts files will be converted to .js files with in the directory
tsc --watch      // converts all .ts files to .js, and watch changes in .ts files
WasiF
  • 26,101
  • 16
  • 120
  • 128
55

The problem is that tsc is not in your PATH if installed locally.

You should modify your .vscode/tasks.json to include full path to tsc.

The line to change is probably equal to "command": "tsc".

You should change it to "command": "node" and add the following to your args: "args": ["${workspaceRoot}\\node_modules\\typescript\\bin\\tsc"] (on Windows).

This will instruct VSCode to:

  1. Run NodeJS (it should be installed globally).
  2. Pass your local Typescript installation as the script to run.

(that's pretty much what tsc executable does)

Are you sure you don't want to install Typescript globally? It should make things easier, especially if you're just starting to use it.

zlumer
  • 6,844
  • 1
  • 25
  • 25
  • 1
    This worked, but I don't understand. [The documentation](https://code.visualstudio.com/updates#vscode) in the `Install TypeScript locally` bullet says nothing about the adjustment you've mentioned. [Other documentation](https://code.visualstudio.com/Docs/languages/typescript) says I only need to adjust the settings.json file to point to "a directory containing the TypeScript tsserver.js and the corresponding lib.*.d.ts files." Once I've adjusted my settings.json to indicate the proper path, why do I need to adjust the path in tasks.json also? – Brent Arias Feb 12 '16 at 19:13
  • The documentation talks about installing typescript for VSCode. Build tasks (the ones described in tasks.json) are a completely different thing — they just use shell to run `tsc`, `node` or any other executable you provided. The problem is that `tsc` is not in your `PATH`, therefore you have to add the path to the executable. – zlumer Feb 12 '16 at 20:22
  • 2
    Why would we want to install typescript globally? Then there would be a danger of our npm scripts using the wrong version of tsc to compile. I want it to always use the tsc local to the project. – pabrams Jul 24 '18 at 14:36
  • 1
    @pabrams these were the good old times when working with locally installed binaries was very hard in npm and yarn wasn't released yet. Please keep in mind that you're commenting on an answer that's 2.5+ years old. – zlumer Jul 24 '18 at 18:12
  • 1
    It's 10x times easier now with `ts-node`, `tsc --init` and automatic build settings in VSCode. – zlumer Jul 24 '18 at 18:14
  • In some cases, if you have installed tsc globally but still getting the error, restarting VSCode might help. – 1fuyouinfinite Oct 05 '20 at 16:52
  • how to modify my .vscode/tasks.json. I can't find this file – Nam Lee May 11 '22 at 11:10
  • 1
    I have installed tsc globally but still getting the error – Nam Lee May 11 '22 at 11:15
48

You need to run:

npx tsc

...rather than just calling tsc own its on like a Windows command as everyone else seems to be suggesting.

If you don't have npx installed then you should. It should be installed globally (unlike Typescript). So first run:

npm install -g npx

..then run npx tsc.

MSOACC
  • 3,074
  • 2
  • 29
  • 50
  • 7
    npx comes with npm starting version [5.2](https://nodejs.dev/learn/the-npx-nodejs-package-runner) so you might not need to install it separately. – Stephen Pham Nov 29 '20 at 23:18
20

For windows

After installing typescript globally

npm install typescript -g 

just search for "node.js command prompt"

type in command promt

tsc -v 

Here we can see tsc command works, now navigate to your folder and type

tsc filename.ts 

its complies ts to js file.

Ashutosh Jha
  • 15,451
  • 11
  • 52
  • 85
  • 3
    Very good! It worked! Nonetheless, I do not comprehend what the problem is with tsc itself. I have installed typescript globally and it should work inside the VSCODE terminal! – Retro Code Mar 02 '20 at 19:59
8

In the VSCode file tasks.json, the "command": "tsc" will try to find the tsc windows command script in some folder that it deems to be your modules folder.

If you know where the command npm install -g typescript or npm install typescript is saving to, I would recommend replacing:

"command": "tsc"

with

"command": "D:\\Projects\\TS\\Tutorial\\node_modules\\.bin\\tsc"

where D:\\...\\bin is the folder that contains my tsc windows executable

Will determine where my vscode is natively pointing to right now to find the tsc and fix it I guess.

Danh
  • 5,916
  • 7
  • 30
  • 45
dropbeardan
  • 1,080
  • 10
  • 11
4

tsc is not recognized as internal or external command

As mentioned in another answer this is because tsc is not present in path.

1. Install as global package

To make TypeScript compiler available to all directories for this user, run the below command:

npm install -g typescript

You will see something similar to

C:\Users\username\AppData\Roaming\npm\tsserver -> C:\Users\username\AppData\Roaming\npm\node_modules\typescript\bin\tsserver C:\Users\username\AppData\Roaming\npm\tsc -> C:\Users\username\AppData\Roaming\npm\node_modules\typescript\bin\tsc + typescript@3.6.3 added 1 package from 1 contributor in 4.769s

2. Set the environment variable

  • Add the npm installation folder to your "user variables" AND "environment variables".

  • In windows you can add environment variable PATH with value

    C:\Users\username\AppData\Roaming\npm\

i.e. wherever the npm installation folder is present.

Note: If multiple Paths are present separate them with a ;(semicolon)

If the below command gives the version then you have successfully installed

tsc --version
Dharman
  • 30,962
  • 25
  • 85
  • 135
Keegs
  • 460
  • 6
  • 12
4

The error is due to installing the dependency locally. I had this issue resolved by:

  1. $ npm install -g typescript Thereafter setting up the path in environment variables:
  2. C:\Users\User\AppData\Roaming\npm
  3. tsc -v
2

You have missed typescript installation, just run below command and then try tsc --init

npm install -g typescript
Nicholas K
  • 15,148
  • 7
  • 31
  • 57
2

If you want to run the tsc command from the integrated terminal with the TypeScript module installed locally, you can add the following to your .vscode\settings.json file.

{
  "terminal.integrated.env.windows": { "PATH": "${workspaceFolder}\\node_modules\\.bin;${env:PATH}" }
}

This will prepend the locally installed node module's binary/executable directory (where tsc.cmd is located) to the $env.PATH variable.

matt.baker
  • 232
  • 1
  • 9
2

One more scenario of this error:

Install typescript locally and run the command without npm run

First, it is important to notice this is a "general" terminal error (Even if you write hello bla.js -or- wowowowow index.js):

enter image description here

"hello world" example of this error:

  1. You install typescript locally (without -g) ==> npm install typescript. https://docs.npmjs.com/downloading-and-installing-packages-locally
  2. In this case tsc commands available if you run npm run inside your local project. For example: npm run tsc -v:

enter image description here

-or- install typescript globally (Like other answer mention).

Ezra Siton
  • 6,887
  • 2
  • 25
  • 37
1

Me too faced the same problem. Use nodeJS command prompt instead of windows command prompt.

Step 1: Execute the npm install -g typescript

Step 2: tsc filename.ts

New file will be create same name and different extension as ".js"

Step 3: node filename.js

You can see output in screen. It works for me.

Srinivasan.S
  • 3,065
  • 1
  • 24
  • 15
0

If none of the above make sense, especially if it used to work previously. Here is my solution: rm -f node_modules and npm i.

My problem was that I ran rm -rf node_modules for a second or so and stopped it. Then I forgot about it and I have also ran npm i. Then I got the tsc not recognized error. So I tried to run npm i to make sure everything is up to date, and it said it it was, which it was not.

ZenVentzi
  • 3,945
  • 3
  • 33
  • 48
0
  • Install TpyeScript :

npm install -g typescript

  • Use command prompt

  • Close your command prompt and open it again

  • For PowerShell bug fixing execute this command in your powershell :

Set-ExecutionPolicy -scope currentuser -executionpolicy remotesigned

M Komaei
  • 7,006
  • 2
  • 28
  • 34
0

It seems on windows one now has to run "Set-ExecutionPolicy -ExecutionPolicy RemoteSigned" to run the tsc command now. You can check the permissions by running tsc from "C:\Users<user>\AppData\Roaming\npm".

MoleIsKing
  • 131
  • 5
0

If tsc is not working simply use with npx

npx tsc myfile.ts (this would create your file)

npx tsc --version (to check tsc version

it would work! enjoy coding.

haroon kiani
  • 199
  • 1
  • 5
0

If you are using a local installation of typescript , Check inside your node_modules.bin folder. Do you see any files named as tsc , if not then this could be the issue , follow the below steps

  • Delete the node_modules folder
  • run npm cache clean --force
  • then npm i
  • then run npx tsc filename.ts

In addition you can add "scripts" : { "build" : "tsc" } in package.json and then npm run build -- filename.ts.

Now you will be able to see the converted js files.

Prashant Joshi
  • 445
  • 6
  • 9
-1

For me, by running Visual Studio Code as Administrator, the problem is resolved.

hoanvd1210
  • 149
  • 5
  • 15
-2

Alternatively you can use npm which automatically looks into the .bin folder. Then you can use tsc

Dirk Bäumer
  • 1,183
  • 9
  • 7