简体   繁体   English

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

[英]Can't find Typescript compiler: Command “tsc” is not valid

Just installed Typescript extension to VS2012 and followed Install TypeScript for Visual Studio 2012 and then the tutorial to 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 ?但是当我尝试编译 .ts 文件时,我应该在哪里输入: 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. ).在 VS 命令行和 Windows 控制台中尝试过,总是得到 tsc 未被识别为命令的消息( Command "tsc" is not valid. )。

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然后它应该在您的节点命令提示符中全局可用

Ensure you have,确保你有,

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

or,要么,

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

on your path.在你的路上。 If not, try restarting CMD.EXE and see if shows up with a fresh copy.如果没有,请尝试重新启动CMD.EXE并查看是否显示新副本。 If that fails, try adding one of the above manually to your path.如果失败,请尝试手动将上述之一添加到您的路径中。

For folks on Windows with 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 的人,他们不想只为tsc.exe安装完整的 Visual Studio,我可以建议从这里简单地下载它: 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 ; WIN + S; e , n , v , i , r , o ; e , n , v , i , r , o ; click Edit environment variables for your account .单击“为您的帐户编辑环境变量”

In the window opened find Path user variable (note that it is not named %PATH% here, but still this is it).在打开的窗口中找到Path用户变量(注意这里没有命名为%PATH% ,但仍然是这个)。 You probably have some path set there already.您可能已经在那里设置了一些路径。 You just have to type ;你只需要输入; , append the TypeScript install location and add one more ; ,追加TypeScript 安装位置并再添加一个; in the end.到底。

Screenshot for your reference:截图供您参考:

在此处输入图片说明

After this is done, open Command Prompt and type in tsc -v .完成后,打开命令提示符并输入tsc -v If tsc.exe 's version is getting displayed, you're done.如果tsc.exe的版本被显示出来,你就大功告成了。 For this to work, restart Command Prompt and VS Code after making the change to the %PATH% .为此,请在更改%PATH%重新启动命令提示符和 VS Code

PS If you get "error TS5057: Cannot find a tsconfig.json file at the specified directory: '.'", just create tsconfig.json file in the document root (that's probably where your .ts files are) with simple contents: {} . PS 如果您收到“错误 TS5057:在指定目录中找不到 tsconfig.json 文件:'.'”,只需在文档根目录(这可能是您的.ts文件所在的位置)中创建tsconfig.json文件,内容简单: {} . This means "an empty JSON file <...>. This will be sufficient for most people."这意味着“一个空的 JSON 文件 <...>。这对大多数人来说已经足够了。” ( source ). 来源)。

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

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').将其粘贴到您的系统路径中(快速访问 - Windows 键,然后键入“env”)。 Then open a new command prompt console as administrator and 'tsc' should work.然后以管理员身份打开一个新的命令提示符控制台,“tsc”应该可以工作。

As new path variable, don't use the displayed tsc version number 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

在 CMD 类型where tsc如果它没有显示这样的路径C:\\Program Files (x86)\\Microsoft SDKs\\TypeScript\\typescript version you installed\\这意味着你的 tsc 没有从这里运行。你需要改变环境按照@TranslucentCloud的回答手动设置路径。

只需删除 AppData\\Roaming\\npm\\tsc.ps1 文件即可解决问题。

In my case, I did install typescript globally by using -g flag but tsc was not recognized.就我而言,我确实通过使用-g标志全局安装了打字稿,但无法识别tsc It turns out that below directory was not included in path environment variable:事实证明,以下目录未包含在path环境变量中:

C:\Users\Jitendra\AppData\Roaming\npm

Adding this directory to path variable worked for me.将此目录添加到path变量对我有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 TypeScript 编译器 (TSC) 解决 Node.js ES6 (ESM) 模块。 TSC 不发出正确的文件扩展名 - How to resolve Node.js ES6 (ESM) Modules with the TypeScript Compiler (TSC). TSC doesn't emit the correct file-ext 使用tsc使用tsc命令将TypeScript转换为JavaScript的利弊 - Pros and Cons for using tsc to transpile TypeScript to JavaScript using tsc command 如何为打字稿编译器(tsc)设置最大旧空间大小? - How to set max-old-space-size for typescript compiler (tsc)? 打字稿命令“ tsc”未定义。 尝试了每个已知的解决方案 - Typescript command “tsc” undefined. Tried every known solution 通过 tsc 命令的打字稿:输出到单个文件而不连接 - TypeScript via tsc command: Output to single file without concatenation 如何使用tsc从typescript生成amd模块? - How can I use tsc to generate amd modules from typescript? TypeScript的tsc编译器在调用jQuery和Flot代码时发出错误 - TypeScript's tsc compiler emits errors when calling jQuery and Flot code Karma - TypeScript - 找不到变量:exports - Karma - TypeScript - Can't find variable: exports 在 TypeScript 中找不到名称 jsPdf - Can't find name jsPdf in TypeScript 打字稿找不到模块少 - typescript can't find module less
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM