【问题标题】:NestJS - exclude folder under watch modeNestJS - 在监视模式下排除文件夹
【发布时间】:2023-04-07 03:28:01
【问题描述】:

我在根目录中创建了一个公用文件夹来存储用户上传的文件。但是npm run start:dev模式下,每次上传文件,Nest都会检测到文件变化,重启服务器。我该怎么做才能避免这种情况?谢谢。

目录结构:

-project
 -dist
 -src
 -public
 -(other files)

【问题讨论】:

  • public 目录添加到exclue 数组中的tsconfig.json

标签: nestjs


【解决方案1】:

tsconfig.json 文件中执行我在以下示例中所做的操作:

{
  "compilerOptions": {
    "module": "commonjs",
    "declaration": true,
    "removeComments": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es2017",
    "sourceMap": true,
    "outDir": "./dist",
    "baseUrl": "./",
    "incremental": true,
    "paths": {

    }
  },
  "exclude": [
    "node_modules",
    "dist",
    "public"   <<-- Add the folder name here to exclude from updates
  ]
}

【讨论】:

  • 这不起作用
【解决方案2】:

tsconfig.json 下,在exclude 属性之后立即包含以下属性

  "include": [ "src"]

【讨论】:

  • 这个是正确的,只添加exclude是不够的
【解决方案3】:

tsconfig.json中插入以下内容:

...,
  "include": ["src"],
  "exclude": [
    "node_modules",
    "dist",
    "public"
  ]

【讨论】:

    【解决方案4】:

    只需添加

      "watchOptions": {
        "excludeFiles": ["yourfile"]
      }
    

    到您的 tsconfig.json。

    【讨论】:

    • 谢谢,这个解决方案对我有用。我使用“公共”目录作为排除文件,它可以正常工作,除非它不是文件而是目录。
    【解决方案5】:

    上面没有任何帮助,所以我发布了我的解决方案。除了在嵌套情况下tsconfig.json 中的“包含”和“排除”,如果您在nest-cli.json 中定义了assets 属性,则 tsconfig 中的排除将不起作用。您不能同时在 nest-cli.json 中观看资产并在 tsconfig 中忽略它们。

    我的无效nest-cli.json

    {
      "collection": "@nestjs/schematics",
      "sourceRoot": "src",
      "assets": [
        {
          "include": "frontend/dist/**",
          "watchAssets": false,
          "exclude": ["frontend/node_modules", "frontend/src"]
        }
      ]
    }
    

    所以当我尝试在tsconfig 中使用"exclude": ["frontend"] 时,它不起作用。

    【讨论】:

      【解决方案6】:

      我尝试做与其他答案相同的事情,但tsconfig.build.json

      {
        "extends": "./tsconfig.json",
        "exclude": ["node_modules", "test", "dist", "**/*spec.ts","uploads"]
      }
      

      添加了uploads 文件夹,它对我有用

      【讨论】:

      • 是的,这也适用于我
      猜你喜欢
      • 1970-01-01
      • 2013-09-09
      • 2013-01-18
      • 2021-10-23
      • 2019-07-02
      • 2017-12-09
      • 1970-01-01
      • 1970-01-01
      • 2022-01-01
      相关资源
      最近更新 更多