Convert Figma logo to code with AI

prettier logoprettier

Prettier is an opinionated code formatter.

49,046
4,282
49,046
1,386

Top Related Projects

24,850

Find and fix problems in your JavaScript code.

8,975

JSHint is a tool that helps to detect errors and potential problems in your JavaScript code

29,051

🌟 JavaScript Style Guide, with linter & automatic code fixer

10,976

A mighty CSS linter that helps you avoid errors and enforce conventions.

23,768

Unified developer tools for JavaScript, TypeScript, and the web

5,909

:vertical_traffic_light: An extensible linter for the TypeScript language

Quick Overview

Prettier is an opinionated code formatter that supports multiple programming languages. It automatically formats code to ensure consistent style, removing the need for manual formatting and reducing debates about code style within development teams.

Pros

  • Supports a wide range of programming languages and file formats
  • Integrates easily with most popular code editors and IDEs
  • Improves code readability and maintainability
  • Saves time by automating code formatting

Cons

  • Opinionated nature may not suit all coding styles or preferences
  • Limited customization options compared to some other formatters
  • Can occasionally produce unexpected formatting results
  • May require initial setup and configuration time

Code Examples

  1. Formatting JavaScript code:
// Before formatting
function greet(name) {
    if(name){
        return `Hello, ${name}!`;
    } else {
        return "Hello, stranger!";
    }
}

// After formatting with Prettier
function greet(name) {
  if (name) {
    return `Hello, ${name}!`;
  } else {
    return "Hello, stranger!";
  }
}
  1. Formatting CSS code:
/* Before formatting */
.container{display: flex; flex-direction:column;
    align-items:center;}
.container > * {margin-bottom: 1rem;}

/* After formatting with Prettier */
.container {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.container > * {
  margin-bottom: 1rem;
}
  1. Formatting JSON:
// Before formatting
{"name":"John Doe","age":30,"city":"New York","hobbies":["reading","swimming","coding"]}

// After formatting with Prettier
{
  "name": "John Doe",
  "age": 30,
  "city": "New York",
  "hobbies": ["reading", "swimming", "coding"]
}

Getting Started

To use Prettier in your project:

  1. Install Prettier:
npm install --save-dev prettier
  1. Create a configuration file (.prettierrc) in your project root:
{
  "semi": true,
  "singleQuote": true,
  "tabWidth": 2,
  "trailingComma": "es5"
}
  1. Add a script to your package.json:
"scripts": {
  "format": "prettier --write \"**/*.{js,jsx,ts,tsx,json,css,scss,md}\""
}
  1. Run Prettier:
npm run format

Competitor Comparisons

24,850

Find and fix problems in your JavaScript code.

Pros of ESLint

  • More customizable and configurable, allowing for fine-tuned rule sets
  • Capable of detecting logical errors and potential bugs, not just style issues
  • Supports plugins for extended functionality and language-specific rules

Cons of ESLint

  • Requires more setup and configuration to get started
  • Can be slower to run, especially on large codebases
  • May lead to debates within teams about specific rule choices

Code Comparison

ESLint (with custom rules):

// eslint-disable-next-line no-unused-vars
function example(param1, param2) {
  const result = param1 + param2;
  return result;
}

Prettier (default formatting):

function example(param1, param2) {
  const result = param1 + param2;
  return result;
}

ESLint focuses on identifying potential issues and enforcing coding standards, while Prettier is primarily concerned with consistent code formatting. ESLint's output may include warnings about unused variables or suggest more idiomatic code structures, whereas Prettier would simply format the code according to its predefined style rules.

Both tools serve different purposes and can be used together in a development workflow. ESLint excels at catching potential errors and enforcing best practices, while Prettier ensures consistent code formatting across a project.

8,975

JSHint is a tool that helps to detect errors and potential problems in your JavaScript code

Pros of JSHint

  • More flexible and customizable linting rules
  • Focuses on detecting potential errors and problematic patterns
  • Integrates well with various build tools and IDEs

Cons of JSHint

  • Doesn't automatically fix code issues
  • Limited formatting capabilities
  • Less active development and community support

Code Comparison

JSHint (linting):

function example(x) {
    if (x == null) return;
    var y = x + 1;
    console.log(y);
}

Prettier (formatting):

function example(x) {
  if (x == null) return;
  var y = x + 1;
  console.log(y);
}

JSHint focuses on identifying potential issues in the code, such as using == instead of === for null checks. Prettier, on the other hand, automatically formats the code to a consistent style, adjusting indentation and line breaks.

While JSHint provides valuable insights into code quality and potential bugs, Prettier excels at maintaining consistent code formatting across projects. JSHint is better suited for catching logical errors and enforcing coding standards, whereas Prettier is ideal for eliminating debates about code style and ensuring uniform formatting.

Many developers use both tools in conjunction, with JSHint for linting and Prettier for formatting, to achieve both code quality and consistent style in their projects.

29,051

🌟 JavaScript Style Guide, with linter & automatic code fixer

Pros of Standard

  • Simpler setup with zero configuration required
  • Includes both linting and formatting in a single package
  • Opinionated style guide that promotes consistency across projects

Cons of Standard

  • Less flexible and customizable than Prettier
  • Limited language support compared to Prettier's wide range
  • Smaller community and ecosystem

Code Comparison

Standard:

function example (foo) {
  if (foo) {
    return foo
  }
  return bar
}

Prettier:

function example(foo) {
  if (foo) {
    return foo;
  }
  return bar;
}

Standard enforces no semicolons and space before function parentheses, while Prettier adds semicolons and removes the space before function parentheses by default.

Summary

Standard offers a simpler, opinionated approach with built-in linting, making it ideal for teams wanting a quick, consistent setup. Prettier provides more flexibility, broader language support, and a larger ecosystem, but requires additional configuration for linting. The choice between them depends on project requirements and team preferences.

10,976

A mighty CSS linter that helps you avoid errors and enforce conventions.

Pros of stylelint

  • Highly customizable with extensive rule set for CSS and preprocessors
  • Supports plugins for additional functionality and custom rules
  • Provides detailed error messages and suggestions for fixes

Cons of stylelint

  • Requires more configuration and setup compared to Prettier
  • Can be slower to run, especially on large codebases
  • May require more maintenance as CSS specifications evolve

Code Comparison

stylelint configuration example:

{
  "rules": {
    "color-hex-case": "lower",
    "indentation": 2,
    "selector-max-id": 0
  }
}

Prettier configuration example:

{
  "printWidth": 80,
  "tabWidth": 2,
  "singleQuote": true
}

Key Differences

  • stylelint focuses specifically on CSS and related languages, while Prettier is a more general-purpose formatter
  • stylelint offers more granular control over styling rules, whereas Prettier aims for consistent formatting with fewer options
  • stylelint can catch potential errors and enforce best practices, while Prettier primarily focuses on code formatting

Use Cases

  • Use stylelint when you need detailed control over CSS styling rules and want to enforce specific coding standards
  • Choose Prettier for quick and consistent formatting across multiple languages with minimal configuration

Both tools can be used together in a project, with stylelint handling CSS-specific linting and Prettier managing overall code formatting.

23,768

Unified developer tools for JavaScript, TypeScript, and the web

Pros of Rome

  • All-in-one toolchain: Rome combines linting, formatting, bundling, and more
  • Faster performance due to being built in Rust
  • Designed for extensibility and customization

Cons of Rome

  • Less mature and stable compared to Prettier
  • Smaller community and ecosystem
  • Limited language support (primarily focused on JavaScript/TypeScript)

Code Comparison

Rome:

function greet(name) {
  return `Hello, ${name}!`;
}

Prettier:

function greet(name) {
  return `Hello, ${name}!`;
}

In this simple example, both Rome and Prettier produce identical output. However, Rome's formatting rules can be more opinionated and may differ in more complex scenarios.

Key Differences

  • Scope: Rome aims to be a comprehensive toolchain, while Prettier focuses solely on code formatting
  • Language support: Prettier supports a wider range of languages
  • Configuration: Rome offers more granular control over formatting rules
  • Performance: Rome generally performs faster due to its Rust implementation
  • Community: Prettier has a larger user base and more widespread adoption

Conclusion

While Rome offers an ambitious all-in-one solution with potential performance benefits, Prettier remains the more established and widely-supported option for code formatting. The choice between the two depends on project requirements, language needs, and desired toolchain integration.

5,909

:vertical_traffic_light: An extensible linter for the TypeScript language

Pros of TSLint

  • More granular control over linting rules and configurations
  • Specifically designed for TypeScript, offering deeper language-specific checks
  • Supports custom rule creation for project-specific needs

Cons of TSLint

  • Slower performance, especially on larger codebases
  • Discontinued and no longer actively maintained
  • Steeper learning curve due to more complex configuration options

Code Comparison

TSLint configuration example:

{
  "rules": {
    "no-unused-variable": true,
    "semicolon": [true, "always"],
    "quotemark": [true, "single"]
  }
}

Prettier configuration example:

{
  "semi": true,
  "singleQuote": true,
  "trailingComma": "es5"
}

TSLint focuses on linting rules and code quality, while Prettier emphasizes consistent code formatting. TSLint offers more fine-grained control but requires more setup, whereas Prettier provides a simpler, opinionated approach to code style.

TSLint has been deprecated in favor of ESLint with TypeScript support, while Prettier continues to be actively maintained and widely adopted for its simplicity and integration with various tools and editors.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Prettier Banner

Opinionated Code Formatter

JavaScript · TypeScript · Flow · JSX · JSON
CSS · SCSS · Less
HTML · Vue · Angular
GraphQL · Markdown · YAML
Your favorite language?

Github Actions Build Status Github Actions Build Status Github Actions Build Status Codecov Coverage Status Blazing Fast
npm version weekly downloads from npm code style: prettier Follow Prettier on Twitter

Intro

Prettier is an opinionated code formatter. It enforces a consistent style by parsing your code and re-printing it with its own rules that take the maximum line length into account, wrapping code when necessary.

Input

foo(reallyLongArg(), omgSoManyParameters(), IShouldRefactorThis(), isThereSeriouslyAnotherOne());

Output

foo(
  reallyLongArg(),
  omgSoManyParameters(),
  IShouldRefactorThis(),
  isThereSeriouslyAnotherOne(),
);

Prettier can be run in your editor on-save, in a pre-commit hook, or in CI environments to ensure your codebase has a consistent style without devs ever having to post a nit-picky comment on a code review ever again!


Documentation

Install · Options · CLI · API

Playground


Badge

Show the world you're using Prettier → code style: prettier

[![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier)

Contributing

See CONTRIBUTING.md.

NPM DownloadsLast 30 Days