Javascript 找不到 Typescript 编译器:命令“tsc”无效

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12685995/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 11:01:02  来源:igfitidea点击:

Can't find Typescript compiler: Command "tsc" is not valid

javascriptvisual-studio-2012typescripttsc

提问by 0x49D1

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

刚刚将 Typescript 扩展安装到 VS2012 并遵循Install TypeScript for Visual Studio 2012和调用编译器的教程

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

但是当我尝试编译 .ts 文件时,我应该在哪里输入:tsc greeter.ts?在 VS 命令行和 Windows 控制台中尝试过,总是得到 tsc 未被识别为 command( Command "tsc" is not valid.) 的消息。

采纳答案by chuckj

Ensure you have,

确保你有,

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

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

or,

或者,

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

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

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

在你的路上。如果没有,请尝试重新启动CMD.EXE并查看是否显示新副本。如果失败,请尝试将上述方法之一手动添加到您的路径中。

回答by lhk

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

如果您使用 tsc 作为节点模块,请确保您已安装它

npm install -g typescript

Then it should be available globally in your node command prompt

然后它应该在您的节点命令提示符中全局可用

回答by Neurotransmitter

For folks on Windowswith 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.

对于Windows 上使用Visual Studio Code 的人,他们不想安装完整的 Visual Studio 只是为了tsc.exe,我可以建议从这里简单地下载它: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.

遗憾的是,TypeScript 下载页面中缺少此链接。

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

尽管安装程序名为TypeScript for Visual Studio 2015,但它也适用于 Visual Studio Code。

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.

下载并安装 TypeScript 后,您应该手动将其安装目录(C:\Program Files (x86)\Microsoft SDKs\TypeScript\2.0\在我的情况下)添加到%PATH%环境变量中

Open environment variables editor:

打开环境变量编辑器:

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

Win+ S; e, n, v, i, r, o; 单击“为您的帐户编辑环境变量”

In the window opened find Pathuser 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 ;, appendthe TypeScript install location and add one more ;in the end.

在打开的窗口中找到Path用户变量(注意%PATH%这里没有命名,但仍然是这个)。您可能已经在那里设置了一些路径。您只需要键入;附加TypeScript 安装位置并;在最后再添加一个。

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 Codeafter making the change to the %PATH%.

完成后,打开命令提示符并输入tsc -v. 如果tsc.exe正在显示 的版本,则您已完成。对于这项工作,重新启动命令提示符,VS代码进行更改到后%PATH%

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

PS 如果您收到“错误 TS5057:在指定目录中找不到 tsconfig.json 文件:'.'”,只需tsconfig.json在文档根目录(这可能是您的.ts文件所在的位置)中创建文件,内容简单:{}. 这意味着“一个空的 JSON 文件 <...>。这对大多数人来说已经足够了。” (来源)。

回答by Laszlo

Usually closing and reopen the command prompt solves the issues.

通常关闭并重新打开命令提示符可以解决问题。

回答by Vinamis

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

作为新的路径变量,不要使用显示的tsc version数字,而是使用不同的 TypeScript 文件夹名称。

Sample :

样本 :

tsc -v display 2.4.1 

but TypeScript directory is

但 TypeScript 目录是

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

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

回答by Chris Halcrow

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

检查这两个文件夹以找出您拥有的 TypeScript 版本:

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.

将其粘贴到您的系统路径中(快速访问 - Windows 键,然后键入“env”)。然后以管理员身份打开一个新的命令提示符控制台,“tsc”应该可以工作。

回答by Shahriar Morshed

In CMD type where tscif 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 .

在 CMD 类型中,where tsc如果它没有显示这样的路径,C:\Program Files (x86)\Microsoft SDKs\TypeScript\typescript version you installed\则表示您的 tsc 未从此处运行。您需要按照以下@TranslucentCloud答案手动更改环境路径。