Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
526 views
in Technique[技术] by (71.8m points)

node.js - nodemon watches onjy JavaScript files not TypeScript

nodemon should be started on debugging in Visual Studio Code and watch TypeScript. On any TypeScript changes, it should re-compile them using ts-node. I'm struggling with many issues here. The currently most important one is that nodemon watches only the generated .jsfiles, but not the original .tsones - although I explicit let them watch typescript.

launch.json

{
    "type": "node",
    "request": "launch",
    "name": "nodemon",
    "runtimeExecutable": "nodemon",
    "program": "${workspaceFolder}/index.ts",
    "outFiles": [
        "${workspaceRoot}/dist/*.js"
    ],
    "restart": true,
    "console": "integratedTerminal",
    "internalConsoleOptions": "neverOpen",
    "args": [
        "--inspect-brk",
        "-x 'echo hello123'"
    ],
    "timeout": 30000
}

When I start debugging, "hello123" is printed. After saving index.ts nothing happens. If the generated dist/index.js script is saved by hand, it shows "hello123" too.

Of course, this is only to isolate the issue. In fact, I want to run a npm script to re-compile typescript like this:

"scripts": {
    "ts-node": "ts-node --inspect-brk index.ts"
}

But the main problem is: Why does nodemononly watches the generated js-files instead of their typescript ones? Seems like this is caused by outFiles attribute. Without it's not working, it shows an error that corresponding js files cant' be found.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can configure nodemon to watch typescript files as it does not watch .ts by default

Add nodemon.json file in root with the following content, and then just run nodemon:

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

or run nodemon as

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

I hope this will work.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...