NPM live-server无法加载项目网站,仅显示目录列表求助
Troubleshooting live-server showing directory listing instead of your site
Hey there! I totally get how confusing this is when you're new to npm and live-server—let's work through this together to get your site loading properly. Here are the most common fixes to check:
Ensure you have an
index.htmlfile in your project root
Live-server automatically looks forindex.htmlas the default "homepage" for your site. If your main HTML file has a different name (likemain.htmlorhome.html), it won't know to load it and will just show the directory listing instead.
Quick fixes:- Rename your main HTML file to
index.html - Or specify the file directly when running the command:
live-server your-custom-filename.html
- Rename your main HTML file to
Double-check your terminal's working directory
If you ranlive-serverfrom a folder that's above your project root (like your Documents folder instead of your actual website folder), it'll list the contents of that parent folder instead of launching your site.
Fix: Use thecdcommand to navigate to your project's root first. For example:cd ~/Projects/my-new-website live-serverVerify your live-server installation method
If you installed live-server locally (without the-gflag), typing justlive-serverwon't recognize the command. You need to usenpxto run the local version.
Fixes:- Try running
npx live-serverinstead - If you want to use it globally (so you can just type
live-serveranywhere), reinstall it with:
(Note: On macOS/Linux you might need to addnpm install -g live-serversudobefore this command, or run Command Prompt as Administrator on Windows.)
- Try running
Check if your HTML file is in a subfolder
If your site's files are nested inside a folder likesrc,public, ordist, live-server won't automatically dig into that folder. You need to point it directly to the subfolder.
Fix: Runlive-server src(replacesrcwith your actual subfolder name) and it'll load theindex.htmlinside that directory.
Hopefully one of these fixes gets your site up and running! 😊
内容的提问来源于stack exchange,提问作者izee

