tsc is not recognized as internal or external command

160,428

Solution 1

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.

Solution 2

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

Solution 3

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.

Solution 4

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.

Solution 5

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.

Share:
160,428
Brent Arias
Author by

Brent Arias

Brent is a full-stack, hands-on software and cloud architect. He can be reached at [email protected]. LinkedIn profile My company AxisCode

Updated on July 05, 2022

Comments

  • Brent Arias
    Brent Arias almost 2 years

    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"
    ]
    
  • Brent Arias
    Brent Arias over 8 years
    This worked, but I don't understand. The documentation in the Install TypeScript locally bullet says nothing about the adjustment you've mentioned. Other documentation 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?
  • zlumer
    zlumer over 8 years
    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.
  • Neutrino
    Neutrino almost 6 years
    What does that even mean?
  • pabrams
    pabrams almost 6 years
    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.
  • zlumer
    zlumer almost 6 years
    @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
    zlumer almost 6 years
    It's 10x times easier now with ts-node, tsc --init and automatic build settings in VSCode.
  • Retro Code
    Retro Code about 4 years
    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!
  • user3162879
    user3162879 over 3 years
    In some cases, if you have installed tsc globally but still getting the error, restarting VSCode might help.
  • Stephen Pham
    Stephen Pham over 3 years
    npx comes with npm starting version 5.2 so you might not need to install it separately.
  • Alice Purcell
    Alice Purcell over 2 years
    Note: you can run this command from VSCode's Terminal (View > Command Palette, and type Javascript Debug Terminal)
  • JesseBoyd
    JesseBoyd about 2 years
    i had to run 'tsc --init' (creates tsconfig.ts if it's missing in project) before i could call 'tsc --watch'
  • Nam Lê Quý
    Nam Lê Quý about 2 years
    how to modify my .vscode/tasks.json. I can't find this file
  • Nam Lê Quý
    Nam Lê Quý about 2 years
    I installed typescript globally but still got errors.
  • Nam Lê Quý
    Nam Lê Quý about 2 years
    I have installed tsc globally but still getting the error