Angular 5执行ng serve报错TS2307:找不到https模块求助
Hey there, let's work through this error you're hitting when running ng serve—it's confusing, especially since older Angular versions' solutions don't apply, and your webpack compiles fine after saving. Let's break down the possible fixes step by step:
1. First: Check if you're mistakenly importing the Node.js https module in frontend code
The https module is a Node.js core module, not something that exists in the browser environment where Angular runs. Take a close look at your app.module.ts (and any other imported files) for lines like:
import * as https from 'https';
If you find this, delete it immediately. For frontend HTTP requests, Angular's built-in HttpClientModule is the correct tool—make sure you've imported that in app.module.ts instead:
import { HttpClientModule } from '@angular/common/http'; @NgModule({ imports: [ HttpClientModule, // ... other imports ] }) export class AppModule { }
2. Adjust TypeScript configuration to avoid Node.js type conflicts
If you didn't import https explicitly, TypeScript might be picking up Node.js types incorrectly:
- Open your
tsconfig.jsonfile - Look for the
typesarray in thecompilerOptionssection. If it includes"node", remove it unless your project is using Angular Universal (server-side rendering). Frontend-only Angular projects don't need Node.js type definitions. - If you do need Node.js types (e.g., SSR), ensure you have the correct type definitions installed:
npm install @types/node --save-dev
3. Clear development server cache (since saving fixes compilation)
Your mention that webpack compiles fine after saving points to a possible cache issue with the Angular dev server. Try these steps:
- Stop the
ng serveprocess - Delete the
node_modules/.cachefolder manually - Restart the server with the
--delete-output-pathflag to force a clean build:ng serve --delete-output-path
4. Upgrade Angular CLI and dependencies (skip system upgrade first)
Upgrading your entire system isn't the first fix to try, but updating your Angular tooling often resolves obscure compatibility bugs:
- Update the global Angular CLI:
npm install -g @angular/cli - Update your project's core dependencies:
ng update @angular/core @angular/cli - If you run into dependency conflicts, manually adjust version numbers in
package.jsonto match the latest stable Angular versions, then runnpm install.
Quick recap of your scenario
You noted that after saving files, webpack completes compilation successfully—this suggests the initial build is hitting a cached or misconfigured state, while the hot reload triggers a cleaner build. The fixes above should address both the initial error and the inconsistent behavior.
内容的提问来源于stack exchange,提问作者Casey Harrils

