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

时间:2020-04-09 18:56:45

标签: node.js express ts-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文件和文件夹?

1 个答案:

答案 0 :(得分:3)

根据ts-node-dev文档,命令为:

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 run build时,将创建一个dist文件夹,并且其中将包含我的所有.js文件