jsb*_*eJS 11 javascript node.js typescript nodemon visual-studio-code
我希望在使用命令保存的每个文件上编译我的打字稿文件tsc。
如何将 tsc 命令与 nodemon 在build:live脚本中运行的命令结合使用
"scripts": {
"start": "npm run build:live",
"build:live": "nodemon --watch '*.ts' --exec 'ts-node' app.ts",
}
Run Code Online (Sandbox Code Playgroud)
此脚本会导致 nodemon 调用自身两次或三次:
"build:live": "nodemon --watch '*.ts' --exec 'ts-node app.ts & tsc'",
Run Code Online (Sandbox Code Playgroud)
kjs*_*js3 20
Nodemon 现在将自动检测和运行.ts文件ts-node。顺便说一句,它实际上.py也.rb可以使用 python 和 ruby运行和文件,您可以--exec为其他人自定义。这是nodemon 中相关代码的链接。
所以以下应该没问题:
"scripts": {
"dev": "nodemon app.ts"
}
Run Code Online (Sandbox Code Playgroud)
Vit*_*ynx 13
根据当前的答案,您可能会在使用 ES 模块时遇到问题。使用时不需要nodemon tsc-watch。它利用增量编译,使应用程序的重新启动速度更快。
我发现以下方法效果最好:
"start": "tsc-watch --onSuccess \"node ./dist/app.js\""
Run Code Online (Sandbox Code Playgroud)
可以outDir在您的中定义tsconfig
看起来它会实现你想要的:
"start": "tsc-watch --project . --outDir ./dist --onSuccess \"nodemon ./dist/bin/www.js\""
Run Code Online (Sandbox Code Playgroud)
来源:https : //github.com/Microsoft/TypeScript/issues/12996#issuecomment-349277673
从 TypeScript 3.8+ 开始,您现在可以使用:
tsc --watch
Run Code Online (Sandbox Code Playgroud)
https://www.typescriptlang.org/docs/handbook/configuring-watch.html
然后您可以使用nodemon编译后的代码,例如nodemon dist/app.js.
小智 5
您可以在项目根目录中创建一个nodemon.json,并在其中添加以下代码:
{
"ext": "*.ts",
"exec": "tsc && ts-node app.ts"
}
Run Code Online (Sandbox Code Playgroud)
并更新您的脚本,如下所示:
"scripts": {
"start": "npm run build:live",
"build:live": "nodemon",
}
Run Code Online (Sandbox Code Playgroud)
发生的情况是,nodemon 将检查所有扩展名为“.ts”的文件并启动 tsc,然后启动 ts-node。
| 归档时间: |
|
| 查看次数: |
9566 次 |
| 最近记录: |