64

I want to open a directory using cmd in visual studio code but its giving me an error in cmd. So, what setting is required for that?

I have performed below command

D:\RND>code .
3

18 Answers 18

116

It needs to be setup so that Code is found in your PATH. If you're on a mac system, do the following (for windows systems, read below):

  1. Launch VS Code
  2. Open up command palette (press F1) and type shell command to find Shell Command: Install 'code' command in PATH command.
  3. Restart terminal

VSCode screen capture

If you're on Windows, you can also set the PATH manually by adding:

C:\Program Files (x86)\Microsoft VS Code\bin

to your PATH environment variable.

  1. Open command prompt
  2. Type the command - setx path "%path%;C:\Program Files (x86)\Microsoft VS Code\bin"
  3. Restart terminal

Update: If you're on Windows 10 and VSCode 1.47.2 and above, the new PATH has to be set to:

C:\Users\{username}\AppData\Local\Programs\Microsoft VS Code\bin
14
  • 1
    @AnkitBhanderi see picture. you can't find that?
    – d.moncada
    Oct 9, 2017 at 5:06
  • @AnkitBhanderi does it now work, or am I right that you can't find it?
    – d.moncada
    Oct 9, 2017 at 5:09
  • 1
    @ilans is correct. In Windows 10 and VSCode 1.47.2 at least, the PATH has to be C:\Users\{username}\AppData\Local\Programs\Microsoft VS Code\bin. Jul 26, 2020 at 9:07
  • 2
    slightly different here: C:\Users\<username>\AppData\Local\Programs\Microsoft VS Code\_\bin worked for my machine
    – nate
    Feb 9, 2022 at 20:32
  • 1
    thanks for destroying my dev environment entire path variable is lost!! guess I'm not sleeping tonight! DO NOT TRUST THIS!!!! Mar 25, 2022 at 18:40
24

if using Windows:

  1. Press the "Windows"-button
  2. Type "Visual Studio Code"
  3. Right click on "VS Code" (or "Visual Studio Code")
  4. Click on "Open file location"

You will then be directed to a folder, where either the VS Code program is or a VS Code shortcut is.

If it is the shortcut, then right click on it and again press "Open file location". Copy the path:

path picture

which in my situation will be: "C:\Program Files\Microsoft VS Code"

And then do as Chawki posted - paste it into your "Environmnet variables" under "Path".

screenshot of environment variable

This is just to show, that the two above might not be the solution for you (they weren't for me).

Should this still not work for some reason then try restarting the terminal and/or add "/bin" to the path you have added inside your environment variable.

0
20

For me it was due to not running cmd as an administrator. (Right click Command Prompt -> Run as Administrator)

13

If you have the insider's editions of vscode installed, the command will be code-insiders.

1
  • 2
    this solved it for me on my LInux machine. To prevent me forgetting, I created an alias alias code="code-insiders" Oct 25, 2020 at 11:48
11

O/S windows

Setting the environment variable path to C:\Users\chawki\AppData\Local\Programs\Microsoft VS Code\bin solve the issue.

Like this.

1
  • Everyone, do not do it, it literally removes all the Paths for different softwares when you add this environmental variable, click "Cancel" before proceeding. Aug 20, 2023 at 19:38
4

Is your VS Code corrupted?

This path issue may indicate that VS Code files are corrupted and you may face other issues. Especially, if code command was working before.

No Repair or Reinstall

According to Microsoft, repair is unsupported.

Ultimate solution

  1. Uninstall code from control panel.
  2. Download and install code using the latest installer.
  3. Make sure to checkbox the path checkbox.

enter image description here

Note: All users?

Download the system installer if you want to make code available to all users.

Finally, enjoy!

0
4

1.Enter "View advanced systems settings" into the box Type here to search

2.Click "View advanced systems settings"

3.Click Environment Variables

enter image description here

3.On System variables click button New Then enter the variable name and add the following path C:\Users\user_name\AppData\Local\Programs\Microsoft VS Code\bin

enter image description here

4.Click OK

5.Close All tab VS Code and open again or Restart

1
3

Make sure you know where is the installation of VS Code, it can be in Program Files (x86) if you installed the 32bits version or Program Files if you installed the 64bits version VS Code location Once you know that Run (Win+r) cmd and type: setx path "%path%;C:\Program Files (x86)\Microsoft VS Code"

or

setx path "%path%;C:\Program Files\Microsoft VS Code"

after that, you can type code . to open VS Code with the project of the current folder.

1
3

I tried all the solutions but they didn't work. Turns out that Code was not installed on my system.

First go to the directory C:\Users\{username}\AppData\Local\Programs\Microsoft VS Code\bin and check if there is a folder named Microsoft VS Code. If there isn't any, download it from the website link attached below.

https://code.visualstudio.com/download

0
1

The best solution was on Mac is to move MSCode to the applications folder, and on Win is to search for the location of MSCode installation ( like: C:\Users{username}\AppData\Local\Programs\Microsoft VS Code\bin ) then you set the Path.

setx path "%path%;C:\Users\{username}\AppData\Local\Programs\Microsoft VS Code\bin
1

Notice that if you're using VS code insider's editions instead of using the command "code ." the command will be "code-insiders .".

That is obvisly after you set your PATH.

1

Windows 11

  • Close the Visual Studio Code
  • Open the Environment variables (Control Panel > System and Security > System Properties)
  • Edit the variable "Path", creating a C:\Users\{username}\AppData\Local\Programs\Microsoft VS Code\bin
  • Ok and ok in options and run VS Code;
  • For test write code in cmd, if to open VS Code is working.
1

Just uninstall and reinstall the vs code it worked for me

1

Uninstalling vscode and installing it again, worked for me.

0

First, check if you're on Windows 10 & Set User variables to

C:\Users\{username}\AppData\Local\Programs\Microsoft VS Code\bin

Give (OK) in environment variables and relaunch any unclosed terminals and type

code

Check if you have installed the VScode-insiders version

C:\Users\{username}\AppData\Local\Programs\Microsoft VS Code Insiders\bin

Give (OK) in environment variables and relaunch any unclosed terminals and type

code-insiders
0

In my case, I had two installs of VS Code in Windows 10. One was appended with (user).

I don't know how this happened but uninstalling the older version fixed my issue.

0

If adding the bin to the Windows PATH and reinstalling VScode does not work, try the following method, which worked for me:

  • Go to Windows Registry Editor
  • Go to HKEY_CURRENT_USER -> Software -> Windows -> Command Processor
  • Delete the value inside the AutoRun key, usually an if

Image

This method works for any command and does not throw back errors.

0

At the time of installation, check the Add to path option. If it is not opening through the cmd using the command code ., then restart your system once and retry the code . in cmd again. It should work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.