Skip to content

max-len ​

Very long lines of code in any language can be difficult to read. In order to aid in readability and maintainability many coders have developed a convention to limit lines of code to X number of characters (traditionally 80 characters).

js
var foo = { "bar": "This is a bar.", "baz": { "qux": "This is a qux" }, "difficult": "to read" }; // very long

Rule Details ​

This rule enforces a maximum line length to increase code readability and maintainability. The length of a line is defined as the number of Unicode characters in the line.

Options ​

This rule can have up to two numbers as positional arguments (for code and tabWidth options), followed by an object option (provided positional arguments have priority):

  • "code" (default 80) enforces a maximum line length
  • "tabWidth" (default 4) specifies the character width for tab characters
  • "comments" enforces a maximum line length for comments; defaults to value of code
  • "ignorePattern" ignores lines matching a pattern; can only match a single line, needs to be double escaped when written in YAML or JSON and must be a string that can be passed to the RegExp constructor.
  • "ignoreComments": true ignores all trailing comments and comments on their own line
  • "ignoreTrailingComments": true ignores only trailing comments
  • "ignoreUrls": true ignores lines that contain a URL
  • "ignoreStrings": true ignores lines that contain a double-quoted or single-quoted string
  • "ignoreTemplateLiterals": true ignores lines that contain a template literal
  • "ignoreRegExpLiterals": true ignores lines that contain a RegExp literal

code ​

Examples of incorrect code for this rule with the default { "code": 80 } option:

js
/* eslint @stylistic/max-len: ["error", { "code": 80 }] */

var foo = { "bar": "This is a bar.", "baz": { "qux": "This is a qux" }, "difficult": "to read" };
incorrect

Examples of correct code for this rule with the default { "code": 80 } option:

js
/* eslint @stylistic/max-len: ["error", { "code": 80 }] */

var foo = {
  "bar": "This is a bar.",
  "baz": { "qux": "This is a qux" },
  "easier": "to read"
};
correct

tabWidth ​

Examples of incorrect code for this rule with the default { "tabWidth": 4 } option:

js
/* eslint @stylistic/max-len: ["error", { "code": 80, "tabWidth": 4 }] */

var foo = { "bar": "This is a bar.", "baz": { "qux": "This is a qux" } };
incorrect

Examples of correct code for this rule with the default { "tabWidth": 4 } option:

js
/* eslint @stylistic/max-len: ["error", { "code": 80, "tabWidth": 4 }] */

		var foo = {
				"bar": "This is a bar.",
				"baz": { "qux": "This is a qux" }
		};
correct

comments ​

Examples of incorrect code for this rule with the { "comments": 65 } option:

js
/* eslint @stylistic/max-len: ["error", { "comments": 65 }] */

/**
* This is a comment that violates the maximum line length we have specified
**/
incorrect

ignoreComments ​

Examples of correct code for this rule with the { "ignoreComments": true } option:

js
/* eslint @stylistic/max-len: ["error", { "ignoreComments": true }] */

/**
 * This is a really really really really really really really really really long comment
 **/
correct

ignoreTrailingComments ​

Examples of correct code for this rule with the { "ignoreTrailingComments": true } option:

js
/* eslint @stylistic/max-len: ["error", { "ignoreTrailingComments": true }] */

var foo = 'bar'; // This is a really really really really really really really long comment
correct

ignoreUrls ​

Examples of correct code for this rule with the { "ignoreUrls": true } option:

js
/* eslint @stylistic/max-len: ["error", { "ignoreUrls": true }] */

var url = 'https://www.example.com/really/really/really/really/really/really/really/long';
correct

ignoreStrings ​

Examples of correct code for this rule with the { "ignoreStrings": true } option:

js
/* eslint @stylistic/max-len: ["error", { "ignoreStrings": true }] */

var longString = 'this is a really really really really really really really long string!';
correct

ignoreTemplateLiterals ​

Examples of correct code for this rule with the { "ignoreTemplateLiterals": true } option:

js
/* eslint @stylistic/max-len: ["error", { "ignoreTemplateLiterals": true }] */

var longTemplateLiteral = `this is a really really really really really long template literal!`;
correct

ignoreRegExpLiterals ​

Examples of correct code for this rule with the { "ignoreRegExpLiterals": true } option:

js
/* eslint @stylistic/max-len: ["error", { "ignoreRegExpLiterals": true }] */

var longRegExpLiteral = /this is a really really really really really long regular expression!/;
correct

ignorePattern ​

NOTE

This option must be a string that can be passed to the RegExp constructor.

Examples of correct code for this rule with the ignorePattern option:

js
/* eslint @stylistic/max-len: [
  "error",
  { "ignorePattern": "^\\s*var\\s.+=\\s*require\\s*\\(" }
] */

var dep = require('really/really/really/really/really/really/really/really/long/module');
correct

Autofix limitations ​

max-len reports lines that exceed the configured limit, but it does not wrap them automatically. Some autofixable layout rules can remove line breaks or increase indentation. As a result, running eslint --fix may leave or introduce max-len violations.

ESLint does not coordinate a rule's fixes with the configured limit of max-len. Therefore, the output of --fix is not guaranteed to satisfy this rule.

Ways to avoid this ​

  • Use a dedicated formatter such as Prettier, dprint, or oxfmt for layout, and disable overlapping autofixable layout rules. Formatters generally treat line width as a wrapping preference rather than a hard limit, so you may still need to relax or disable max-len.
  • If you keep ESLint layout fixers enabled, choose compatible rule options and a suitable max-len limit, then manually resolve any remaining violations.
  • Use options such as ignoreComments, ignoreStrings, ignoreUrls, or ignorePattern when specific kinds of long lines are acceptable.

Released under the MIT License.