tsc is not recognized as internal or external comm

2020-01-31 19:15发布

问题:

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"
]

回答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.



回答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 file.ts // file.ts will be converted to file.js file
tsc         // all .ts files will be converted to .js files in the directory
tsc --watch // converts all .ts files to .js, and watch changes in .ts files


回答3:

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.



回答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.



回答5:

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



回答6:

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

npm install -g typescript


回答7:

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.