How to tell eslint that you prefer single quotes around your strings

node.jsEslint

node.js Problem Overview


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.

node.js Solutions


Solution 1 - node.js

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

{
  "env": {
    "node": 1
  },
  "rules": {
    "quotes": [2, "single", { "avoidEscape": true }]
  }
}

Solution 2 - node.js

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

Solution 3 - node.js

rules: {
  'prettier/prettier': [
    'warn',
    {
      singleQuote: true,
      semi: true,
    }
  ],
},

In version "eslint": "^7.21.0"

Solution 4 - node.js

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.

Solution 5 - node.js

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 }]
  }
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAntonius BlochView Question on Stackoverflow
Solution 1 - node.jsAntonius BlochView Answer on Stackoverflow
Solution 2 - node.jsmtpultzView Answer on Stackoverflow
Solution 3 - node.jsMo.View Answer on Stackoverflow
Solution 4 - node.jsAllan MwesigwaView Answer on Stackoverflow
Solution 5 - node.jsAnomalyView Answer on Stackoverflow