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:
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.
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?
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.
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.
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?
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..
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With