我正在尝试用TypeScript和Angular应用程序运行一个开发服务器,而不是每次都转译ts文件。
我发现我可以用ts-node运行.ts文件,但我也想观看.ts文件并重新加载我的应用程序/服务器。这方面的一个例子是命令gulp watch。
我正在尝试用TypeScript和Angular应用程序运行一个开发服务器,而不是每次都转译ts文件。
我发现我可以用ts-node运行.ts文件,但我也想观看.ts文件并重新加载我的应用程序/服务器。这方面的一个例子是命令gulp watch。
当前回答
步骤1:你可以简单地安装nodemon和ts-node(如果你已经安装了,跳过)
npm install --save-dev nodemon ts-node
步骤2:你可以在package.json中配置启动脚本
"start": "nodemon ./src/app.ts"
现在nodemon自动从项目中识别typescript,自己使用ts-node命令。使用npm start,它会自动编译/监视和重载。
如果你得到任何错误,如typescript模块没有在项目中找到。在项目文件夹中简单使用此命令。
npm link typescript
其他回答
我做过
"start": "nodemon --watch 'src/**/*.ts' --ignore 'src/**/*.spec.ts' --exec ts-node src/index.ts"
纱线开始。ts节点不像' Ts-node '
这里有一个替代HeberLZ的答案,使用npm脚本。
我的package.json:
"scripts": {
"watch": "nodemon -e ts -w ./src -x npm run watch:serve",
"watch:serve": "ts-node --inspect src/index.ts"
},
-e标志设置要查找的扩展, -w设置监视目录, -x执行脚本。
watch:serve脚本中的——inspect实际上是一个node.js标志,它只是打开调试协议。
nodemon和ts-node:
nodemon --watch source --ext ts,json --exec "node --loader ts-node/esm ./source/index.ts"
另一种方法是先在监视模式下使用tsc -w编译代码,然后在javascript上使用nodemon。这种方法在速度上与ts-node-dev相似,并且具有更类似于生产的优点。
"scripts": {
"watch": "tsc -w",
"dev": "nodemon dist/index.js"
},
步骤1:你可以简单地安装nodemon和ts-node(如果你已经安装了,跳过)
npm install --save-dev nodemon ts-node
步骤2:你可以在package.json中配置启动脚本
"start": "nodemon ./src/app.ts"
现在nodemon自动从项目中识别typescript,自己使用ts-node命令。使用npm start,它会自动编译/监视和重载。
如果你得到任何错误,如typescript模块没有在项目中找到。在项目文件夹中简单使用此命令。
npm link typescript