Angular 5访问http://localhost:4200提示Cannot GET /问题求助
ng serve Compiles Successfully Hi Imran, sorry to hear you're stuck with this frustrating issue—let's walk through some targeted fixes to get your app up and running:
1. Check Node.js & Angular CLI Version Compatibility
Angular 5 has strict version requirements for Node.js (it officially supports Node 8.x to 10.x). If you're running a newer Node version (like 12+), it can cause silent runtime conflicts.
- Run these commands in your terminal to verify versions:
node -v ng -v
If your Node version is outside the supported range, use a version manager like nvm to switch to a compatible release (e.g., Node 10.24.1).
2. Validate .angular-cli.json Configuration
Angular 5 uses .angular-cli.json (not the newer angular.json) to define project settings. A misconfigured entry here can prevent the server from finding your app's entry point:
- Open
.angular-cli.jsonand confirm theappssection has these correct values:"apps": [ { "root": "src", "outDir": "dist", "index": "index.html", "main": "main.ts", "polyfills": "polyfills.ts", "prefix": "app" } ]
Double-check that the index field points directly to src/index.html—a typo here will break the server's ability to load your app.
3. Clean Corrupted Cache & Reinstall Dependencies
Corrupted node modules or build cache are common culprits. Follow these steps to reset your project:
- Stop the
ng serveprocess (pressCtrl+Cin your terminal) - Delete these items from your project directory:
node_modulesfolderdistfolderpackage-lock.jsonfile
- Reinstall dependencies:
npm install - Rebuild and serve the project again:
ng serve
4. Test on a Different Port
Sometimes the default port 4200 is occupied by another application. Try serving your app on an alternate port:
ng serve --port 4201
Then visit http://localhost:4201 to see if the app loads correctly.
5. Check src/index.html for Issues
- Ensure the
srcfolder contains anindex.htmlfile (it should have been auto-created byng new) - Verify the
<base href="/">tag is present in the<head>section ofindex.html—this tag tells Angular how to resolve route paths, and missing it can cause the "Cannot GET /" error.
Let me know if any of these steps resolve your problem!
内容的提问来源于stack exchange,提问作者Imran

