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 init
for a new package.json - 我为新的package.json运行了npm init
- I ran
npm i --save-dev typescript
because I want a local install, rather than a global install. - 我运行npm i --save-dev typescript因为我想要本地安装,而不是全局安装。
- I created a launch.json to define a node.js project.
- 我创建了一个launch.json来定义一个node.js项目。
- I created the tasks.json file, with prescribed settings for tsc.
- 我创建了tasks.json文件,并为tsc指定了设置。
- I ran
- 我根据VS Code指令创建了一个全新的“HelloWorld”项目。这包括:我运行npm init以获取新的package.json我运行npm i --save-dev typescript因为我想要本地安装,而不是全局安装。我创建了一个launch.json来定义一个node.js项目。我创建了tasks.json文件,并为tsc指定了设置。
- I have made a settings.json file, as shown here. It did not help.
- 我已经制作了一个settings.json文件,如下所示。它没有帮助。
- 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.
- 我安装了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"
]
6 个解决方案
#1
22
The problem is that tsc
is not in your PATH
if installed locally.
问题是如果在本地安装,则tsc不在您的PATH中。
You should modify your .vscode/tasks.json
to include full path to tsc
.
您应该修改.vscode / tasks.json以包含tsc的完整路径。
The line to change is probably equal to "command": "tsc"
.
要改变的行可能等于“命令”:“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:“args”:[“$ {workspaceRoot} \\ node_modules \\ typescript \\ bin \\ tsc”](在Windows上)。
This will instruct VSCode to:
这将指示VSCode:
- Run NodeJS (it should be installed globally).
- 运行NodeJS(它应该全局安装)。
- Pass your local Typescript installation as the script to run.
- 将本地Typescript安装作为要运行的脚本传递。
(that's pretty much what tsc
executable 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吗?它应该让事情变得更容易,特别是如果你刚刚开始使用它。
#2
11
npm install -g typescript // installs typescript globally
then
然后
tsc file.ts // file.ts will be converted to file.js file
tsc // all .ts files will be converted to .js files
tsc --watch // converts the .ts files to .js on file every changes saved
#3
7
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”将尝试在它认为是您的modules文件夹的某个文件夹中找到tsc windows命令脚本。
If you know where the command npm install -g typescript
or npm install typescript
is saving to, I would recommend replacing:
如果您知道命令npm install -g typescript或npm install typescript保存到哪里,我建议替换:
"command": "tsc"
with
同
"command": "D:\\Projects\\TS\\Tutorial\\node_modules\\.bin\\tsc"
where D:\\...\\bin
is 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并修复它我想。
#4
1
Alternatively you can use npm which automatically looks into the .bin folder. Then you can use tsc
或者,您可以使用npm自动查看.bin文件夹。然后你可以使用tsc
#5
0
You have missed typescript installation, just run below command and then try tsc --init
你错过了打字稿安装,只需在命令下运行,然后尝试tsc --init
npm install -g typescript
npm install -g typescript
#6
0
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步:节点filename.js
You can see output in screen. It works for me.
您可以在屏幕上看到输出。这个对我有用。
#1
22
The problem is that tsc
is not in your PATH
if installed locally.
问题是如果在本地安装,则tsc不在您的PATH中。
You should modify your .vscode/tasks.json
to include full path to tsc
.
您应该修改.vscode / tasks.json以包含tsc的完整路径。
The line to change is probably equal to "command": "tsc"
.
要改变的行可能等于“命令”:“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:“args”:[“$ {workspaceRoot} \\ node_modules \\ typescript \\ bin \\ tsc”](在Windows上)。
This will instruct VSCode to:
这将指示VSCode:
- Run NodeJS (it should be installed globally).
- 运行NodeJS(它应该全局安装)。
- Pass your local Typescript installation as the script to run.
- 将本地Typescript安装作为要运行的脚本传递。
(that's pretty much what tsc
executable 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吗?它应该让事情变得更容易,特别是如果你刚刚开始使用它。
#2
11
npm install -g typescript // installs typescript globally
then
然后
tsc file.ts // file.ts will be converted to file.js file
tsc // all .ts files will be converted to .js files
tsc --watch // converts the .ts files to .js on file every changes saved
#3
7
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”将尝试在它认为是您的modules文件夹的某个文件夹中找到tsc windows命令脚本。
If you know where the command npm install -g typescript
or npm install typescript
is saving to, I would recommend replacing:
如果您知道命令npm install -g typescript或npm install typescript保存到哪里,我建议替换:
"command": "tsc"
with
同
"command": "D:\\Projects\\TS\\Tutorial\\node_modules\\.bin\\tsc"
where D:\\...\\bin
is 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并修复它我想。
#4
1
Alternatively you can use npm which automatically looks into the .bin folder. Then you can use tsc
或者,您可以使用npm自动查看.bin文件夹。然后你可以使用tsc
#5
0
You have missed typescript installation, just run below command and then try tsc --init
你错过了打字稿安装,只需在命令下运行,然后尝试tsc --init
npm install -g typescript
npm install -g typescript
#6
0
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步:节点filename.js
You can see output in screen. It works for me.
您可以在屏幕上看到输出。这个对我有用。