我正在尝试用TypeScript和Angular应用程序运行一个开发服务器,而不是每次都转译ts文件。
我发现我可以用ts-node运行.ts文件,但我也想观看.ts文件并重新加载我的应用程序/服务器。这方面的一个例子是命令gulp watch。
我正在尝试用TypeScript和Angular应用程序运行一个开发服务器,而不是每次都转译ts文件。
我发现我可以用ts-node运行.ts文件,但我也想观看.ts文件并重新加载我的应用程序/服务器。这方面的一个例子是命令gulp watch。
当前回答
这里有一个替代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标志,它只是打开调试协议。
其他回答
修改后清除控制台日志
Javascript:
"start": "nodemon -x \"cls && node\" index.js",
打字稿:
"start": "nodemon -x \"cls && ts-node\" index.ts",
第一步-在deDependencies中安装以下包
npm i -D @types/express @types/node nodemon ts-node tsc typescript
或者使用纱线
yarn add -D @types/express @types/node nodemon ts-node tsc typescript
第二步—在您的tsconfig中使用此配置。json文件
{
"compilerOptions": {
"target": "es6" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', 'ES2021', or 'ESNEXT'. */,
"module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */,
"lib": [
"DOM",
"ES2017"
] /* Specify library files to be included in the compilation. */,
"sourceMap": true /* Generates corresponding '.map' file. */,
"outDir": "./dist" /* Redirect output structure to the directory. */,
"rootDir": "./src" /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */,
"strict": true /* Enable all strict type-checking options. */,
"moduleResolution": "node" /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */,
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */,
"skipLibCheck": true /* Skip type checking of declaration files. */,
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
},
"exclude": ["node_modules"],
"include": ["./src"]
}
第三步——在包中使用这些脚本。json文件
"scripts": {
"start": "node ./dist/server.js",
"dev": "nodemon -L ./src/server.ts && tsc -w"
},
这对我来说很管用:
nodemon src/index.ts
显然,由于这个拉请求:https://github.com/remy/nodemon/pull/1552
你可以使用ts-node-dev
当任何必需的文件发生变化时,它重新启动目标节点进程(作为标准的node-dev),但在重新启动之间共享Typescript编译过程。
安装
yarn add ts-node-dev --dev
还有你的包裹。Json可以是这样的
"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"
}
你现在可以简单地用npm install——save-dev ts-node nodemon,然后用.ts文件运行nodemon,它就可以工作了:
nodemon app.ts
以前的版本:
我在开发环境中也遇到了同样的问题,直到我注意到没有一个demon的API允许我们为了执行自定义命令而改变它的默认行为。
例如,对于nodemon的最新版本:
nodemon --watch "src/**" --ext "ts,json" --ignore "src/**/*.spec.ts" --exec "ts-node src/index.ts"
或者创造一个无恶魔。Json文件,内容如下:
{
"watch": ["src"],
"ext": "ts,json",
"ignore": ["src/**/*.spec.ts"],
"exec": "ts-node ./src/index.ts" // or "npx ts-node src/index.ts"
}
然后无参数地运行。
通过这样做,您将能够实时重新加载ts-node进程,而不必担心底层实现。
更古老的nodemon版本:
nodemon --watch 'src/**/*.ts' --ignore 'src/**/*.spec.ts' --exec 'ts-node' src/index.ts
或者更好:将nodemon的配置外部化为一个nodemon。json文件,包含以下内容,然后运行nodemon,正如Sandokan建议的那样:
{
"watch": ["src/**/*.ts"],
"ignore": ["src/**/*.spec.ts"],
"exec": "ts-node ./index.ts"
}