Configuring Visual Studio Code to Use a Specific Node.js Version with NVM

Soefyan Syah
Soefyan Syah27 Feb 2024
javascriptnode.jsvscode

Visual Studio Code (VS Code) is a popular code editor that supports a wide range of programming languages. When working on a project, it's essential to ensure that the correct Node.js version is used to avoid compatibility issues. This article will discuss two solutions to set up Visual Studio Code to use a specific Node.js version specified by Node Version Manager (NVM) on macOS.

Configuring Visual Studio Code to Use a Specific Node.js Version with NVM
Configuring Visual Studio Code to Use a Specific Node.js Version with NVM

Solution 1: Setting Default Node.js Version with NVM Alias The first solution involves using NVM to set a default Node.js version for the entire system. By running the command nvm alias default v21.0.0, we can instruct NVM to use version 21.0.0 as the default Node.js version. This ensures that any new terminal sessions or applications, including Visual Studio Code, will use the specified Node.js version.

Solution 2: Configuring Visual Studio Code Launch Configuration The second solution is to create a configuration file specifically for Visual Studio Code within the project's root directory. This file, named "launch.json," allows us to define how VS Code should run and which Node.js version to use. Below is an example of the "launch.json" file content:

{
  "type": "node",
  "request": "launch",
  "name": "App",
  "program": "${workspaceRoot}/index.js",
  "runtimeExecutable": "${env:HOME}/.nvm/versions/node/v21.0.0/bin/node"
}


In this configuration, we specify the type of program as "node," set the launch request to "launch," provide a name for the configuration ("App"), specify the entry point of the application ("index.js"), and set the "runtimeExecutable" to the path of the Node.js executable for version 21.0.0.

Compatibility on macOS: Both solutions work seamlessly on macOS, ensuring that Visual Studio Code uses the specified Node.js version consistently. macOS users can confidently choose the method that best fits their workflow or project requirements.

Conclusion: Configuring Visual Studio Code to use a specific Node.js version with NVM is crucial for maintaining project consistency and avoiding compatibility issues. By either setting a default Node.js version with an NVM alias or configuring the launch settings in Visual Studio Code, developers on macOS can ensure a smooth development experience. Choose the solution that aligns with your preferences and project specifications to enhance your coding workflow with Visual Studio Code.

Soefyan Syah

Written by Soefyan Syah

Hey there! I hope you find this post useful. If you have anything to say, questions or feedback, send me a tweet or an email.