nodemon+typescript 简单 NodeJS 项目配置

654 阅读1分钟

nodemon+typescript 简单 NodeJS 项目配置

初始化

初始化项目

npm init -y

安装依赖

npm install typescript @types/node ts-node nodemon -D

配置文件

nodemon.json

详细配置见:nodemon --help options nodemon github

nodemon.json

{
  "verbose": false,
  "watch": ["src"],
  "ext": "js,ts,json",
  "quiet": true,
  "exec": "ts-node src/index.ts"
}

tsconfig.json

保持commonjs模块输出;设置打包目录build;输出ES标准;

{
  "compilerOptions": {
    "target": "ESNext",
    "module": "commonjs",
    "outDir": "./build",
  }
}

package.json

添加命令

{
  "type": "commonjs",
  "main": "build/index.js",
  "scripts": {
    "test": "echo "Error: no test specified" && exit 1",
    "build": "tsc",
    "dev": "npx nodemon -x ts-node src/index.ts",
    "prod": "node ./build/index.js"
  }
}