# no-reference-error

Rule `sonarjs/no-reference-error`. Variables should be defined before being used

- Source: sonarjs
- Technologies: JavaScript
- Kind: correctness
- Needs type information: no
- Autofix: no
- Reach: 0 weekly downloads of presets that enable it
- Trend: 0% last quarter
- Rank: #2160 by reach

## Linters

| Linter | Rule name | Recommended | Autofix | Details | Docs |
| --- | --- | --- | --- | --- | --- |
| ESLint | `sonarjs/no-reference-error` | no | no | package `eslint-plugin-sonarjs` | [docs](https://rules.sonarsource.com/javascript/RSPEC-3827/) |

## Code examples

### Incorrect: the rule reports this

```js
var john = {
  firstName: "john",
  show: function() { console.log(firstName); } // Noncompliant: firstName is not defined
}
john.show();
```

### Correct: the rule accepts this

```js
var john = {
  firstName: "john",
  show: function() { console.log(this.firstName); }
}
john.show();
```

Examples from the ESLint documentation.

## Enable it

### ESLint: `eslint.config.js`

```js
export default [
  {
    rules: {
      "sonarjs/no-reference-error": "error",
    },
  },
];
```

## Related rules

- [sonarjs/no-require-or-define](https://lintradar.dev/rules/sonarjs/no-require-or-define.md): "import" should be used to include external code
- [sonarjs/no-networkidle-wait](https://lintradar.dev/rules/sonarjs/no-networkidle-wait.md): Playwright "networkidle" waits should not be used
- [sonarjs/no-return-type-any](https://lintradar.dev/rules/sonarjs/no-return-type-any.md): Primitive return types should be used
- [sonarjs/no-nested-incdec](https://lintradar.dev/rules/sonarjs/no-nested-incdec.md): Increment (++) and decrement (--) operators should not be used in a method call or mixed with other operators in an expression
- [sonarjs/no-sonar-comments](https://lintradar.dev/rules/sonarjs/no-sonar-comments.md): Track uses of "NOSONAR" comments
- [sonarjs/no-mixed-completion-style](https://lintradar.dev/rules/sonarjs/no-mixed-completion-style.md): Test and hook callbacks should use a single completion style

---

Web page: https://lintradar.dev/rules/sonarjs/no-reference-error. Data from npm and the linters' documentation, collected Dec 4, 2026. Site guide for agents: https://lintradar.dev/llms.txt
