Skip to content
Notifications
Clear all

Step-by-step: Setting up Cursor rules to ignore generated code and node_modules.

2 Posts
2 Users
0 Reactions
21 Views
(@cameronj)
Reputable Member
Joined: 3 months ago
Posts: 324
Topic starter   [#27617]

Let's be honest: if you're using Cursor for anything resembling a modern JavaScript/TypeScript project, the first ten minutes of your experience will be spent watching it desperately try to index every single file in `node_modules` and propose autocompletions for `left-pad`. The marketing suggests an AI that understands your codebase, but out-of-the-box it seems to understand that you have 200,000 dependencies more than it understands your actual application logic.

I've seen a few breezy "just ignore it!" posts, but they typically omit the fact that Cursor's rule system is a bit of a maze. It's not just a single `.cursorignore` file; there's a hierarchy of settings, and if you don't set them all, you'll still get the phantom suggestions from the depths of your dependency tree. So, for those of us who'd prefer our AI assistant to focus on the code we wrote, here's the complete, multi-layered approach.

First, create a `.cursorignore` file in your project root. This is the primary filter for what Cursor's background indexer will even look at. Treat it like a `.gitignore`.

```plaintext
# Dependencies
node_modules/
**/node_modules/*
.next/
dist/
build/
*.log
coverage/
.env*.local
```

That's a good start, but it's not sufficient. You also need to adjust the *autocomplete* rules separately. Open Cursor's settings (Cmd/Ctrl + ,), navigate to the "Autocomplete" section, and find the "Files to exclude" setting. You'll want to add patterns there as well.

```json
"filesToExclude": {
"**/node_modules/**": true,
"**/.next/**": true,
"**/dist/**": true,
"**/*.log": true,
"**/coverage/**": true
}
```

Finally, and this is the step most tutorials blissfully ignore, you should tell Cursor's *context* engine to avoid these paths. When you use `@` to reference code, it shouldn't be scraping files from `node_modules`. In the settings, under "Context," you'll often find a similar exclusion list or a way to prioritize certain directories. Ensure your exclusion patterns are mirrored there.

After applying all three layers—global ignore, autocomplete exclude, and context exclude—you should finally stop getting suggestions about whether to import `chalk` from the 17th nested `node_modules` folder. It's baffling that this isn't the default configuration for JavaScript/TypeScript projects, but here we are, manually teaching a tool marketed for developers to ignore the most basic noise in a developer's environment.

-- Cam


Trust but verify.


   
Quote
(@brianw5)
Reputable Member
Joined: 3 months ago
Posts: 276
 

Yep, that `.cursorignore` is the right first move. I've found you need to pair it with the project-level settings though, or the built-in completions can still pull from node_modules. In Cursor, go to Settings > Project, and add patterns there too.

I also throw in patterns for lockfiles and Docker build directories. It's a bit redundant but covers all the indexers.

```json
{
"exclude": ["**/node_modules", "**/.next", "**/dist", "**/*.lock", "**/.docker/**"]
}
```

The combo finally got it to stop suggesting `require('lodash')` in my ES modules.


Automate all the things.


   
ReplyQuote