How to make Visual Studio Code check entire project for errors?

JavascriptTypescriptVisual Studio-Code

Javascript Problem Overview


I'm using VS Code for TypeScript/JavaScript development. When I open a file it will check that file for errors. The problem is if I'm refactoring (like I move some shared code to a new location or change a name) it won't show me the errors this caused until I open the file with the problem. ...so if I want to do extensive refactoring I have to open every file just to make it scan the file for errors.

How can I make VS Code scan the whole project for errors without having to open each file one by one manually?

Javascript Solutions


Solution 1 - Javascript

VS Code (v1.44) has an experimental feature, that allows project wide error reporting in TS. Try it out:

// put this line in settings.json
"typescript.tsserver.experimental.enableProjectDiagnostics": true

Solution 2 - Javascript

Figured it out. Note this answer is specific to TypeScript, which is what I am using. Here it is:

Make sure typescript is installed globally (I just had mine installed locally apparently): npm install -g typescript

Then in VS Code press Shift+Ctrl+B. If you don't have a task runner set up it will ask what you want. I selected typescript and the tasks.json file will look like this:

{
    "version": "0.1.0",
    "command": "tsc",
    "isShellCommand": true,
    "args": ["-p", "."],
    "showOutput": "silent",
    "problemMatcher": "$tsc"
}

Then pressing Shift+Ctrl+B (or Shift+Command+B in macOS) will check the entire project for problems and they will be reported in your "problems" panel.

Solution 3 - Javascript

If you don't want to install TypeScript globally, you can do the following:

  1. Install TypeScript locally on the project, that is yarn add --dev typescript or npm install --save-dev typescript.

  2. Add a check-types run script to ./package.json. --noEmit means that the compiler will won't generate any JavaScript files.

{
  "scripts": {
    "check-types": "tsc --noEmit"
  }
}

3. Let VSCode know about the run script in /.vscode/tasks.json.

{
  "version": "2.0.0",
  "tasks": [
    {
      "type": "npm",
      "script": "check-types",
      "problemMatcher": [
        "$tsc"
      ]
    }
  ]
}

4. To run the tasks hit the F1 key and select 'Run Task', and then 'npm: check-types'.

  1. If you add the following lines to the task, pressing Ctrl+B will run it.

    "group": { "kind": "build", "isDefault": true }

Solution 4 - Javascript

For the most recent version of tasks.json this is the correct json, following deprecations in version 1.14. Create this as /.vscode/tasks.json

{
    "version": "2.0.0",
    "command": "tsc",
    "type": "shell",
    "args": [
        "-p",
        "."
    ],
    "presentation": {
        "reveal": "silent"
    },
    "problemMatcher": "$tsc"
}

Solution 5 - Javascript

Once you have open your project in vs code, open the vs code terminal and run:

node_modules/.bin/tsc --noEmit

Solution 6 - Javascript

UPDATE.
My answer below does not answer the original question, but if you're like me and have found this thread searching for how to turn on // @ts-check project-wide in VSCode so that you don't need to add // @ts-check to every file then my answer below is what you need. I have searched and searched and kept getting this thread as my top result so hopefully this helps others as well


I'm on vscode version 1.52.1 and the answer is so simple and right on the vscode website:

https://code.visualstudio.com/docs/nodejs/working-with-javascript#_type-checking-javascript

scroll down to the "Using jsconfig or tsconfig" section

Add a jsconfig.json in your project root and add "checkJs": true

{
  "compilerOptions": {
    "checkJs": true
  },
  "exclude": ["node_modules", "**/node_modules/*"]
}

You might need to restart vscode once you add it in

Solution 7 - Javascript

  1. Go to View menu > Extensions and make sure the Microsoft VS Code ESLint extension is installed.
  2. In Settings, search for "ESLint > Lint Task: Enable", and enable that setting (docs).
  3. In the Terminal menu, choose Run Task… > eslint: lint whole folder.

Solution 8 - Javascript

Edit: Since updating to 1.52.0 this no longer works. It will instead replace the current project files with what you selected...

===

I've tried every solution I can find and I think it's safe to say the answer is: You can't*

The best I've found is still technically opening each file manually, but at least it's not one-by-one:

  1. You can't drag/drop a directory but you can select multiple files (including directories) and drag/drop. The directories will be ignored and any files you had selected will be opened in tabs.
  2. Next, you need to give each tab focus so it triggers eslint to run. (Holding down the next tab keyboard shortcut works.)

This is terrible and I beg someone to prove me wrong.

*I haven't found a way that works as well as opening files manually. Each method -- experimental vscode feature and tsc task -- has its own set of drawbacks. The vscode feature is clearly the solution but without a way to ignore node_modules, etc. it's too painful to use.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionWillyCView Question on Stackoverflow
Solution 1 - Javascriptford04View Answer on Stackoverflow
Solution 2 - JavascriptWillyCView Answer on Stackoverflow
Solution 3 - JavascriptJan AagaardView Answer on Stackoverflow
Solution 4 - JavascriptmanoftheyearView Answer on Stackoverflow
Solution 5 - JavascriptAriElsaView Answer on Stackoverflow
Solution 6 - JavascriptFrancisc0View Answer on Stackoverflow
Solution 7 - JavascriptjrcView Answer on Stackoverflow
Solution 8 - JavascriptCory MawhorterView Answer on Stackoverflow