Search code examples
bitbucketbitbucket-pipelinesprettier

How to use Prettier on Bitbucket pipeline?


I am trying to use code formatter, Prettier, on a Bitbucket pipeline but all I get is a "prettier command not found".

This is my pipeline configuration:

mage: node:latest
pipelines:
  branches:
    master:
      - step:
          caches:
          - node
          script:
          - npm install prettier
          - prettier --check

Is it even possible to run it through pipelines or would have to forcefully do it locally?


Solution

  • I will suggest you follow the documentation https://prettier.io/docs/en/install.html

    • install prettier in your repo

      npm install --save-dev --save-exact prettier

    "--save-dev" because the production will not need prettier to serve your application

    • add your rules in .prettierrc.json file in the root of the repo

    • in the pipeline use

      npx prettier --check .

    What is that npx thing? npx ships with npm and lets you run locally installed tools. We’ll leave off the npx part for brevity throughout the rest of this file! Note: If you forget to install Prettier first, npx will temporarily download the latest version. That’s not a good idea when using Prettier, because we change how code is formatted in each release! It’s important to have a locked down version of Prettier in your package.json. And it’s faster, too.

    When you have installed prettier in your repo, you can even add pre-commit hooks to modify the staged files before pushing.