---
# System prepended metadata

title: typescript 編譯設定
tags: [nodejs]

---


# 初始化 nodejs 專案
```
npm init -y
```

# 安裝 typescript 相關套件
```
npm i express
npm i -D typescript @types/node @types/express
npm i -D ts-node nodemon
```

# 初始化 typescript 設定檔
```
npx tsc --init
```

# 修改 tsconfig.json
```json=
{
    "outDir": "./dist" 
}
```

## app.ts
```typescript=
mkdir src
touch src/app.ts
```

```typescript=
import express from 'express';
export const app = express();

app.get('/', (req, res) => {
  res.send('hello typescript');
});

export default app;
```

## index.ts
```shell=
touch index.ts
```

```typescript=
import { app } from './src/app';

app.listen(3000);

```

# 修改npm script
```javascript=
scripts: {
  "dev": "nodemon index.ts",
  "build": "tsc"
}
```

## 執行npm script
```shell=
npm run dev
npm run build
```