skip to Main Content

I’m using VSCode for a Node.js project, and I have set up my editor to automatically sort JSON files on save, using the following setting in my settings.json:

"editor.codeActionsOnSave": {
    "source.sort.json": "always"
}

I want to exclude package.json from being automatically sorted when I save it, while keeping the auto-sort feature for other JSON files in my project. Is there a way to specifically exclude package.json from the editor.codeActionsOnSave rule, or any other workaround that would allow me to achieve this behavior?

I looked into VSCode documentation and tried searching for similar issues but couldn’t find a direct solution. Any suggestions or alternative approaches are greatly appreciated.

2

Answers


  1. Chosen as BEST ANSWER

    OK, I've found a halfway decent way to integrate without hacking.

    1. First remove the general setting "auto-sort" from your settings.json to ensure that it does not apply globally to all files. Here's what you should remove:
    "editor.codeActionsOnSave": {
        "source.sort.json": "always"
    }
    
    1. Next, reapply the setting specifically for JSON files (set Prettier as the default formatter):
    "[json]": {
      "editor.codeActionsOnSave": {
        "source.sort.json": "always"
      },
      "editor.defaultFormatter": "esbenp.prettier-vscode"
    },
    
    1. Classify package.json as jsonc (JSON with comments), which is not targeted by the auto-sort setting. Add this to your settings.json:
    "files.associations": {
      "package.json": "jsonc"
    }
    
    1. Finally, set the default formatter for JSONC files, and deactivate auto-sort:
    "[jsonc]": {
      "editor.codeActionsOnSave": {
        "source.sort.json": "never"
      },
      "editor.defaultFormatter": "esbenp.prettier-vscode"
    },
    

    Until a more convenient solution is implemented by the VSCode developers, you can use this solution here.

    Every JSON will be sorted and every JSONC not. You can extend this list below:

    "files.associations": {
      "package.json": "jsonc", 
      "settings.json": "json"
    }
    

    With this setup, package.json remains untouched by the auto-sort feature, while files like settings.json continue to be sorted. This distinction hinges on the file extension — json for sorting, jsonc for no sorting.

    Be aware of the last letter c

    Another Example

      "files.associations": {
        "de.json": "json",  // sort
        "en.json": "json",  // sort
        "fr.json": "json",  // sort
        "nx.json": "jsonc", // do not sort
        "package.json": "jsonc", // do not sort
        "project.json": "jsonc", // do not sort
        "settings.json": "json", // sort
        "tsconfig.app.json": "jsonc", // do not sort
        "tsconfig.base.json": "jsonc", // do not sort
        "tsconfig.e2e.json": "jsonc", // do not sort
        "tsconfig.editor.json": "jsonc", // do not sort
        "tsconfig.json": "jsonc", // do not sort
        "tsconfig.lib.json": "jsonc", // do not sort
        "tsconfig.spec.json": "jsonc" // do not sort
      },
    

  2. To prevent a file from being formatted on save, you can use the corresponding shortcuts:

    • Linux, Windows: Press Ctrl + K simultaneously, and then S
    • Mac: Press ⌘ Command + K simultaneously, and then S

    enter image description here

    You can check your keybindings in Settings > Keyboard Shortcuts.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search