使用打字稿时nodemon不刷新

Posted

技术标签:

【中文标题】使用打字稿时nodemon不刷新【英文标题】:nodemon not refreshing when using typescript 【发布时间】:2018-08-26 13:01:23 【问题描述】:

我正在使用 NodeJs、express、typescript 和 nodemon 开发一个应用程序。

但是当我更改了ts文件中的一些代码时,页面没有刷新。

如何在js中编译ts文件并用nodemon(或其他工具)刷新浏览器?

package.json


  "name": "myapp",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "dependencies": 
    "bcryptjs": "^2.4.3",
    "body-parser": "^1.17.2",
    "debug": "^2.6.8",
    "dotenv": "^2.0.0",
    "express": "^4.15.3",
    "iconv-lite": "^0.4.17",
    "inversify": "^4.11.1",
    "jsonwebtoken": "^7.4.1",
    "mongoose": "^5.0.10",
    "morgan": "^1.8.2",
    "passport": "^0.3.2",
    "passport-jwt": "^2.2.1",
    "rxjs": "^5.4.2",
    "typescript": "^2.7.2",
    "winston": "^2.3.1"
  ,
  "devDependencies": 
    "@types/chai": "^4.0.0",
    "@types/debug": "0.0.29",
    "@types/dotenv": "^2.0.20",
    "@types/express": "^4.0.35",
    "@types/mocha": "^2.2.41",
    "@types/mongoose": "^4.7.15",
    "@types/morgan": "^1.7.32",
    "@types/node": "^6.0.77",
    "@types/passport": "^0.3.3",
    "@types/passport-jwt": "^2.0.20",
    "chai": "^4.0.2",
    "chai-http": "^3.0.0",
    "gulp": "^3.9.1",
    "gulp-clean": "^0.3.2",
    "gulp-typescript": "^3.1.7",
    "gulp-yaml": "^1.0.1",
    "mocha": "^3.4.2",
    "mocha-typescript": "^1.1.4"
  ,
  "scripts": 
    "start": "gulp build && nodemon dist/index.js",
    "build": "gulp build",
    "test": "gulp build && mocha -t 30000 dist/**/*.test.js"
  ,
  "repository": 
    "type": "git",
    "url": "git+https://github.com/devslaw/TypeScript-Node.js-REST-example.git"
  ,
  "keywords": [],
  "author": "Arthur Arakelyan <arthur@devslaw.com>",
  "license": "ISC",
  "homepage": "https://github.com/devslaw/TypeScript-Node.js-REST-example#readme"

所以知道,每当我进行更改时,我都需要停止服务器并再次运行 npm start

【问题讨论】:

替代方法是使用 ts-node How to watch and reload ts-node when typescript file changes的可能重复 【参考方案1】:

问题在于您的启动脚本正在观看dist/index.js。此文件夹仅在代码重新编译为纯 javascript 时才会更改。因此,您将需要查看 TypeScript 文件。最棒的是您可以更改 nodemon 的默认行为。您还需要将ts-node 添加到您的package.json

尝试将脚本设置为以下内容:

nodemon --watch 'src/**/*.ts' --exec 'ts-node' src/index.ts

然后,您还需要使用以下内容设置nodemon.json 文件:


  "watch": ["src"],
  "ext": "ts",
  "exec": "ts-node ./src/index.ts"

【讨论】:

以上是关于使用打字稿时nodemon不刷新的主要内容,如果未能解决你的问题,请参考以下文章

nodemon

Webstorm- Nodemon + Typescript + Docker - 调试器已连接但断点不起作用

使用 Typescript 编译运行 Nodemon?

自动刷新服务:nodemon

gulp+browserSync+nodemon 实现express 全端自动刷新的实践

Nodemon 未在 React-Express-Node 应用程序中刷新浏览器