Skip to content

Can we use Prettier (or similar formatter) for Javascript code? #16115

Description

@trivikr

Can we use Prettier for formatting Javascript code in NodeJS?
Prettier is an opinionated code formatter. It removes all original styling and ensures that all outputted code conforms to a consistent style.

Reasons:

  • Right now there are 1600 contributors in NodeJS repository (thanks to its huge popularity), but every Javascript developer has their own style of formatting.
  • Some code is difficult to read because of manual formatting. Prettier will ensure that such issue will not occur.

We recently started using Prettier for private projects, and life has been better. We don't worry/argue about formatting any more :-)
Of course there was initial oppositions to some of the decisions taken by Prettier (like lack of parenthesis around single param arrow functions), but we got used to it with time.
For Open Source projects which don't use Prettier, I manually run Prettier CLI for the files which I'm editing. I believe developers will be more productive if we use formatters like Prettier in NodeJS. What do you think?

Activity

  1. added
    metaIssues and PRs related to the general management of the project.
    questionIssues asking questions about Node.js.
    on Oct 10, 2017
  2. mscdex commented on Oct 10, 2017

    @mscdex
    Contributor

    I would guess as long as the eslint config is happy, then sure, go ahead.

  3. trivikr commented on Oct 10, 2017

    @trivikr
    MemberAuthor

    Prettier is going to remove all original styling in code and reprint it from scratch. So it'll touch all the Javascript files and create merge conflicts for all pull requests.
    It's going to be a big move - we'll have to find out how other big projects like react, jest, babeljs have done it.

    I wanted to hear about what other contributors/owners think about it.
    In my private repos, moving to Prettier was simple as we incorporated it during the beginning of the new projects.

  4. trivikr commented on Oct 10, 2017

    @trivikr
    MemberAuthor

    @mscdex I've used Prettier CLI on the code which I've contributed to NodeJS.
    The only difference I noticed is node eslint config requires us to put parenthesis around single parameter arrow functions - where Prettier doesn't provide an option

  5. BridgeAR commented on Oct 10, 2017

    @BridgeAR
    Member

    Eslint itself is a very powerful prettier and formatter. I would rather not add another tool. If there is need for more explicit styling there is probably already a rule in eslint that could be added. Adding one rule at a time also reduces the churn and it will be easier to find a compromise between all collaborators and their individual wishes.

  6. trivikr commented on Oct 10, 2017

    @trivikr
    MemberAuthor

    Prettier just takes care of stylistic issues.
    We still need to use eslint for enforcing good coding practices and avoiding bugs.

    The move to Prettier will be tough in the beginning, but with time we'll see the benefits as we no longer worry about formatting and concentrate on code.

  7. bnoordhuis commented on Oct 10, 2017

    @bnoordhuis
    Member

    We only switched from closure_linter to eslint two years ago!

    Like @BridgeAR mentions, eslint is already pretty good at fixing style issues.

  8. mscdex commented on Oct 10, 2017

    @mscdex
    Contributor

    Maybe I misunderstood, I thought the suggestion was if individual contributors could use it on their own. Adopting it officially project-wide is probably not a good idea as Ben and Ruben have said.

  9. trivikr commented on Oct 10, 2017

    @trivikr
    MemberAuthor

    Yeah, individual developers are free to use any formatter they want as long as it doesn't interfere with eslint rules. I use Prettier CLI, and add parenthesis for single param arrow functions before running make lint in my code.

  10. Trott commented on Oct 15, 2017

    @Trott
    Member

    Seems like this can be closed. If I'm mistaken about that, please comment. Thanks!

  11. gengjiawen commented on Nov 18, 2018

    @gengjiawen
    Member

    Prettier is different from eslint, it bring the consistent style for project like semicolons and quotes.

  12. gengjiawen commented on Nov 18, 2018

    @gengjiawen
    Member

    I did a little dig

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    metaIssues and PRs related to the general management of the project.questionIssues asking questions about Node.js.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions