Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running tsc from the Windows command line

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

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...

Step 1: install 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 tsc directly from the Windows command line:

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

Compile a single file:

tsc app.ts

above example is from http://www.primordialcode.com/blog/post/typescript-command-line-compiler

This does not work as shown because:

  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.

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

Inspecting the tsc file:

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

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

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

C:\>node

> 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 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...

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)?

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?

like image 767
Neoheurist Avatar asked Apr 20 '16 19:04

Neoheurist


People also ask

How do I run a TypeScript file in CMD?

We can use the ts-node package to execute TypeScript files from the command line. Install it with npm or other package manager. After that, simply execute the TypeScript files with the command: ts-node filename.

What is the tsc command?

The tsc command envokes the TypeScript compiler. When no command-line options are present, this command looks for the tsconfig. json file. If no tsconfig. json is found, it returns by dumping the usage of tsc command.


2 Answers

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.

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

%APPDATA%\npm

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

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?

like image 121
Mattias Buelens Avatar answered Oct 03 '22 10:10

Mattias Buelens


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-windows after failing miserably with other approaches.

Gotcha #1 - Both tsc and tsc.cmd are on the path (at %NVM_SYMLINK%) but tsc is the bash script. In other words you have to use the command tsc.cmd in order to actually invoke the windows version! Just invoking tsc will (in my Powershell terminal) cause it to fall over with weird errors.

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 also fail.. till I closed the editor.

Hope this helps someone..

like image 38
Razzle Avatar answered Oct 03 '22 09:10

Razzle