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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41624686/
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-10-21 04:09:48  来源:igfitidea点击:

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

node.jstypescripttsc

提问by codingyo

I am a beginner with type script. I am unable to compile the type script file.

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

As I hit Ctrl+Shift+B in the VS Code, i get the error "tsc is not recognised"

当我在 VS Code 中按 Ctrl+Shift+B 时,我收到错误“tsc 无法识别”

I used npm while downloading transcript.

我在下载成绩单时使用了 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
`-- [email protected]

Here is my tasks.json file

这是我的 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"
}

Can anyone help?

任何人都可以帮忙吗?

采纳答案by Uzair Sajid

Have you tried running tsc --initin your directory? This will create a tsconfig.jsonfile in your directory where you can define your configuration. VSCode will also start using this file once it finds it in your root directory.

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

Check out tsconfig.jsondocumentation for more details.

查看tsconfig.json文档以获取更多详细信息。

回答by Prayag Sharma

I had a similar issue and didn't even showed the version of Typescript after typing tsc -v. Even i had to scratch my head for few days and couldn't solve it.

我遇到了类似的问题,甚至在键入 tsc -v 后都没有显示 Typescript 的版本。即使我不得不挠头几天,也无法解决。

At last i did 4 things.

最后我做了4件事。

  1. installed latest version of nodejs which is i.e. 8.1.3
  2. installed npm which is 5.1.0 ( npm install npm@latest -g)
  3. installed typescript 2.4.1 ( npm install -g typescript)
  4. as suggested by @Uzair Sajid above. (tsc --init)
  1. 安装了最新版本的 nodejs,即 8.1.3
  2. 安装的 npm 是 5.1.0 ( npm install npm@latest -g )
  3. 已安装的 typescript 2.4.1 ( npm install -g typescript )
  4. 正如上面@Uzair Sajid 所建议的那样。(tsc --init)

Now, I can see the TS version, and also compile the TS code.

现在,我可以看到 TS 版本,也可以编译 TS 代码。

回答by Shardul Birje

I'm using Windows OS and For some reason the marked answer didn't work for me.I tried tsc--initand i was returned the same error.I updated the path variable to C:\Users\"UserName"\AppData\Roaming\npmand this worked..

我正在使用 Windows 操作系统,出于某种原因,标记的答案对我不起作用。我尝试过tsc--init,但返回了相同的错误。我 将路径变量更新为C:\Users\"UserName"\AppData\Roaming\npm,这有效..

Note:replace Username with your own Username

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

回答by codingyo

I tried using the command line to set the target.

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

tsc first.ts --target ES5

This created the first.js( compiled type script in javascript format).I manually moved this file to scripts folder.

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

Now when i ran the server using lite-servercommand. The browser could find the first.js file and ran the typescript code.

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