87

I'm new to eslint and it's spewing out a ton of errors telling me to use doublequotes:

error  Strings must use doublequote

That's not my preference. I've got an .eslintrc file set up with the basics:

{
  "env": {
    "node": 1
  }
}

I'd like to configure it for single quotes.

Antonius Bloch
  • 2,311
  • 2
  • 14
  • 14

5 Answers5

121

http://eslint.org/docs/rules/quotes.html

{
  "env": {
    "node": 1
  },
  "rules": {
    "quotes": [2, "single", { "avoidEscape": true }]
  }
}
Knu
  • 14,806
  • 5
  • 56
  • 89
Antonius Bloch
  • 2,311
  • 2
  • 14
  • 14
  • 16
    If I need to use either of single or double quotes, what to change in above rule ? – vikramvi Mar 07 '19 at 09:36
  • 3
    @Antonius Bloch, this solution doesn't work in my Vue.js project... `"eslintConfig": { "root": true, "env": { "node": true }, "extends": [ "plugin:vue/essential", "@vue/prettier" ], "rules": { "no-console": "off", "quotes": [1, "single", { "avoidEscape": true }] },` – Riko3412 Dec 28 '19 at 13:00
  • 2
    What is the `2` for? I can't see any explanation in that link. – ryan2johnson9 Dec 07 '21 at 03:07
  • 4
    @ryan2johnson9 It is a rule severity level: 0 = off, 1 = warn, 2 = error – Dmitry Barskov Jan 12 '22 at 20:55
50

If you're using TypeScript/ES6 you might want to include template literals (backticks). This rule prefers single quotes, and allows template literals.

TypeScript Examples

"@typescript-eslint/quotes": [
  "error",
  "single",
  {
    "allowTemplateLiterals": true
  }
]

Another useful option is to allow single or double quotes as long as the string contains an escapable quote like "lorem ipsum 'donor' eta" or 'lorem ipsum "donor" eta'.

"@typescript-eslint/quotes": [
  "error",
  "single",
  {
    "avoidEscape": true,
    "allowTemplateLiterals": true
  }
]

References:

ESLint

https://eslint.org/docs/rules/quotes

TypeScript ESLint

https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/quotes.md

mtpultz
  • 17,267
  • 22
  • 122
  • 201
  • 2
    I don't think you need to set `allowTemplateLiterals` to `true` unless you want to allow using template literal strings even when only single quotes would work. Template literals are allowed by default. This can easily be tested. – oyalhi Sep 17 '21 at 17:06
  • both examples in the post are exactly the same showing example for `allowTemplateLiterals`, very confusing. Downvoting until fixed. – oyalhi Sep 17 '21 at 17:07
  • 1
    @oyalhi yep somehow missed `avoidEscape`. Thanks – mtpultz Sep 18 '21 at 19:06
25
rules: {
  'prettier/prettier': [
    'warn',
    {
      singleQuote: true,
      semi: true,
    }
  ],
},

In version "eslint": "^7.21.0"

Mo.
  • 26,306
  • 36
  • 159
  • 225
5

For jsx strings, if you would like to set this rule for all files, create the rule in the eslint config file.

  rules: {
    'jsx-quotes': [2, 'prefer-single'],
  }

Or 'prefer-double' for double quotes.

Allan Mwesigwa
  • 1,210
  • 14
  • 13
4

If you also want to allow double quotes if they would avoid escaping a single quote inside the string, and allow template literals (the reasonable defaults, imo) try the following:

{
  "env": {
    "node": 1
  },
  "rules": {
    "quotes": [2, "single", { "avoidEscape": true, "allowTemplateLiterals": true }]
  }
}
Anomaly
  • 932
  • 1
  • 10
  • 18