This gist will walk you through configuring your Node.js Project so that you can write your code in TypeScript using Visual Studio Code. These isntructions were written for attendees at Microsoft Build doing a super secret thing I can't talk about (ooo, the suspense!), but they also will work for any Node.js project written in TypeScript.
Before we get started configuring TypeScript, we need to initialize a new Node.js project. If you have not already installed Node.js, please head over to nodejs.org and install the latest LTS version of Node.js. Once this is done, create a folder for your Node.js project and open a terminal in this folder. In the terminal, run the command:
npm init
This command will ask you a series of questions. You can use the defaults for each question here because they don't really matter. These only matter if you intend to publish your module to npmjs.com, which we will not be doing.
Now that we've created a Node.js project, we're ready to configure TypeScript! Let's start by installing TypeScript for Node.js by running the following commands:
npm install --save-dev typescript @types/node
This will install both the TypeScript compiler as well as the Node.js TypeScript type definitions (tl;dr @types/node
allows TypeScript to understand the Node.js APIs. Read more here).
Awesome! Now we're ready to configure TypeScript. Let's start by creating a TypeScript configuration file by running
npx tsc --init
Now let's open our project using VS Code. You can go to File->Open
on *NIX platforms and File->Open Folder
on Windows, and then select our project folder. Let's start by creating a src
folder by right clicking in the empty area of the project folder in the lower-left corner of VS Code and selecting New Folder
. Then, let's create an empty file in src
called index.ts
by right clicking on the src
folder in the lower left and slecting New File
. We should have the following directory structure so far:
/
- node_modules/
- src/
- index.ts
- package.json
- tsconfig.json
Let's open tsconfig.json
and make a few changes so that the following entries have been changed (leave everything else the same):
{
"compilerOptions": {
"outDir": "dist",
"rootDir": "src",
},
"include": [
"src/**/*"
]
}
Next, let's open package.json
and create a "build" command. Let's change package.json
so that the "scripts" and "main" section looks like this, leaving everything else unchanged:
{
"main": "dist/index.js",
"scripts": {
"build": "tsc",
"start": "node dist/index.js"
}
},
}
And we're done! We can now compile our code by running npm run build
and we can run our code using npm start
. We're not necessarily done though. Most Node.js modules are not written in TypeScript. If you use any third party modules, such as the request
module, you'll need to install TypeScript support for them. To add support for the request
module, run this command:
npm install --save-dev @types/request
With this, you're ready to write all your Node.js code with type safety bliss. Happy hacking!
I've configured many times TypeScript in a Node.js project and I ALWAYS forget the initial steps. I was looking for something like this, straight to the point. Thank you for this gist. Probably I'm going to visit it many times from now on.