Hello Trailblazers,
In this post, we're going to learn how to setup prettier for apex in VSCode. We're going to use a plugin named as prettier-plugin-apex. Once you've created a salesforce project, the first step is to install Prettier - Code formatter extension.
Click on the Extensions Tab in the left sidebar in your VSCode and search for prettier as shown below:
The first option that you'll see is for Prettier - Code Formatter open that and click on the Install button.
Once you have installed prettier, now it's time to install prettier-plugin-apex. You can install the plugin in your project only by running the command given below:
npm install --save-dev --save-exact prettier prettier-plugin-apex
The output of the above command is given below:
Please note that this will install prettier-plugin-apex locally in your project. If you want to install it globally so that you can use it in all the projects, you can use the below command:
npm install --global prettier prettier-plugin-apex
If you've installed it locally, it'll the package.json file in your salesforce project and you'll find the entry for prettier under scripts as shown below:
So, if you want to run prettier for your project, you can run it by executing the below command:npm run prettier
I tried this command in my project, so you can see the sample output below:
Your articles published are very useful, always provide in-depth details
ReplyDeleteHi @Rahul, thanks for your inputs. But when im trying to add the Prettier to the vscode and try to run the npm commands on the terminal it says that no command npm. But i see the prettier is configured in the package.json file. Can you help me how to add prettier. Do i miss any thing? i have installed (salesforce extension pack (Expanded) from the extenstion.
ReplyDeleteThanks in advance,
Sagar
Hi Sagar, looks like you don't have nodejs installed. Can you install it and try again?
Delete