ng build throws error: "Tried to find bootstrap code, but could not."
ng build throws error: "Tried to find bootstrap code, but could not."
尝试使用 ng-build
构建我的应用程序时,出现以下错误:
Tried to find bootstrap code, but could not. Specify either statically analyzable bootstrap code or pass in an entryModule to the plugins options.
我在 .angular-cli.json
中指定的主文件非常简单:
import { NgModule } from '@angular/core';
import { LoginComponent } from './login.component';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'
import { HttpClientModule } from '@angular/common/http';
import { LoginService } from '../login.service'
@NgModule({
declarations: [
LoginComponent
],
imports: [
BrowserModule,
FormsModule,
HttpClientModule
],
providers: [LoginService],
bootstrap: [LoginComponent]
})
export class AppModule { }
platformBrowserDynamic().bootstrapModule(AppModule);
ng --version
returns 以下内容:
Angular CLI: 1.7.0
Node: 6.11.0
OS: win32 x64
Angular: 5.2.5
... common, compiler, compiler-cli, core, forms
... platform-browser, platform-browser-dynamic
@angular/cli: 1.7.0
@angular-devkit/build-optimizer: 0.3.1
@angular-devkit/core: 0.3.1
@angular-devkit/schematics: 0.3.1
@ngtools/json-schema: 1.2.0
@ngtools/webpack: 1.10.0
@schematics/angular: 0.3.1
@schematics/package-update: 0.3.1
typescript: 2.7.2
webpack-replace: 1.0.0
webpack-vendor-chunk-plugin: 1.0.0
webpack: 3.11.0
以防万一有人遇到同样的问题,我的问题是我在包含要引导的模块(即 AppModule
)的同一个文件中引导应用程序,而 ng build
期望模块 导入 到您调用 bootstrapModule()
的文件中。相关代码在 node_modules\@ngtools\webpack\src\entry_resolver.js
中的函数 resolveEntryModuleFromMain()
中,它查找文件中声明的所有导入,其中 resolveEntryModuleFromMain()
被调用以用于引导模块。将对 bootstrapModule()
的调用移动到另一个文件并导入 AppModule
解决了这个问题(无论如何都是更好的做法)。
从 David 的回答中得到线索,我尝试了这个并为我工作。
步骤 1:
在与 main.ts 文件相同的文件夹中创建一个名为 AppModule.ts 的新文件。
第 2 步:
将所有内容从 main.ts 文件移动到 AppModule.ts 文件。
第 3 步:
移动后,main.ts 文件应该只有这个代码:
import './polyfills';
import {platformBrowserDynamic} from '@angular/platform-browser-dynamic';
import {AppModule} from './AppModule';
platformBrowserDynamic().bootstrapModule(AppModule);
步骤 4:
我的 AppModule.ts 文件有这段代码。根据您编码的内容,您的 AppModule.ts 可能有不同的代码。
import {HttpClientModule} from '@angular/common/http';
import {NgModule} from '@angular/core';
import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
import {BrowserModule} from '@angular/platform-browser';
import {YOURCustomCode} from './app/YOURCustomCode';
@NgModule({
imports: [
BrowserModule,
BrowserAnimationsModule,
FormsModule,
HttpClientModule,
DemoMaterialModule,
MatNativeDateModule,
ReactiveFormsModule,
],
entryComponents: [YOURCustomCode],
declarations: [YOURCustomCode],
bootstrap: [YOURCustomCode],
providers: []
})
export class AppModule {}
步骤 5:
运行 命令 ng serve
并且编译和构建没有错误 bootstrap 现在模块错误。
尝试使用 ng-build
构建我的应用程序时,出现以下错误:
Tried to find bootstrap code, but could not. Specify either statically analyzable bootstrap code or pass in an entryModule to the plugins options.
我在 .angular-cli.json
中指定的主文件非常简单:
import { NgModule } from '@angular/core';
import { LoginComponent } from './login.component';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'
import { HttpClientModule } from '@angular/common/http';
import { LoginService } from '../login.service'
@NgModule({
declarations: [
LoginComponent
],
imports: [
BrowserModule,
FormsModule,
HttpClientModule
],
providers: [LoginService],
bootstrap: [LoginComponent]
})
export class AppModule { }
platformBrowserDynamic().bootstrapModule(AppModule);
ng --version
returns 以下内容:
Angular CLI: 1.7.0
Node: 6.11.0
OS: win32 x64
Angular: 5.2.5
... common, compiler, compiler-cli, core, forms
... platform-browser, platform-browser-dynamic
@angular/cli: 1.7.0
@angular-devkit/build-optimizer: 0.3.1
@angular-devkit/core: 0.3.1
@angular-devkit/schematics: 0.3.1
@ngtools/json-schema: 1.2.0
@ngtools/webpack: 1.10.0
@schematics/angular: 0.3.1
@schematics/package-update: 0.3.1
typescript: 2.7.2
webpack-replace: 1.0.0
webpack-vendor-chunk-plugin: 1.0.0
webpack: 3.11.0
以防万一有人遇到同样的问题,我的问题是我在包含要引导的模块(即 AppModule
)的同一个文件中引导应用程序,而 ng build
期望模块 导入 到您调用 bootstrapModule()
的文件中。相关代码在 node_modules\@ngtools\webpack\src\entry_resolver.js
中的函数 resolveEntryModuleFromMain()
中,它查找文件中声明的所有导入,其中 resolveEntryModuleFromMain()
被调用以用于引导模块。将对 bootstrapModule()
的调用移动到另一个文件并导入 AppModule
解决了这个问题(无论如何都是更好的做法)。
从 David 的回答中得到线索,我尝试了这个并为我工作。
步骤 1: 在与 main.ts 文件相同的文件夹中创建一个名为 AppModule.ts 的新文件。
第 2 步: 将所有内容从 main.ts 文件移动到 AppModule.ts 文件。
第 3 步: 移动后,main.ts 文件应该只有这个代码:
import './polyfills';
import {platformBrowserDynamic} from '@angular/platform-browser-dynamic';
import {AppModule} from './AppModule';
platformBrowserDynamic().bootstrapModule(AppModule);
步骤 4: 我的 AppModule.ts 文件有这段代码。根据您编码的内容,您的 AppModule.ts 可能有不同的代码。
import {HttpClientModule} from '@angular/common/http';
import {NgModule} from '@angular/core';
import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
import {BrowserModule} from '@angular/platform-browser';
import {YOURCustomCode} from './app/YOURCustomCode';
@NgModule({
imports: [
BrowserModule,
BrowserAnimationsModule,
FormsModule,
HttpClientModule,
DemoMaterialModule,
MatNativeDateModule,
ReactiveFormsModule,
],
entryComponents: [YOURCustomCode],
declarations: [YOURCustomCode],
bootstrap: [YOURCustomCode],
providers: []
})
export class AppModule {}
步骤 5:
运行 命令 ng serve
并且编译和构建没有错误 bootstrap 现在模块错误。