Tsc 不能识别为内部或外部命令

我从 VSCode 0.10.6更新到0.10.8,并且第一次尝试使用 Typecript。不幸的是,当我告诉 VSCode 去构建时,我得到了一个错误:

Tsc 不是一个公认的内部或外部命令..。

以下是有关资料:

  • 我创建了一个新的“ HelloWorld”项目 根据 VS 代码指令,其中包括:
    • 我用 npm init查了一个新包 json
    • 我运行 npm i --save-dev typescript是因为我想要本地安装,而不是全局安装。
    • 我创建了一个 launch.json 来定义一个 node.js 项目。
    • 我创建了 tasks.json 文件,并为 tsc 设置了规定的设置。
  • 我已经做了一个 setings.json 文件,如图所示。它没有帮助。
  • 我已经安装了 VisualStudio2015Community,但是我还没有安装任何类型的类型脚本扩展。当我在开发人员命令提示符下键入“ where tsc”时,它会回复“ could not find”。我猜这是件好事。

我已经重新启动了 VSCode (好几次)。我错过了什么? 还需要做什么?

更新

我尝试了@zlumer 提供的解决方案。它成功地使类型脚本编译器运行,但随后它导致数千个错误出现。为了解决这个问题,我还必须调整 tsconfig.json 以排除 node _ module 文件夹:

"exclude": [
"node_modules"
]
204242 次浏览

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.

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

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.

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

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

npm install -g typescript

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.

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.

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

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.

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

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.

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).

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.

  • 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