Forcetrails: Apex code formatting don't work after setting new Salesforce project in VS Code

All the configurations needed for Apex, LWC, Aura, and VF code formatting to work in Visual Studio code are automatically set up when you create a new Salesforce project with VS Code. This new project can be with or without the package.xml file. But still, the code formatting doesn't work properly for Salesforce code, majorly Apex code.


Problem

The problem is that when the project is set up all the node Node modules used by VS Code are not installed in the project folder by default. That is why the formatting does not work properly.


Solution For Newly Setup Projects

Whenever you set up a new Salesforce project you need to do these steps.
  1. Check if your project has package.json file.
  2. If the doesn't exist in your project folder then run the command on terminal npm init. If your VS code setup is correct then this file is generated at the time of project creation. If the file is already there in your project, no need to run this command, you can skip this step.
  3. Now open a new terminal inside the vs code and run this command: npm install. This command installs all the dependent node packages required by the project based on the package.json file.
  4. If you run into any errors from the above command try running npm audit fix.
  5. And you are done.

Solution For Existing Projects

If you have an existing project set up and code formatting is not working properly then follow the below steps.

  1. Make sure that you are able to see hidden files on your PC/Mac.
  2. Go to your project folder and look for the node_modules folder and delete it.
  3. Now open a new terminal inside the vs code and run this command: npm install. This command installs all the dependent node packages required by the project based on the package.json file.
  4. If you run into any errors from the above command try running npm audit fix.
  5. And you are done.
If you still face any issues check if you have Node installed on your machine.
Hope this helps! Let me know in the comments if any other issues.

Check out my post: Enable code formatting for apex in vs code if you want to customize the formatting style further.

No comments :
Post a Comment

Hi there, comments on this site are moderated, you might need to wait until your comment is published. Spam and promotions will be deleted. Sorry for the inconvenience but we have moderated the comments for the safety of this website users. If you have any concern, or if you are not able to comment for some reason, email us at rahul@forcetrails.com