In this section, you will learn how to create a basic NodeJS project with TypeScript.
This step will cover the initial NodeJS setup required, basic configuration for TypeScript projects, and how to setup the development scripts.
node -v
, npm -v
, npx -v
in your terminal and make sure these commands are available for use.This tutorial teaches you how to build a NodeJS project from scratch, so the first thing you need to do is create the directory that’ll hold the files for your project:
This creates a new directory called hackernews-node-ts
and initializes it with a package.json
file.
The package.json
is the configuration file for the Node.js app you’re building. It lists all dependencies and other configuration options (such as scripts) needed for the app.
To add TypeScript support for your NodeJS project, do the following:
This will add the dependencies to your project, and will update the package.json
.
The command above will get you the following libraries installed in the project:
typescript
is the basic TypeScript language support and compiler.@types/node
is a package that contains the basic TypeScript types for NodeJS environment.ts-node
and ts-node-dev
are libraries that allows you to run .ts
files directly, without a compilation step to JavaScript.This will create a tsconfig.json
file in your project. The tsconfig.json
is the TypeScript configuration file, and it exists per-project. This is where you tell TypeScript compiler which files to compile and how to compile.
This will allow you to run the following scripts in your project directory:
npm run start
- will start the server.npm run dev
- will start the server and restarts it on every change.And to run your server in watch mode, run in your terminal:
npm run dev
Watch mode tracks your project’s files, and re-runs your project automatically on a file change.
You should now see some log output regarding your build process, followed by:
Hello World!
You can try to change the script, and notice that the server restarts automatically on every change!
The index.ts
is the project entrypoint - this is where you NodeJS project starts and this runs the rest of the code.
Congratulations! You now have a ready-to-use project with NodeJS, TypeScript and development scripts configured.
The next step will show you how to setup a basic GraphQL schema and a GraphQL server!