I had to create a quick console app in Typescript a few days back to test a few things. This post should help you (and me) setup a typescript console app within a minute.
For the sake of this example, I’m going to create a simple hello-world-ish console app that prints our public IP.
Setup the new application
1mkdir foo && cd foo
2npm init --yes foo
Update the generated package.json as required.
Now add the tsconfig.json file. Here’s a basic one:
1{
2 "compilerOptions": {
3 "target": "es5",
4 "sourceMap": true
5 }
6}
If you’re using VS Code, you may need to restart it for intellisense to work.
Install dependencies
1npm install --save typescript ts-node
2npm install --save-dev @types/node
We’ll need typescript and ts-node to run the app. We could install these globally as well, but I prefer to keep and use local dependencies.
@types/node has typings for node related elements like process.argv.
Create the entry point
Create a file called index.ts and add the following code:
1#!/usr/bin/env node
2
3console.log('hey there!')
Run the program
We can run our index.ts using:
1./node_modules/ts-node/dist/bin.js index.ts
I’m going to create a script out of the command in package.json to save me some keystrokes. Here’s what my package.json looks like:
1{
2 "name": "foo-ip",
3 "version": "0.0.1",
4 "scripts": {
5 "foo": "./node_modules/ts-node/dist/bin.js index.ts"
6 },
7 "dependencies": {
8 "ts-node": "^3.2.0",
9 "typescript": "^2.4.1"
10 },
11 "devDependencies": {
12 "@types/node": "^8.0.7"
13 }
14}
We can now run the console app using:
1npm run foo
If you’re familiar with typescript, you should be able to take it from here.
Writing our program
Okay, so let’s get to writing the program that displays our public IP. We’ll obtain our public IP by hitting the REST API at https://httpbin.org/ip using axios.
Install axios using:
1npm install --save axios
Replace the index.ts code with this:
1#!/usr/bin/env node
2
3import axios from 'axios'
4
5axios.get('https://httpbin.org/ip')
6 .then((response) => {
7 console.log(`Your IP is ${response.data.origin}`)
8 })
Running npm run foo should print your ip now.
Using CommonJS style libraries
Let’s say we need to display the text in cyan using chalk.
Install chalk using:
1npm install --save chalk
Their README expects us to use CommonJS’s require() syntax. We’ll have to do it the Typescript/ES6 way instead:
1import * as chalk from 'chalk'
You’ll need to do this for all libraries that require you to use require() to import it.
Our new code now becomes:
1#!/usr/bin/env node
2
3import axios from 'axios'
4import * as chalk from 'chalk'
5
6axios.get('https://httpbin.org/ip')
7 .then((response) => {
8 console.log(chalk.cyan(`Your IP is ${response.data.origin}`))
9 })

Taking it from here
That’s all you need to know to create a typescript console app. The rest is the same usual node thing, you know. If you’re trying to create a CLI, Peter Benjamin has a nice blogpost on creating one.