You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Angular 5执行ng serve报错TS2307:找不到https模块求助

Fixing "Cannot find module 'https'" Error in Angular (Latest Versions)

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.json file
  • Look for the types array in the compilerOptions section. 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 serve process
  • Delete the node_modules/.cache folder manually
  • Restart the server with the --delete-output-path flag 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:

  1. Update the global Angular CLI:
    npm install -g @angular/cli
    
  2. Update your project's core dependencies:
    ng update @angular/core @angular/cli
    
  3. If you run into dependency conflicts, manually adjust version numbers in package.json to match the latest stable Angular versions, then run npm 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 04:30:06