# no-hardcoded-passwords

Rule `sonarjs/no-hardcoded-passwords`. Credentials should not be hard-coded

- Source: sonarjs
- Technologies: JavaScript
- Kind: correctness
- Needs type information: no
- Autofix: no
- Reach: 6.2M weekly downloads of presets that enable it
- Trend: +62.1% last quarter
- Rank: #1277 by reach, #578 by growth

## Linters

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

## Code examples

### Correct: the rule accepts this

```js
var mysql = require('mysql');

var connection = mysql.createConnection({
  host: process.env.MYSQL_URL,
  user: process.env.MYSQL_USERNAME,
  password: process.env.MYSQL_PASSWORD,
  database: process.env.MYSQL_DATABASE
});
connection.connect();
```

Examples from the ESLint documentation.

## Enable it

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

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

## Presets that enable it

| Preset | Linter | Package | Rules | Weekly downloads | Trend |
| --- | --- | --- | --- | --- | --- |
| [SonarJS recommended](https://lintradar.dev/presets/sonarjs-recommended.md) | ESLint | `eslint-plugin-sonarjs` | 230 | 6.2M | +62.1% |

## Related rules

- [sonarjs/no-hardcoded-ip](https://lintradar.dev/rules/sonarjs/no-hardcoded-ip.md): IP addresses should not be hardcoded
- [sonarjs/no-hardcoded-secrets](https://lintradar.dev/rules/sonarjs/no-hardcoded-secrets.md): Secrets should not be hard-coded
- [sonarjs/no-gratuitous-expressions](https://lintradar.dev/rules/sonarjs/no-gratuitous-expressions.md): Boolean expressions should not be gratuitous
- [sonarjs/no-hook-setter-in-body](https://lintradar.dev/rules/sonarjs/no-hook-setter-in-body.md): React's useState hook should not be used directly in the render function or body of a component
- [sonarjs/no-globals-shadowing](https://lintradar.dev/rules/sonarjs/no-globals-shadowing.md): Special identifiers should not be bound or assigned
- [sonarjs/no-identical-conditions](https://lintradar.dev/rules/sonarjs/no-identical-conditions.md): "if/else if" chains and "switch" cases should not have the same condition

---

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