提问者:小点点

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


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

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

我试过这样的事情:

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

但它试图将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个答案

匿名用户

根据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运行build时,会创建一个dist文件夹,里面有我所有的.js文件