# Setup NodeJS Project

#### ติดตั้ง module พื้นฐานเพื่อให้ nodejs รัน typescript ได้

```batch
#Npm
npm init
npm install typescript --save-dev
npm install @types/node --save-dev 

#Yarn
yarn init
touch yarn.lock
touch .yarnrc.yml
กรณีต้องการ Node Resolution Strategy เป็น pnp
###.yarnrc.yml##
    enableGlobalCache: true
    nodeLinker: pnp
    yarnPath: ".yarn/releases/yarn-berry.cjs"
yarn add typescript @types/node -D
yarn dlx @yarnpkg/sdks vscode
##Reload Windows##
    Command + Shift + P ==> Reload Window
```

#### สร้างตัว tsconfig.json

```batch
#Npm
npx tsc --init --rootDir src --outDir build \ --esModuleInterop --resolveJsonModule --lib es7 \ --module commonjs --allowJs true --noImplicitAny true
#Yarn
yarn run tsc --init --rootDir src --outDir build \ --esModuleInterop --resolveJsonModule --lib es7 \ --module commonjs --allowJs true --noImplicitAny true
```

#### สร้างไฟล์ src/index.ts

```batch
mkdir src 
touch src/index.ts 
```

#### ติดตั้ง  ts-node ,ts-node-dev

{% hint style="info" %}
ts-node-dev เป็นโปรแกรมที่คอย update update โปรแกรมหรือ auto restart nodejs ของเราทันทีเมื่อเราทําการ กด save&#x20;
{% endhint %}

```batch
#Npm
npm install --save-dev ts-node ts-node-dev
#Yarn
yarn add ts-node ts-node-dev -D
touch nodemon.json
```

#### ติดตั้ง rimraf

{% hint style="info" %}
rimraf เป็น module สำหรับใช้ลบไฟล์ ที่ทำงานเหมือนกันการรันคำสั่ง rm-rf&#x20;
{% endhint %}

```batch
#Npm
npm install --save-dev rimraf
#Yarn
yarn add rimraf -D
```

#### ติดตั้ง dotenv

{% hint style="info" %}
&#x20;dotenv เป็นไฟล์ที่ใช้เก็บค่าพารามิเตอร์ต่างๆที่ต้องการนำมาใช้กับ application

<mark style="color:red;">\*\*ไฟล์นี้จะไม่ upload ขั้น git</mark>
{% endhint %}

```batch
npm install dotenv
touch .env
```

#### แก้ไขไฟล์

```json
#nodemon.json
{
  "watch": ["src"],
  "ext": ".ts,.js",
  "ignore": [],
  "exec": "ts-node ./src/index.ts"
}
```

```json
#package.json
#Npm
"scripts": {
    "start": "run build && node build/index.js",
    "dev": " ts-node-dev --respawn --transpile-only ./src/index.ts",
    "build": "rimraf ./build && tsc"
    }
#Yarn
"scripts": {
     "start": "run build && node build/index.js",
    "dev": " ts-node-dev --respawn --transpile-only ./src/index.ts",
    "build": "rimraf ./build && tsc"
    }
```

{% hint style="info" %}
คำสั่ง --save-dev

package.json จะแยกเป็น 2 ส่วนคือส่วนที่เป็น

devDependencies และ dependencies

การใช้ --save-dev จะเป็นการบันทึกว่า module ที่ install จะใช้ใน devDependencies
{% endhint %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://kidkung.gitbook.io/node-js/setup-nodejs-project.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
