node.js tsc 不被识别为内部或外部命令

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

tsc is not recognized as internal or external command

node.jstypescriptvisual-studio-code

提问by Brent Arias

I updated from VSCode 0.10.6 to 0.10.8, and tried using Typescript for the first time. Unfortunately I when I tell VSCode to build, I get the error:

我从 VSCode 0.10.6 更新到 0.10.8,并且第一次尝试使用 Typescript。不幸的是,当我告诉 VSCode 构建时,出现错误:

tsc is not a recognized as an internal or external command...

tsc 不被识别为内部或外部命令...

Here are the relevant details:

以下是相关详情:

  • I created a fresh "HelloWorld" project according to VS Code instructions. This included:
    • I ran npm initfor a new package.json
    • I ran npm i --save-dev typescriptbecause I want a local install, rather than a global install.
    • I created a launch.json to define a node.js project.
    • I created the tasks.json file, with prescribed settings for tsc.
  • I have made a settings.json file, as shown here. It did not help.
  • I do have Visual Studio 2015 Community installed, but I have not installed a Typescript extension of any kind. When I type "where tsc" at a developer command prompt, it replies "could not find". I assume this is a good thing.
  • 根据 VS Code 说明创建了一个新的“HelloWorld”项目。这包括:
    • 我跑npm init了一个新的 package.json
    • 我跑npm i --save-dev typescript是因为我想要本地安装,而不是全局安装。
    • 我创建了一个 launch.json 来定义一个 node.js 项目。
    • 我创建了 tasks.json 文件,并为 tsc 指定了设置。
  • 我制作了一个 settings.json 文件,如下所示。它没有帮助。
  • 我确实安装了 Visual Studio 2015 社区,但我没有安装任何类型的 Typescript 扩展。当我在开发人员命令提示符下键入“where tsc”时,它会回复“找不到”。我认为这是一件好事。

I have restarted VSCode (several times). What am I missing? What more must be done?

我已经重新启动了 VSCode(几次)。我错过了什么?还必须做什么?

Update

更新

I tried the solution offered by @zlumer. It succeeded in making the typescript compiler run, but then it caused thousands of errors to appear. To fix that, I also had to adjust my tsconfig.json to exclude the node_modules folder:

我尝试了@zlumer 提供的解决方案。它成功地使打字稿编译器运行,但随后导致出现数千个错误。为了解决这个问题,我还必须调整我的 tsconfig.json 以排除 node_modules 文件夹:

"exclude": [
    "node_modules"
]

回答by WasiF

There might be a reason that Typescriptis not installed globally, so install it

可能是因为Typescript没有全局安装的原因,所以安装它

npm install -g typescript // installs typescript globally

If you want to convert .tsfiles into .js, do this as per your need

如果要将.ts文件转换为.js,请根据需要执行此操作

tsc file.ts // file.ts will be converted to file.js file
tsc         // all .ts files will be converted to .js files in the directory
tsc --watch // converts all .ts files to .js, and watch changes in .ts files

回答by zlumer

The problem is that tscis not in your PATHif installed locally.

问题是如果在本地安装,tsc则不在您的范围内PATH

You should modify your .vscode/tasks.jsonto include full path to tsc.

您应该修改您的.vscode/tasks.json以包含tsc.

The line to change is probably equal to "command": "tsc".

要更改的行可能等于"command": "tsc"

You should change it to "command": "node"and add the following to your args: "args": ["${workspaceRoot}\\node_modules\\typescript\\bin\\tsc"](on Windows).

您应该将其更改为"command": "node"并将以下内容添加到您的参数中:("args": ["${workspaceRoot}\\node_modules\\typescript\\bin\\tsc"]在 Windows 上)。

This will instruct VSCode to:

这将指示 VSCode:

  1. Run NodeJS (it should be installed globally).
  2. Pass your local Typescript installation as the script to run.
  1. 运行 NodeJS(它应该是全局安装的)。
  2. 将本地 Typescript 安装作为要运行的脚本传递。

(that's pretty much what tscexecutable does)

(这几乎就是tsc可执行文件所做的)

Are you sure you don't want to install Typescript globally? It should make things easier, especially if you're just starting to use it.

您确定不想全局安装 Typescript 吗?它应该让事情变得更容易,特别是如果你刚刚开始使用它。

回答by Drop Bear Dan

In the VSCode file tasks.json, the "command": "tsc"will try to find the tsc windows command script in some folder that it deems to be your modules folder.

在 VSCode 文件中tasks.json"command": "tsc"将尝试在它认为是您的模块文件夹的某个文件夹中找到 tsc windows 命令脚本。

If you know where the command npm install -g typescriptor npm install typescriptis saving to, I would recommend replacing:

如果您知道命令npm install -g typescriptnpm install typescript保存到的位置,我建议替换:

"command": "tsc"

with

"command": "D:\Projects\TS\Tutorial\node_modules\.bin\tsc"

where D:\\...\\binis the folder that contains my tsc windows executable

D:\\...\\bin包含我的 tsc windows 可执行文件的文件夹在哪里

Will determine where my vscode is natively pointing to right now to find the tsc and fix it I guess.

将确定我的 vscode 现在本机指向的位置以找到 tsc 并修复它,我猜。

回答by Ashutosh Jha

For windows

窗户用

After installing typescript globally

全局安装打字稿后

npm install typescript -g 

just search for "node.js command prompt"

只需搜索“node.js 命令提示符”

type in command promt

输入命令提示符

tsc -v 

Here we can see tsc command works, now navigate to your folder and type

在这里我们可以看到 tsc 命令有效,现在导航到您的文件夹并键入

tsc filename.ts 

its complies ts to js file.

它符合 ts 到 js 文件。

回答by Virendra khade

You have missed typescript installation, just run below command and then try tsc --init

您错过了打字稿安装,只需运行以下命令,然后尝试tsc --init

npm install -g typescript

回答by Srinivasan.S

Me too faced the same problem. Use nodeJS command prompt instead of windows command prompt.

我也面临同样的问题。使用 nodeJS 命令提示符而不是 windows 命令提示符。

Step 1: Execute the npm install -g typescript

第 1 步:执行 npm install -g typescript

Step 2: tsc filename.ts

第2步: tsc filename.ts

New file will be create same name and different extension as ".js"

新文件将创建与“ .js”相同的名称和不同的扩展名

Step 3: node filename.js

第 3 步: node filename.js

You can see output in screen. It works for me.

您可以在屏幕中看到输出。这个对我有用。

回答by hoanvd1210

For me, by running Visual Studio Code as Administrator, the problem is resolved.

对我来说,通过以管理员身份运行 Visual Studio Code,问题解决了。

回答by Dirk B?umer

Alternatively you can use npm which automatically looks into the .bin folder. Then you can use tsc

或者,您可以使用 npm 自动查看 .bin 文件夹。然后你可以使用 tsc