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
Create a strict mode compliance rule
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.
1---2description: Enforce JavaScript strict mode compliance3globs: "*.js,*.jsx,*.mjs"4alwaysApply: true5---67# Strict Mode Compliance8- NEVER use undeclared variables (always use const, let, or var)9- NEVER use the 'with' statement10- NEVER use duplicate parameter names in functions11- NEVER use octal numeric literals (0644) — use 0o644 instead12- NEVER assign to read-only properties or undeletable properties13- NEVER use 'arguments.callee' or 'arguments.caller'14- NEVER use eval() to create variables in surrounding scope15- ALWAYS declare variables before use16- ALWAYS use 'use strict' at the top of .js files (or use ES modules)1718## TypeScript files are strict by default if tsconfig has strict: trueExpected result: Cursor generates JavaScript that complies with strict mode restrictions.
Add ESLint strict mode rules
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.
1// eslint.config.js2import js from '@eslint/js';34export 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.
Fix existing violations with Cmd+K
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.
1Fix all strict mode violations in this code:2- Add const/let declarations for any undeclared variables3- Replace 'with' statements with explicit property access4- Replace octal literals (0644) with ES6 octal syntax (0o644)5- Remove duplicate function parameter names6- Replace arguments.callee with named function references7- Ensure 'use strict' is present at file top if not using ES modulesExpected result: All strict mode violations are fixed while maintaining the original code behavior.
Verify with TypeScript strict configuration
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.
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": true14 }15}Expected result: TypeScript compiler catches strict mode violations at build time, complementing Cursor rules and ESLint.
Test generated code for strict mode compliance
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.
1@strict-mode.mdc @codebase23Search the codebase for JavaScript strict mode violations:41. Variables used without declaration52. 'with' statement usage63. Duplicate function parameter names74. Octal numeric literals without 0o prefix85. eval() calls that create variables96. arguments.callee or arguments.caller usage107. Files missing 'use strict' that are not ES modules1112For 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
1---2description: Enforce JavaScript strict mode compliance3globs: "*.js,*.jsx,*.mjs"4alwaysApply: true5---67# Strict Mode Compliance Rules89## NEVER generate:10- Undeclared variables (always use const or let)11- The 'with' statement12- Duplicate parameter names13- Octal literals without 0o prefix14- arguments.callee or arguments.caller15- eval() to create variables in enclosing scope16- Assignment to read-only globals (undefined, NaN, Infinity)17- delete on plain variable names1819## ALWAYS:20- Use 'use strict' at file top for .js files21- Use const for values that never change22- Use let only when reassignment is needed23- Use named functions instead of arguments.callee24- Use 0o prefix for octal numbers (0o755 not 0755)25- Use ES module syntax (import/export) which implies strict mode2627## Correct:28```javascript29'use strict';30const fileMode = 0o755;31const items = [1, 2, 3];32const sum = items.reduce((acc, val) => acc + val, 0);33```3435## Wrong:36```javascript37fileMode = 0755; // undeclared var + old octal38with (obj) { x = 1; } // with statement39function f(a, a) {} // duplicate params40```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.
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.
@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.
Talk to an Expert
Our team has built 600+ apps. Get personalized help with your project.
Book a free consultation