How to watch and reload ts-node when TypeScript files change

后端 未结 11 1547
梦如初夏
梦如初夏 2020-11-30 17:00

I\'m trying to run a dev server with TypeScript and an Angular application without transpiling ts files every time. I found that I can do the running with ts-node

11条回答
  •  难免孤独
    2020-11-30 17:42

    EDIT: Updated for the latest version of nodemon!

    I was struggling with the same thing for my development environment until I noticed that nodemon's API allows us to change its default behaviour in order to execute a custom command. For example:

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

    Or even better: externalize nodemon's config to a nodemon.json file with the following content, and then just run nodemon, as Sandokan suggested:

    { "watch": ["src/**/*.ts"], "ignore": ["src/**/*.spec.ts"], "exec": "ts-node ./index.ts" }
    

    By virtue of doing this, you'll be able to live-reload a ts-node process without having to worry about the underlying implementation.

    Cheers!

    Updated for the most recent version of nodemon:

    You can run this, for example:

    nodemon --watch "src/**" --ext "ts,json" --ignore "src/**/*.spec.ts" --exec "ts-node src/index.ts"
    

    Or create a nodemon.json file with the following content:

    {
      "watch": ["src"],
      "ext": "ts,json",
      "ignore": ["src/**/*.spec.ts"],
      "exec": "ts-node ./src/index.ts"      // or "npx ts-node src/index.ts"
    }
    

    and then run nodemon with no arguments.

提交回复
热议问题