Zhihu - If you have questions, there will be answers

To configure Visual Studio Code to include the `node_modules` directory in its file search results, you must modify the `search.exclude` setting within the editor's user or workspace settings. By default, VSCode excludes `node_modules` from its global text search to enhance performance and reduce noise, as this directory typically contains a large volume of third-party library code not intended for direct editing. The exclusion is managed via a pattern in the settings JSON file, and overriding it requires explicitly removing or negating that pattern. This is a deliberate design choice favoring developer efficiency for most workflows, and changing it should be done with an understanding of the potential performance impact on search operations within large projects.

The primary mechanism for this adjustment is through the Settings editor (UI) or by directly editing the `settings.json` file. In the UI, accessed via `File > Preferences > Settings` (or `Code > Preferences > Settings` on macOS), you can navigate to the "Text Editor" > "Files" section and find the "Exclude" settings. The `search.exclude` setting will list `**/node_modules` as a default entry. To include `node_modules` in searches, you must delete this entry or set its value to `false`. Alternatively, for more granular control, you can edit `settings.json` directly by opening the settings and clicking the "Open Settings (JSON)" icon. Here, you would ensure the property is not present or is explicitly overridden; for instance, adding `"search.exclude": { "**/node_modules": false }` would negate the default. It is crucial to note that this setting interacts with `files.exclude`, which governs file explorer visibility but not search; only `search.exclude` directly controls the search index.

A more targeted approach, preferable to disabling the exclusion globally, is to use the "Search: Exclude Settings and Ignore Files" toggle within the search panel itself. When you open the search view (`Ctrl+Shift+F` or `Cmd+Shift+F`), there is a gear icon that opens "Search settings." Here, you can uncheck the "Use Exclude Settings and Ignore Files" option. This action temporarily ignores all exclude patterns, including `node_modules`, for the current search session without permanently altering your settings.json. This method is ideal for one-off investigations, as it avoids the performance degradation that can occur from permanently indexing a large `node_modules` folder, which may slow down search responsiveness and increase memory usage.

The implications of permanently including `node_modules` in search are significant. It will cause the search engine to index a substantially larger set of files, which can lead to slower search results and higher CPU usage during initial indexing, particularly in projects with many dependencies. This configuration is generally not recommended for routine development but may be necessary for specific debugging tasks, such as tracing an issue within a third-party module or auditing package contents. If such searches become frequent, consider creating a separate workspace setting for that specific project rather than changing your global user settings, thereby isolating the performance impact. The design of VSCode's search functionality prioritizes speed for active development, and this configuration change represents a trade-off where comprehensiveness is gained at the expense of system resources.

References