DEV Community

GoldJns
GoldJns

Posted on • Updated on

Boost your Workflow with VS Code Tasks: Reinstalling NPM Dependencies 🚀

As developers, we often find ourselves in the midst of managing npm dependencies. Whether it's updating packages, resolving conflicts, or simply cleaning up the project, this task can sometimes feel cumbersome. However, with the right tools, we can streamline this process and save valuable time.

The Challenge: Managing NPM Dependencies
One common scenario is when we need to clean up our project's dependencies and reinstall them. This might be necessary due to conflicts, outdated packages, or simply to start with a clean slate. Traditionally, we might resort to running a series of commands in the terminal:

rimraf node_modules
rimraf package-lock.json
npm install
Enter fullscreen mode Exit fullscreen mode

Btw, if you dont have rimraf, consider using it! It is really helpful to delete folders effectively:

npm install rimraf
Enter fullscreen mode Exit fullscreen mode

Honestly, I hate typing out these commands every time; I'd rather have a one-click option to completely clean up and reinstall the dependencies.

The Solution: VS Code Tasks
Enter Visual Studio Code tasks - a powerful feature that allows us to automate common tasks directly within the editor. By leveraging tasks, we can encapsulate complex workflows into simple, one-click actions.

Let's create a VS Code task that cleans up our project's dependencies and reinstalls them with a single command.

Step 1: Creating the Task

Open your project in Visual Studio Code and navigate to the Tasks menu (Ctrl + Shift + B).

Select Configure Tasks and then Create tasks.json file from template > Others.

Step 2: Define the Task

Replace the contents of tasks.json with the following configuration:

{
  "version": "2.0.0",
  "tasks": [
 {
      "label": "Clean node_modules",
      "type": "shell",
      "command": "rimraf",
      "args": ["node_modules"]
    },
    {
      "label": "Clean package-lock.json",
      "type": "shell",
      "command": "rimraf",
      "args": ["package-lock.json"]
    },
    {
      "label": "Install dependencies",
      "type": "shell",
      "command": "npm",
      "args": ["install"]
    },
    {
      "label": "Npm reinstall",
      "dependsOrder": "sequence",
      "dependsOn": [
        "Clean node_modules",
        "Clean package-lock.json",
        "Install dependencies",
      ]
    }
]
}
Enter fullscreen mode Exit fullscreen mode

I used vs code compound task so the execution order is correct and the task does not abort after one command.

Step 3: Running the Task

Save tasks.json, and you're all set! Now, whenever you need to clean up your project's dependencies and reinstall them, simply press Ctrl + Shift + B to run the task or click on it on your vs code toolbar.

Conclusion
By using VS Code tasks, we've simplified the process of managing npm dependencies. With just a single command, we can now clean up our project and ensure that we're always working with the latest dependencies.

Take advantage of this tip to boost your workflow and focus on what really matters - writing great code!

Top comments (0)