我最近在 VS Code 中从事一个小项目,主要是打字稿。
我将其提交到 GitHub 存储库,并将其下载到我经常用于 TypeScript 开发的机器上。 但是,当我尝试构建 VS Code 项目时,出现错误:
tsc : The term 'tsc' is not recognized as the name of a cmdlet, function,
script file, or operable program. Check the spelling of the name, or if a
path was included, verify
that the path is correct and try again.
At line:1 char:1
+ tsc
+ ~~~
+ CategoryInfo : ObjectNotFound: (tsc:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
在阅读了有关该主题的其他 StackOverflow 问题后,我尝试了一些方法:
npm install -g typescript
安装了 typescript - 我没有收到任何错误消息,因此看来安装成功(在步骤 3 中确认)。tsc
编译器的任何异常位置。 我没有找到。npm list -g
以确保 TypeScript 已安装(确实如此)C:\Users\MyUserName\AppData\Roaming\npm\tsc
- 运行的路径tsc -v
- 命令未被操作系统识别。setx path "%paths%:C:\MyUserName\AppData\Roaming\npm"
尝试将 npm 全局目录添加到系统环境 paths
变量中。 运行命令后,关闭命令行,打开一个新命令行,然后尝试不合格的 tsc
命令。 没有运气。几乎就好像
npm install -g typescript
命令不足以实际上安装TypeScript,以便系统在全局基础上识别它(尽管使用了-g/“global”标志)。 显然我错过了一些东西。 我错过了什么?
将 node.js 更新到 10.13.0 LTS 后,关闭并重新打开命令提示符和 VS Code 后,
tsc
运行。
所以,如果您开始从 tsc 收到有趣的反馈,请务必更新节点!
尝试运行 npx tsc -v; 它应该打印打字稿版本。 一旦你明白了,如果你想运行一个 .ts 文件,请使用命令 npx tsc filename.ts 运行。 应该可以。