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

后端 未结 11 1515
梦如初夏
梦如初夏 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:27

    I've dumped nodemon and ts-node in favor of a much better alternative, ts-node-dev https://github.com/whitecolor/ts-node-dev

    Just run ts-node-dev src/index.ts

    0 讨论(0)
  • 2020-11-30 17:27

    Here's an alternative to the HeberLZ's answer, using npm scripts.

    My package.json:

      "scripts": {
        "watch": "nodemon -e ts -w ./src -x npm run watch:serve",
        "watch:serve": "ts-node --inspect src/index.ts"
      },
    
    • -e flag sets the extenstions to look for,
    • -w sets the watched directory,
    • -x executes the script.

    --inspect in the watch:serve script is actually a node.js flag, it just enables debugging protocol.

    0 讨论(0)
  • 2020-11-30 17:27

    Specifically for this issue I've created the tsc-watch library. you can find it on npm.

    Obvious use case would be:

    tsc-watch server.ts --outDir ./dist --onSuccess "node ./dist/server.js"

    0 讨论(0)
  • 2020-11-30 17:27

    Add "watch": "nodemon --exec ts-node -- ./src/index.ts" to scripts section of your package.json.

    0 讨论(0)
  • 2020-11-30 17:27

    you could use ts-node-dev

    It restarts target node process when any of required files changes (as standard node-dev) but shares Typescript compilation process between restarts.

    Install

    yarn add ts-node-dev --dev
    

    and your package.json could be like this

    "scripts": {
      "test": "echo \"Error: no test specified\" && exit 1",
      "tsc": "tsc",
      "dev": "ts-node-dev --respawn --transpileOnly ./src/index.ts",
      "prod": "tsc && node ./build/index.js"
    }
    
    0 讨论(0)
  • 2020-11-30 17:29

    add this to your package.json file

    scripts {
    "dev": "nodemon --watch '**/*.ts' --exec 'ts-node' index.ts"
    }
    

    and to make this work you also need to install ts-node as dev-dependency

    yarn add ts-node -D
    

    run yarn dev to start the dev server

    0 讨论(0)
提交回复
热议问题