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.
npx -vin 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 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
The command above will get you the following libraries installed in the project:
typescriptis the basic TypeScript language support and compiler.
@types/nodeis a package that contains the basic TypeScript types for NodeJS environment.
ts-node-devare libraries that allows you to run
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:
You can try to change the script, and notice that the server restarts automatically on every change!
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!