typescript: tsc 不是内部或外部命令,也不是可运行的程序 或批处理文件

typescript: tsc is not recognized as an internal or external command, operable program or batch file

我是打字稿的初学者。我无法编译类型脚本文件。

当我在 VS Code 中按 Ctrl+Shift+B 时,出现错误 "tsc is not recognised"

我在下载成绩单时使用了 npm。

C:\Users\sramesh>npm install -g typescript
C:\Users\sramesh\AppData\Roaming\npm\tsserver -> C:\Users\sramesh\AppData\Roaming\npm\node_modules\typescript\bin\tsserver
C:\Users\sramesh\AppData\Roaming\npm\tsc -> C:\Users\sramesh\AppData\Roaming\npm\node_modules\typescript\bin\tsc
C:\Users\sramesh\AppData\Roaming\npm
`-- typescript@2.1.5

这是我的 tasks.json 文件

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "0.1.0",
    "command": "C:\Users\sramesh\AppData\Roaming\npm\node_modules\typescript\bin\tsc",
    "isShellCommand": true,
    "args": [],
    "showOutput": "silent",
    "problemMatcher": "$tsc"
}

有人可以帮忙吗?

我尝试使用命令行设置目标。

tsc first.ts --target ES5

这创建了 first.js(javascript 格式的编译类型脚本)。我手动将此文件移动到脚本文件夹。

现在当我 运行 服务器使用 lite-server 命令时。浏览器可以找到 first.js 文件和 运行 打字稿代码。

您是否在您的目录中尝试过 运行 tsc --init?这将在您的目录中创建一个 tsconfig.json 文件,您可以在其中定义配置。 VSCode 一旦在您的根目录中找到该文件,它也会开始使用它。

查看 tsconfig.json 文档了解更多详情。

我遇到了类似的问题,输入 tsc -v 后甚至没有显示 Typescript 的版本。连我都摸不着头脑几天也解决不了

最后我做了4件事

  1. 已安装最新版本的 nodejs,即 8.1.3
  2. 已安装 npm 5.1.0 ( npm install npm@latest -g)
  3. 已安装打字稿 2.4.1(npm install -g 打字稿)
  4. 如上@Uzair Sajid 所建议。 (tsc --init)

现在可以看到TS版本了,还能编译TS代码

我正在使用 Windows OS 并且出于某种原因,标记的答案对 me.I 无效 tsc--init 并且我返回了相同的 error.I 将路径变量更新为 C:\Users\"UserName"\AppData\Roaming\npm 并且这有效..

注意:将用户名替换为您自己的用户名

安装后,我尝试的简单解决方案是将环境变量中的路径变量设置为安装 npm 的文件夹。

C:\Users\"Username"\AppData\Roaming\npm

其中用户名将是您自己的用户名。

然后,打开 cmd,使用 cd AppData/Roaming/npm 转到 npm 文件夹 并输入 tsc ,它将 运行。 现在只要关闭cmd再打开,输入tsc --version OR tsc,就会运行成功。

我在打字稿上遇到了同样的问题,'tsc'终端无法识别。

在我用像 'yarn global add typescript' 这样的纱线安装打字稿之前, 然后我用 'npm install -g typescript' 这样的 npm 安装了打字稿,问题就解决了, 终端知道打字稿命令和它自己。

也许这个答案会有帮助

我通过以下方式在我的 Windows 10 21H1 桌面上解决了这个问题:

  1. 全局安装 typescript 包 → npm install -g typescript

  2. 正在更新 Path 环境变量:

    • 在开始菜单中搜索Environment Variables
    • Select Edit the system environment variables
    • Select Advanced 部分中的 Environment Variables... 按钮
    • System Variables 部分查找 Path 变量
    • 双击该变量
    • Select New...
    • 输入您的 npm 配置下载全局包的位置。您可以使用 npm list -g 命令找出位置。对我来说,它位于 C:\Users\MyUsername\AppData\Roaming\npm — 请务必将 MyUsername 替换为您的用户名
  3. Re/open 一个活跃终端

  4. 使用tsc -v命令观察结果