Installing the prerequisites
The contents of this chapter are expected to work on all major and up-to-date desktop operating systems, including Windows, OS X, and Linux.
As Node.js is widely used as a runtime for server applications as well as frontend build tools, we are going to make it the main playground of code in this book.
TypeScript compiler, on the other hand, is the tool that compiles TypeScript source files into plain JavaScript. It's available on multiple platforms and runtimes, and in this book we'll be using the Node.js version.
Installing Node.js
Installing Node.js should be easy enough. But there's something we could do to minimize incompatibility over time and across different environments:
- Version: We'll be using Node.js 6 with npm 3 built-in in this book. (The major version of Node.js may increase rapidly over time, but we can expect minimum breaking changes directly related to our contents. Feel free to try a newer version if it's available.)
- Path: If you are installing Node.js without a package manager, make sure the environment variable
PATH
is properly configured.
Open a console (a command prompt or terminal, depending on your operating system) and make sure Node.js as well as the built-in package manager npm
is working:
$ node -v
6.x.x
$ npm -v
3.x.x
Installing TypeScript compiler
TypeScript compiler for Node.js is published as an npm package with command line interface. To install the compiler, we can simply use the npm install
command:
$ npm install typescript -g
Option -g
means a global installation, so that tsc
will be available as a command. Now let's make sure the compiler works:
$ tsc -v Version 2.x.x
Note
You may get a rough list of the options your TypeScript compiler provides with switch -h
. Taking a look into these options may help you discover some useful features.
Before choosing an editor, let's print out the legendary phrase:
- Save the following code to file
test.ts
:function hello(name: string): void { console.log(`hello, ${name}!`); } hello('world');
- Change the working directory of your console to the folder containing the created file, and compile it with
tsc
:$ tsc test.ts
- With luck, you should have the compiled JavaScript file as
test.js
. Execute it with Node.js to get the ceremony done:$ node test.js hello, world!
Here we go, on the road to retire your CTO.