Last updated: Apr 6, 2024
Reading time·7 min
There are multiple reasons why the Live Server extension might not work in Visual Studio Code:
index.html
file in your project folder.index.html
file
(e.g. in the project folder).index.html
file (e.g. forgetting to close
the body
tag).index.html
file in your root directoryThe first thing you should try is to place your index.html
file in the root
directory of your project (where your package.json
file is).
Your index.html
file might be as simple as the following.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> </head> <body> <div id="box">bobbyhadz.com</div> <script src="index.js"></script> </body> </html>
The related index.js
file should be located in the same directory as your
index.html
file.
console.log('hello world');
This assumes that your project's folder structure looks as follows.
my-project/ └── index.html └── index.js
You can right-click in your index.html
file and select "Open with Live
Server" to start the live server extension.
This should automatically start the live server and open a tab in your browser, e.g. under http://127.0.0.1:5500/index.html.
If you need to open VS Code in the root directory of your project:
code .
command.code .
The .
signals to VS Code to open the code editor in the current folder.
You can also issue Live Server commands by using the Command Palette:
Open your index.html
file.
Press:
Ctrl
+ Shift
+ P
on Windows and LinuxCommand
+ Shift
+ P
on macOSF1
to open the Command Palette.Type live server.
Issue the specific command to start or stop the live server.
If the issue persists, try to restart the live server.
index.html
file and select Stop Live Server.index.html
file and select Open with Live Server.If the error persists, try restarting your VS Code Window.
Ctrl
+ Shift
+ P
on Windows and Linux.Command
+ Shift
+ P
on macOS.F1
to open the Command Palette.Another thing you should ensure is that the name of your project's folder
doesn't contain spaces or special characters (e.g. hashes #
or dollar signs
$
).
The complete path to the folder should not contain spaces.
For example, if your project's folder is named my folder
(contains a space),
you have a couple of options:
my-folder
.my_folder
.myFolder
.If you happen to rename your project folder or any folder in the path, make sure to restart VS Code as shown in the previous subheading.
Make sure the HTML markup in your index.html
file is syntactically correct.
Here is an example of a valid index.html
file.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> </head> <body> <div id="box">bobbyhadz.com</div> <script src="index.js"></script> </body> </html>
Notice that the html
, head
, body
, div
and script
tags are closed.
The index.js
file that is loaded may be as simple as follows.
console.log('hello world');
The example assumes that the index.html
and index.js
files are located in
the same directory.
my-project/ └── index.html └── index.js
VS Code has Emmet enabled by default, so you can quickly add a boilerplate HTML template by:
!
(an exclamation mark) in your index.html
file.Tab
or selecting the option from the menu.Ctrl
+ Shift
+ X
on Windows or Linux.Command
+ Shift
+ X
on macOS.Also, make sure to install the correct Live Server extension by Ritwick Dey.
If the error persists, try to disable and reenable the Live Server extension.
I've also written an article on how to install an older version of an extension in VS Code in case you got the issue after updating your live server version.
If the error persists, set the default browser for the live server extension.
Ctrl
+ Shift
+ P
(or Command
+ Shift
+ P
on macOS).F1
to open the Command Palette.You can also open the settings screen by pressing Ctrl
+ ,
on Windows and
Linux or Cmd
+ ,
on macOS.
By default, the extension opens your default browser (based on your operating system settings).
For example, you could select chrome
.
settings.json
fileYou can also set the default browser for the extension in your settings.json file.
Ctrl
+ Shift
+ P
(or Command
+ Shift
+ P
on macOS).F1
to open the Command Palette.Type user settings json.
Click on Preferences: Open User Settings (JSON)
settings.json
file.{ "liveServer.settings.CustomBrowser": "chrome", }
You can also set the setting to different values, e.g. firefox
.
An alternative approach is to set the default browser for the extension applied only to the current project.
In the root directory of your project, create a .vscode
folder.
Create a settings.json
file in the .vscode
folder.
Add the following code to your settings.json
file.
{ "liveServer.settings.CustomBrowser": "chrome" }
The settings that are in your .vscode/settings.json
file are only applied to
the current project and override any global settings.
If the issue persists, try setting the default browser for your operating system.
How to set your default browser depends on your operating system.
For example, on Windows 11:
You can also select your preferred browser under the Web browser setting in the Default apps section.
To solve the issue where the Live Server extension doesn't work in VS Code, make sure:
index.html
file to the root directory of your project.index.html
file.You can learn more about the related topics by checking out the following tutorials: