从 Windows 命令行运行 tsc [英] Running tsc from the Windows command line

查看:82
本文介绍了从 Windows 命令行运行 tsc的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

npm 已安装并在 IntelliJ IDEA 15 中被积极使用

npm is installed and is actively being used from IntelliJ IDEA 15

我的目标是在 IntelliJ 中为我的 TypeScript 源代码生成类型,但我想学习使用 Windows 命令行,因此我可以明确指定命令行选项以 tinker 了解每个选项的作用.我对通过谷歌搜索发现的与设置和使用它相关的各种花絮感到困惑......我确信我错过了一些非常基本的东西,那些写博客或回答问题的人认为这是常识...

My goal is to generate typings for my TypeScript source code in IntelliJ, but I want to learn using the Windows command line, so I can explicitly specify the command line options to tinker to understand what each option does. I am confused by the various tidbits related to setting this up and using it that I've found by Googling... I'm sure that I'm missing something very basic that those who blog or answer questions assume as common knowledge...

这是我尝试过的和我看到的......

Here's what I've attempted and what I'm seeing...

第 1 步:安装打字稿:

npm install -g typescript

这导致我的系统上安装了以下文件/目录结构:

This results in the following file/directory structure being installed on my system:

C:\Users\{my user id}\AppData\Roaming\npm\node_modules\typescript
|---bin
|   |--- tsc
|   |--- tscserver
|---lib
|   |--- lib.core.d.ts
|   |--- ...
|   |--- typescriptServices.js
|--- .npmignore
|--- ...
|--- ThirdPartyNoticeText.txt

第 2 步:天真地尝试直接从 Windows 命令行运行 tsc:

Step 2: naively try to run tsc directly from the Windows command line:

我通过谷歌搜索找到的示例采用以下形式:

The examples that I've found by Googling take the form:

编译单个文件:

tsc app.ts

以上示例来自 http://www.primordialcode.com/博客/帖子/打字稿命令行编译器

这不起作用,因为:

  1. tsc 的安装目录不在 Windows Path C:\Users\{my user id}\AppData\Roaming\npm\node_modules\typescript\bin,显然这可以通过更改 Window PATH 环境变量和/或完全限定 tsc 的路径来轻松解决或解决输入要执行的命令时的文件.

  1. The install directory of tsc is not on the Windows Path C:\Users\{my user id}\AppData\Roaming\npm\node_modules\typescript\bin, obviously this is easily remedied or worked around by changing the Window PATH environmental variable and/or fully qualifying the path to the tsc file when entering the command to execute.

更重要的是,tsc 文件不是 Windows 可执行文件……#! Unix 脚本(shebang) 是一个死的赠品.

More significantly the tsc file is not a Windows executable... the #! Unix script (shebang) being a dead giveaway.

检查 tsc 文件:

#!/usr/bin/env node
require('../lib/tsc.js')

第 3 步:尝试从节点命令提示符运行 tsc:

Step 3: try to run tsc from the node command prompt:

C:\>node

> tsc

ReferenceError: tsc is not defined
at repl:1:1
at REPLServer.defaultEval (repl.js:252:27)
at bound (domain.js:287:14)
at REPLServer.runBound [as eval] (domain.js:300:12)
at REPLServer.<anonymous> (repl.js:417:12)
at emitOne (events.js:82:20)
at REPLServer.emit (events.js:169:7)
at REPLServer.Interface._onLine (readline.js:210:10)
at REPLServer.Interface._line (readline.js:549:8)
at REPLServer.Interface._ttyWrite (readline.js:826:14)

^C

好的...让我们指定 tsc 脚本的完整路径:

OK... let's specify the full path to the tsc script:

C:\>node

> C:\Users\{我的用户 ID}\AppData\Roaming\npm\node_modules\typescript\bin\tsc

...

从字面上看,当指定 tsc 脚本的完整路径时,唯一的输出是 ......我猜它需要参数...但是点击 tab 键会显示一个似乎是 node 命令的列表(不是 tsc 命令)......所以我'我不知道这里发生了什么......

literally the only output is ... when specifying the full path to the tsc script... I guess that it wants parameters... but hitting the tab key reveals a list of what seem to be node commands (not tsc commands)... so I've no idea what's going on here...

现在我卡住了

我需要安装/配置/使用什么环境来调用tsc(如图所示:http://www.primordialcode.com/blog/post/typescript-command-line-compiler)?

What environment do I need to install/configure/use to invoke tsc (as illustrated by: http://www.primordialcode.com/blog/post/typescript-command-line-compiler)?

和/或

是否有教程或网站可以帮助我从干净的 Windows 系统转变为能够从命令行使用 TypeScript 编译器为我的 TypeScript 源文件生成类型?

Is there a tutorial or site that would help me go from a clean Windows system to being able to use the TypeScript compiler from the command line to generate typings for my TypeScript source files?

推荐答案

您不应将 TypeScript 的 bin 文件夹直接添加到 Windows PATH.如您所见,bin 文件夹中的文件不能直接从命令行执行.

You should not add TypeScript's bin folder directly to the Windows PATH. As you noticed, the files in that bin folder are not directly executable from the command line.

相反,npm 为全局安装包中的每个配置的可执行文件创建一个 .cmd 脚本并将其放入:

Instead, npm creates a .cmd script for every configured executable in a globally installed package and puts it in:

%APPDATA%\npm

尝试更新您的 PATH 以包含此文件夹,重新打开命令行并再次尝试运行 tsc.

Try updating your PATH to include this folder, re-open your command line and try running tsc again.

旁注:Windows 的 Node.js 安装程序默认将 Node 和 NPM 添加到您的 Windows 路径.如果您已经正常安装了 Node.js,这应该可以正常工作.您如何设置 Node 有什么特别之处吗?

Side note: the Node.js installer for Windows by default adds Node and NPM to your Windows path. If you have installed Node.js normally, this should have worked just fine. Anything special about how you have your Node set up?

这篇关于从 Windows 命令行运行 tsc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆