node.js 从 Windows 命令行运行 tsc
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36753867/
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
Running tsc from the Windows command line
提问by Neoheurist
npmis installed and is actively being used from IntelliJ IDEA 15
从 IntelliJ IDEA 15 安装npm并积极使用
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...
我的目标是在 IntelliJ 中为我的 TypeScript 源代码生成类型,但我想学习使用 Windows 命令行,因此我可以明确指定命令行选项以 tinker 了解每个选项的作用。我对通过谷歌搜索发现的与设置和使用它相关的各种花絮感到困惑......我确信我错过了一些非常基本的东西,那些写博客或回答问题的人认为这是常识.. .
Here's what I've attempted and what I'm seeing...
这是我尝试过的和我所看到的......
Step 1: install typescript:
第 1 步:安装打字稿:
npm install -g typescript
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
Step 2: naively try to run tscdirectly from the Windows command line:
第 2 步:天真地尝试tsc直接从 Windows 命令行运行:
The examples that I've found by Googling take the form:
我通过谷歌搜索找到的示例采用以下形式:
Compile a single file:
编译单个文件:
tsc app.ts
tsc app.ts
above example is from http://www.primordialcode.com/blog/post/typescript-command-line-compiler
上面的例子来自http://www.primordialcode.com/blog/post/typescript-command-line-compiler
This does not work as shown because:
这不起作用,因为:
The install directory of
tscis not on the WindowsPathC:\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 thetscfile when entering the command to execute.More significantly the
tscfile is not a Windows executable... the#!Unix script (shebang) being a dead giveaway.
的安装目录
tsc不在 Windows 上,显然这可以通过更改 Window PATH 环境变量和/或在输入要执行的命令时完全限定文件路径来轻松补救或解决。PathC:\Users\{my user id}\AppData\Roaming\npm\node_modules\typescript\bintsc更重要的
tsc是,该文件不是 Windows 可执行文件......#!Unix 脚本(shebang)是一个致命的赠品。
Inspecting the tscfile:
检查tsc文件:
#!/usr/bin/env node
require('../lib/tsc.js')
Step 3: try to run tscfrom the node command prompt:
第 3 步:尝试tsc从节点命令提示符运行:
C:\>node
C:\>node
>tsc
>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
OK... let's specify the full path to the tscscript:
好的...让我们指定tsc脚本的完整路径:
C:\>node
C:\>node
>C:\Users\{my user id}\AppData\Roaming\npm\node_modules\typescript\bin\tsc
>C:\Users\{my user id}\AppData\Roaming\npm\node_modules\typescript\bin\tsc
...
literally the only output is ...when specifying the full path to the tscscript... I guess that it wants parameters... but hitting the tabkey reveals a list of what seem to be nodecommands (not tsccommands)... so I've no idea what's going on here...
从字面上看,唯一的输出是...指定tsc脚本的完整路径时......我猜它需要参数......但是tab按下键会显示一个似乎是节点命令(不是tsc命令)的列表......所以我已经不知道这里发生了什么......
Now I'm stuck
现在我被困住了
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)?
我需要安装/配置/使用什么环境来调用tsc(如图所示:http: //www.primordialcode.com/blog/post/typescript-command-line-compiler)?
and/or
和/或
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?
是否有教程或网站可以帮助我从干净的 Windows 系统转到能够从命令行使用 TypeScript 编译器为我的 TypeScript 源文件生成类型?
回答by Mattias Buelens
You should not add TypeScript's binfolder directly to the Windows PATH. As you noticed, the files in that binfolder are not directly executable from the command line.
您不应将 TypeScript 的bin文件夹直接添加到 Windows PATH. 正如您所注意到的,该文件bin夹中的文件不能直接从命令行执行。
Instead, npmcreates a .cmdscript for every configured executable in a globally installed package and puts it in:
相反,为全局安装的包中的每个配置的可执行文件npm创建一个.cmd脚本并将其放入:
%APPDATA%\npm
Try updating your PATHto include this folder, re-open your command line and try running tscagain.
尝试更新您的PATH以包含此文件夹,重新打开命令行并tsc再次尝试运行。
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 的 Node.js 安装程序默认将 Node 和 NPM 添加到您的 Windows 路径。如果您已经正常安装了 Node.js,这应该可以正常工作。您如何设置 Node 有什么特别之处吗?
回答by deniz gül
I was getting the same error. I added the full path to system variables. Full path for my case is C:\Users\User\node_modules\typescript\bin
我遇到了同样的错误。我添加了系统变量的完整路径。我的情况的完整路径是 C:\Users\User\node_modules\typescript\bin
Now tsc transpiles my typescript files.
现在 tsc 转换我的打字稿文件。
回答by Razzle
I'll share a couple of gotchas (that got me!) when I installed typescript (I followed https://www.typescriptlang.org/docs/handbook/typescript-in-5-minutes.html) on windows 10. I had earlier setup node by following the instructions at https://github.com/coreybutler/nvm-windowsafter failing miserably with other approaches.
当我在 Windows 10 上安装打字稿(我遵循https://www.typescriptlang.org/docs/handbook/typescript-in-5-minutes.html)时,我将分享一些问题(这让我明白了!)。我在使用其他方法惨遭失败后,按照https://github.com/coreybutler/nvm-windows 上的说明进行了更早的设置节点。
Gotcha #1- Both tsc and tsc.cmd areon the path (at %NVM_SYMLINK%) buttsc is the bash script. In other words you have to use the command tsc.cmdin order to actually invoke the windows version! Just invoking tsc will (in my Powershell terminal) cause it to fall over with weird errors.
陷阱#1- tsc 和 tsc.cmd都在路径上(在 %NVM_SYMLINK%),但tsc 是 bash 脚本。换句话说,您必须使用命令tsc.cmd才能实际调用 Windows 版本!仅调用 tsc 将(在我的 Powershell 终端中)导致它因奇怪的错误而失败。
Gotcha #2- Windows file system locking semantics strikes again! Because I had been digging into the problem, I had the tsc.cmd file open in an editor - which was locking the file! This causes the correct invocation (tsc.cmd) to alsofail.. till I closed the editor.
陷阱 #2- Windows 文件系统锁定语义再次出现!因为我一直在深入研究这个问题,所以我在编辑器中打开了 tsc.cmd 文件 - 它正在锁定文件!这会导致正确的调用 (tsc.cmd)也失败......直到我关闭编辑器。
Hope this helps someone..
希望这可以帮助某人..
回答by JiPaix
Generic solution :
通用解决方案:
Analyze
分析
typescript\bin\tscfile content is a unix script but it runs under node
typescript\bin\tsc文件内容是一个 unix 脚本,但它在 node 下运行
#!/usr/bin/env node //<<< this loads node env
require('../lib/tsc.js') //<<< this is plain javascript
This file loads node, and execute file : typescript\lib\tsc.js
该文件加载节点,并执行文件: typescript\lib\tsc.js
Solution:
解决方案:
We want to reproduce the same behavior.
To run .js files with node on Windows :
我们想要重现相同的行为。
要在 Windows 上使用 node 运行 .js 文件:
- open CMD or PowerShell
- type:
node /path/to/typescript/lib/tsc.js -options -as-if-i-was-on-linux - press enter
- 打开 CMD 或 PowerShell
- 类型:
node /path/to/typescript/lib/tsc.js -options -as-if-i-was-on-linux - 按回车

