Skip to main content
RapidDev - Software Development Agency
cursor-tutorial

Why Cursor Generates Invalid JavaScript

Cursor can generate JavaScript that uses patterns banned in strict mode, such as undeclared variables, duplicate parameters, with statements, and octal literals. Since most modern projects enforce strict mode through ES modules or explicit 'use strict' directives, this code fails at runtime. Adding .cursor/rules/ with strict mode patterns and combining with ESLint's strict-mode rules ensures Cursor generates valid, strict-mode-compliant JavaScript.

What you'll learn

  • How to create Cursor rules that enforce strict mode compliance
  • Which JavaScript patterns Cursor generates that violate strict mode
  • How to combine ESLint strict rules with Cursor project rules
  • How to fix existing strict mode violations using Cmd+K
Book a free consultation
4.9Clutch rating
600+Happy partners
17+Countries served
190+Team members
Beginner6 min read10-15 minCursor Free+, JavaScript/TypeScript projectsMarch 2026RapidDev Engineering Team
TL;DR

Cursor can generate JavaScript that uses patterns banned in strict mode, such as undeclared variables, duplicate parameters, with statements, and octal literals. Since most modern projects enforce strict mode through ES modules or explicit 'use strict' directives, this code fails at runtime. Adding .cursor/rules/ with strict mode patterns and combining with ESLint's strict-mode rules ensures Cursor generates valid, strict-mode-compliant JavaScript.

Why Cursor generates invalid JavaScript and how to fix it

JavaScript strict mode prohibits error-prone patterns like implicit global variables, duplicate function parameters, and the with statement. ES modules automatically enable strict mode, so any non-strict code Cursor generates fails silently or throws errors. This tutorial configures Cursor to always generate strict-mode-compliant code.

Prerequisites

  • Cursor installed with a JavaScript or TypeScript project
  • Basic understanding of JavaScript strict mode
  • ESLint configured in the project
  • Familiarity with Cursor project rules

Step-by-step guide

1

Create a strict mode compliance rule

Add a project rule that lists specific strict mode violations to avoid. TypeScript projects get most of these for free, but Cursor can still generate patterns that cause runtime issues in JavaScript files.

.cursor/rules/strict-mode.mdc
1---
2description: Enforce JavaScript strict mode compliance
3globs: "*.js,*.jsx,*.mjs"
4alwaysApply: true
5---
6
7# Strict Mode Compliance
8- NEVER use undeclared variables (always use const, let, or var)
9- NEVER use the 'with' statement
10- NEVER use duplicate parameter names in functions
11- NEVER use octal numeric literals (0644) use 0o644 instead
12- NEVER assign to read-only properties or undeletable properties
13- NEVER use 'arguments.callee' or 'arguments.caller'
14- NEVER use eval() to create variables in surrounding scope
15- ALWAYS declare variables before use
16- ALWAYS use 'use strict' at the top of .js files (or use ES modules)
17
18## TypeScript files are strict by default if tsconfig has strict: true

Expected result: Cursor generates JavaScript that complies with strict mode restrictions.

2

Add ESLint strict mode rules

Configure ESLint rules that catch strict mode violations. These rules create a feedback loop where Cursor reads lint errors via @Lint Errors and adjusts its output automatically.

eslint.config.js
1// eslint.config.js
2import js from '@eslint/js';
3
4export default [
5 js.configs.recommended,
6 {
7 rules: {
8 'strict': ['error', 'safe'],
9 'no-undef': 'error',
10 'no-var': 'error',
11 'no-with': 'error',
12 'no-octal': 'error',
13 'no-eval': 'error',
14 'no-caller': 'error',
15 'no-implicit-globals': 'error',
16 'no-shadow-restricted-names': 'error',
17 },
18 },
19];

Expected result: ESLint catches strict mode violations in Cursor-generated code and feeds errors back to Cursor.

3

Fix existing violations with Cmd+K

Select code with strict mode violations and use Cmd+K to fix them. Common fixes include replacing undeclared variables with const/let, removing with statements, and converting octal literals to the 0o prefix syntax.

Cmd+K prompt
1Fix all strict mode violations in this code:
2- Add const/let declarations for any undeclared variables
3- Replace 'with' statements with explicit property access
4- Replace octal literals (0644) with ES6 octal syntax (0o644)
5- Remove duplicate function parameter names
6- Replace arguments.callee with named function references
7- Ensure 'use strict' is present at file top if not using ES modules

Expected result: All strict mode violations are fixed while maintaining the original code behavior.

4

Verify with TypeScript strict configuration

If your project uses TypeScript, enable strict mode in tsconfig.json. TypeScript strict mode catches most JavaScript strict mode violations at compile time, providing an additional safety layer beyond Cursor rules.

tsconfig.json
1{
2 "compilerOptions": {
3 "strict": true,
4 "noImplicitAny": true,
5 "strictNullChecks": true,
6 "strictFunctionTypes": true,
7 "strictBindCallApply": true,
8 "noImplicitThis": true,
9 "alwaysStrict": true,
10 "noUnusedLocals": true,
11 "noUnusedParameters": true,
12 "noImplicitReturns": true,
13 "noFallthroughCasesInSwitch": true
14 }
15}

Expected result: TypeScript compiler catches strict mode violations at build time, complementing Cursor rules and ESLint.

5

Test generated code for strict mode compliance

Use Cursor Chat to audit your codebase for remaining strict mode issues. This catches violations in both AI-generated and manually written code.

Cmd+L prompt
1@strict-mode.mdc @codebase
2
3Search the codebase for JavaScript strict mode violations:
41. Variables used without declaration
52. 'with' statement usage
63. Duplicate function parameter names
74. Octal numeric literals without 0o prefix
85. eval() calls that create variables
96. arguments.callee or arguments.caller usage
107. Files missing 'use strict' that are not ES modules
11
12For each violation, show the file, line, and the fix.

Expected result: Cursor identifies remaining strict mode violations with specific fixes for each.

Complete working example

.cursor/rules/strict-mode.mdc
1---
2description: Enforce JavaScript strict mode compliance
3globs: "*.js,*.jsx,*.mjs"
4alwaysApply: true
5---
6
7# Strict Mode Compliance Rules
8
9## NEVER generate:
10- Undeclared variables (always use const or let)
11- The 'with' statement
12- Duplicate parameter names
13- Octal literals without 0o prefix
14- arguments.callee or arguments.caller
15- eval() to create variables in enclosing scope
16- Assignment to read-only globals (undefined, NaN, Infinity)
17- delete on plain variable names
18
19## ALWAYS:
20- Use 'use strict' at file top for .js files
21- Use const for values that never change
22- Use let only when reassignment is needed
23- Use named functions instead of arguments.callee
24- Use 0o prefix for octal numbers (0o755 not 0755)
25- Use ES module syntax (import/export) which implies strict mode
26
27## Correct:
28```javascript
29'use strict';
30const fileMode = 0o755;
31const items = [1, 2, 3];
32const sum = items.reduce((acc, val) => acc + val, 0);
33```
34
35## Wrong:
36```javascript
37fileMode = 0755; // undeclared var + old octal
38with (obj) { x = 1; } // with statement
39function f(a, a) {} // duplicate params
40```

Common mistakes

Why it's a problem: Assuming TypeScript files are immune to strict mode issues

How to avoid: Enable alwaysStrict in tsconfig.json and add ESLint rules for eval and arguments usage.

Why it's a problem: Not adding 'use strict' to CommonJS files

How to avoid: Add 'use strict' as the first line of all CommonJS files, or migrate to ES modules which enable strict mode automatically.

Why it's a problem: Using eval for dynamic code execution

How to avoid: Add NEVER use eval to your rules. Use JSON.parse for JSON, computed property access for dynamic properties, and new Function() only as a last resort.

Best practices

  • Enable TypeScript strict mode in tsconfig.json for compile-time checking
  • Add ESLint strict mode rules for runtime JavaScript files
  • Use ES modules (import/export) which enable strict mode automatically
  • Add 'use strict' to any remaining CommonJS files
  • Audit generated code with @codebase for strict mode violations
  • Keep Cursor rules and ESLint rules aligned for consistent enforcement
  • Test generated code in strict mode environments before deploying

Still stuck?

Copy one of these prompts to get a personalized, step-by-step explanation.

ChatGPT Prompt

Review this JavaScript file for strict mode violations. Check for undeclared variables, with statements, duplicate parameters, octal literals, and eval usage. Show the fixed version with proper const/let declarations and ES module syntax.

Cursor Prompt

@strict-mode.mdc Fix all strict mode violations in this file. Replace undeclared variables with const/let, remove with statements, fix octal literals to use 0o prefix, and ensure the file starts with 'use strict' or uses ES module syntax.

Frequently asked questions

Are ES modules always in strict mode?

Yes. Any file using import/export syntax is automatically in strict mode, regardless of whether 'use strict' is present. This is the simplest way to ensure strict mode compliance.

Does strict mode affect performance?

Strict mode can actually improve performance because the engine can make certain optimizations when it knows variables are properly declared and eval does not create new variables.

What about Node.js strict mode?

Node.js follows the same rules. ES modules (.mjs or type: module in package.json) are strict by default. CommonJS files need explicit 'use strict' at the top.

Will Cursor Tab completion violate strict mode?

Tab completion does not read project rules, so it may suggest non-strict patterns. ESLint catches these immediately, and you can reject them with Esc.

How do I migrate a legacy codebase to strict mode?

Use Cursor with @folder to process files one at a time. Prompt: Fix all strict mode violations in this file. Test after each file to ensure behavior is preserved.

Can RapidDev help modernize our JavaScript codebase?

Yes. RapidDev handles JavaScript modernization including strict mode migration, ES module conversion, and TypeScript adoption, with Cursor rules configured for ongoing compliance.

RapidDev

Talk to an Expert

Our team has built 600+ apps. Get personalized help with your project.

Book a free consultation

Need help with your project?

Our experts have built 600+ apps and can accelerate your development. Book a free consultation — no strings attached.

Book a free consultation

We put the rapid in RapidDev

Need a dedicated strategic tech and growth partner? Discover what RapidDev can do for your business! Book a call with our team to schedule a free, no-obligation consultation. We'll discuss your project and provide a custom quote at no cost.