Starting a Typescript Node project

Thursday, 29 July 2021

Starting a fresh NodeJS project is as simple as running yarn init and following the prompts (unless you like crafting package.json files yourself)

With your vanilla project initialised you can start writing Javascript referencing external packages. But why settle for second best when enabling Typescript couldn’t be easier.

Install the requisite packages -

1yarn add typescript ts-node
2yarn add -D @types/node

Initialise the tsconfig.json file -

1yarn run tsc —init

Now you can run Typescript files with -

1yarn run ts-node index.ts

To simplify things, you can add a script to your package.json -

1“scripts”: {
2  “start”: “node -r ts-node/register index.ts”
3}

Then you can just start your project with yarn start

That’s all it takes. Completely self contained, no additional dependencies or configuration required.