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

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

    This works for me:

    nodemon src/index.ts
    

    Apparently thanks to since this pull request: https://github.com/remy/nodemon/pull/1552

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

    i did with

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

    and yarn start.. ts-node not like 'ts-node'

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

    Another way could be to compile the code first in watch mode with tsc -w and then use nodemon over javascript. This method is similar in speed to ts-node-dev and has the advantage of being more production-like.

     "scripts": {
        "watch": "tsc -w",
        "dev": "nodemon dist/index.js"
      },
    
    0 讨论(0)
  • 2020-11-30 17:36

    I would prefer to not use ts-node and always run from dist folder.

    To do that, just setup your package.json with default config:

    ....
    "main": "dist/server.js",
      "scripts": {
        "build": "tsc",
        "prestart": "npm run build",
        "start": "node .",
        "dev": "nodemon"
      },
    ....
    

    and then add nodemon.json config file:

    {
      "watch": ["src"],
      "ext": "ts",
      "ignore": ["src/**/*.spec.ts"],
      "exec": "npm restart"
    }
    

    Here, i use "exec": "npm restart"
    so all ts file will re-compile to js file and then restart the server.

    To run while in dev environment,

    npm run dev
    

    Using this setup I will always run from the distributed files and no need for ts-node.

    0 讨论(0)
  • 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.

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