为什么我的 node_modules 文件夹在执行“npm install”命令后变灰?

qui*_* 6 visual-studio-code

我想知道为什么我的 node_modules 文件夹在我运行命令后在我的 VS Code 编辑器中显示为灰色npm install

这是我文件夹的图片:

在此处输入图片说明

Que*_*yot 8

这是因为您的文件node_modules中引用了您的文件夹.gitignore
Visual Studio 通过将其灰显来告诉您版本控制会忽略此文件夹。


art*_*tze 8

包含在其中的文件/文件夹显示.gitignore为灰色。通常node_modules文件夹包含在.gitignore.


Dom*_*omi 6

正如已经指出的,VSCode 自动忽略.gitignore. 但是,它默认也会明确忽略某些文件夹。所有这些都可以配置。以下是您可能想要采取的步骤:

  1. 查看有关工作区设置的官方文档
  2. 打开相关设置(可以在User(全局)和之间进行选择Workspace)。我通常通过CTRL(或Command在 Mac 上) + SHIFT+ P->执行此操作> settings,这将向您显示相关选择:在此输入图像描述。对于每一个,您都可以选择 UI 或JSON变体。
  3. 还要确保通过检查默认设置Peferences: Open Default Settings (JSON)。您会发现,截至目前(2021 年 5 月),**/node_modules也被自动排除,独立于其他忽略文件。目前默认设置如下所示:
     // Configure glob patterns for excluding files and folders in fulltext searches and quick open. Inherits all glob patterns from the `files.exclude` setting. Read more about glob patterns [here](https://code.visualstudio.com/docs/editor/codebasics#_advanced-search-options).
     "search.exclude": {
         "**/node_modules": true,
         "**/bower_components": true,
         "**/*.code-search": true
     },
    
     // Controls whether to use global `.gitignore` and `.ignore` files when searching for files.
     "search.useGlobalIgnoreFiles": false,
    
     // Controls whether to use `.gitignore` and `.ignore` files when searching for files.
     "search.useIgnoreFiles": true,
    
    Run Code Online (Sandbox Code Playgroud)

启用搜索node_modules

  "search.exclude": {
    "**/node_modules": false
  },
  "search.useIgnoreFiles": false
Run Code Online (Sandbox Code Playgroud)
  • 警告:这会大大减慢速度,因为node_modules需要永远进行搜索 - 至少在冷启动期间,即未缓存内容时。(请注意,很难估计它们的缓存是如何工作的,因为它随着时间的推移而不断发展。)
  • 为了缓解这种情况,您可能需要添加一些额外的search.exclude设置。请注意,您必须明确列出您不想要的所有内容,因为......

VSCode搜索的一大缺点

search.exclude不支持:

  • 负前瞻/后瞻
  • 相互级联查询

这样的事情是行不通的

  // does NOT work! :((
  "search.exclude": {
    "**/node_modules/@types": false,
    "**/node_modules": true
  }
Run Code Online (Sandbox Code Playgroud)

这是一个重大问题,在几乎著名的#869 期中已经被广泛讨论了6 年!加入并添加 100 多条评论!:)

更新:截至 2021 年 8 月 14 日,该问题已从积压队列中删除并放入On Deck队列中(但这也可能没有多大意义)。