当ts node dev重新启动服务器时运行脚本



我目前在node js服务器上安装了ts node dev,并让它使用--respawn标志监视我的.ts文件。我的服务器正在成功重新启动,但当.ts文件发生更改时,我需要运行类似yarn build的命令来编译更改的文件(或所有文件(,以确保我的更改存在于重新启动的服务器中。

当服务器重新启动时,我似乎找不到运行脚本的方法。

我试过这样的东西:

"start": "ts-node-dev --respawn --transpile-only yarn build && src/main.js"

在我的package.json中,但它试图将我的yarn build命令解析为文件名。

如何将脚本绑定到重新启动过程中?

{
"compileOnSave": true,
"compilerOptions": {
"target": "es2017",
"lib": ["es2017", "esnext.asynciterable"],
"module": "commonjs",
"moduleResolution": "node",
"rootDir": ".",
"sourceMap": true,
"newLine": "LF",
"forceConsistentCasingInFileNames": true,
"noImplicitReturns": true,
"strict": true,
// For typeORM support
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"strictPropertyInitialization": false,
"pretty": true,
"typeRoots": ["node_modules/@types"]
},
"include": ["src/**/*", "db/**/*", "swagger/**/*", "test/**/*"]
}

这是我的tsconfig.json,如何使用watch命令指定监视所有src文件和文件夹?

根据节点开发文档,命令为:

ts-node-dev --respawn --transpileOnly <YOUR TS FILE>

你应该在package.json:上试试你的启动脚本

"dev": "ts-node-dev --respawn --transpileOnly --watch src,db,swagger,test src/main.ts"
"start": "node dist/src/main.js"

在你的tsconfig.json文件中,你应该有outDir配置,这个配置定义了你编译的代码将被放置的文件夹,例如,看看我的tsconfig.json:

{
"compilerOptions": {
"module": "commonjs",
"esModuleInterop": true,
"target": "ES2017",
"moduleResolution": "node",
"outDir": "./dist",
"strict": true,
"strictPropertyInitialization": false,
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true
},
"exclude": ["node_modules"],
"include": [
"./src/**/*.tsx",
"./src/**/*.ts",
"src/__tests__",
"./src/**/*",
]  
}

我有outDir配置,当我运行tsc或npm运行构建时,会创建一个dist文件夹,里面会有我所有的.js文件

最新更新