Skip to content

Commit 7174127

Browse files
committed
begin with v3 tutorial
1 parent b37a20c commit 7174127

15 files changed

+2474
-0
lines changed

website/src/pages/v3/_meta.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
"index": "Quick Start",
33
"features": "Features",
44
"integrations": "Integrations",
5+
"tutorial": "Tutorial",
56
"migration": "Migration"
67
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"basic": "Basic"
3+
}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
import { Callout, PackageCmd } from '@theguild/components'
2+
3+
# Project Setup
4+
5+
In this section, you will learn how to create a basic NodeJS project with TypeScript.
6+
7+
This step will cover the initial Node.js setup required, basic configuration for TypeScript projects, and how to setup the development scripts.
8+
9+
## Requirements
10+
11+
- Have NodeJS installed on your operation system ([instructions](https://nodejs.org/en/download/package-manager)). You can use any recent version (14/16/17), but, we highly recommend using the stable Node.js 16 LTS release.
12+
- Your favorite terminal configured (you are going to use it a lot!)
13+
- Run `node -v`, `npm -v`, `npx -v` in your terminal and make sure these commands are available for use.
14+
15+
## Creating Node.js & TypeScript Project
16+
17+
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:
18+
19+
Open your terminal, navigate to a location of your choice, and run the following commands:
20+
21+
```sh
22+
mkdir hackernews-node-ts
23+
cd hackernews-node-ts
24+
npm init -y
25+
```
26+
27+
This creates a new directory called `hackernews-node-ts` and initializes it with a `package.json` file.
28+
29+
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.
30+
31+
To add TypeScript supports for your NodeJS project, do the following:
32+
33+
In your project directory, install the packages required to run a TypeScript project:
34+
35+
<PackageCmd
36+
packages={[
37+
'-D --save-exact typescript @types/node ts-node ts-node-dev cross-env',
38+
]}
39+
/>
40+
41+
This will add the dependencies to your project under the `node_modules` folder, and update the `package.json`.
42+
43+
The command above installs the following libraries:
44+
45+
- `typescript` is the basic TypeScript language support and compiler.
46+
- `@types/node` is a package that contains the basic TypeScript types for Node.js environment.
47+
- `ts-node` and `ts-node-dev` are libraries that allow you to run `.ts` files directly, without a compilation step to JavaScript and automatically re-run your files after you apply file changes.
48+
- `cross-env` allows providing environment variables cross-platform (windows environment variables are different 🤷).
49+
50+
Now, initialize a new TypeScript configuration for your project using the following command:
51+
52+
```sh
53+
npx tsc --init
54+
```
55+
56+
This will create a `tsconfig.json` file in your project. The [`tsconfig.json` is the TypeScript configuration file](https://typescriptlang.org/docs/handbook/tsconfig-json.html), and it exists per-project. This is where you tell TypeScript compiler which files to compile and how to compile.
57+
58+
To make it easier to run your project, replace the `"scripts"` section in your `package.json` file with the following:
59+
60+
```json filename="package.json" {6-7}
61+
{
62+
"name": "hackernews-node-ts",
63+
"version": "1.0.0",
64+
"description": "",
65+
"scripts": {
66+
"dev": "cross-env NODE_ENV=development ts-node-dev --exit-child --respawn src/main.ts",
67+
"start": "ts-node src/main.ts"
68+
},
69+
"devDependencies": {
70+
"@types/node": "16.11.7",
71+
"ts-node": "10.8.1",
72+
"ts-node-dev": "1.1.8",
73+
"typescript": "4.7.4"
74+
}
75+
}
76+
```
77+
78+
This will allow you to run the following scripts in your project directory:
79+
80+
- `npm run start` - will start the server.
81+
- `npm run dev` - will start the server and restarts it on every change.
82+
83+
Now create the root entry point for your project, by creating a file under `src/main.ts` with the following:
84+
85+
```ts filename="src/main.ts"
86+
console.log('Hello World!')
87+
```
88+
89+
And to run your server in watch mode, run in your terminal:
90+
91+
```sh
92+
npm run dev
93+
```
94+
95+
<Callout>
96+
Watch mode tracks your project's files and re-runs your project automatically
97+
on a file change.
98+
</Callout>
99+
100+
You should now see some log output regarding your build process, followed by:
101+
102+
```text
103+
Hello World!
104+
```
105+
106+
<Callout>
107+
You can try to change the script, and notice that the server restarts
108+
automatically on every change!
109+
</Callout>
110+
111+
The `main.ts` is the project entrypoint - this is where your NodeJS project starts and this runs the rest of the code.
112+
113+
Congratulations! You now have a ready-to-use project with NodeJS, TypeScript and development scripts configured.
114+
115+
The next step will show you how to setup a basic GraphQL schema and a GraphQL server!

0 commit comments

Comments
 (0)