angular-cli icon indicating copy to clipboard operation
angular-cli copied to clipboard

bug(webpack): Webpack do not find bootstrap code if app is bootstrapped asynchronously

Open cladera opened this issue 9 years ago • 50 comments

OS?

Mac OSX (Sierra)

Versions.

angular-cli: 1.0.0-beta.22-1
node: 6.5.0
os: darwin x64

Repro steps.

  1. Configure i18n according to angular's cookbook
  2. run ng serve

The log given by the failure.

Tried to find bootstrap code, but could not. Specify either statically analyzable bootstrap code or pass in an entryModule to the plugins options.
Error: Tried to find bootstrap code, but could not. Specify either statically analyzable bootstrap code or pass in an entryModule to the plugins options.
    at Object.resolveEntryModuleFromMain (/Users/cgcladera/Projects/ws-v4/webcastudio-wms/node_modules/@ngtools/webpack/src/entry_resolver.js:121:15)
    at AotPlugin._setupOptions (/Users/cgcladera/Projects/ws-v4/webcastudio-wms/node_modules/@ngtools/webpack/src/plugin.js:129:58)
    at new AotPlugin (/Users/cgcladera/Projects/ws-v4/webcastudio-wms/node_modules/@ngtools/webpack/src/plugin.js:37:14)
    at Object.exports.getWebpackNonAotConfigPartial (/Users/cgcladera/Projects/ws-v4/webcastudio-wms/node_modules/angular-cli/models/webpack-build-typescript.js:20:13)
    at new NgCliWebpackConfig (/Users/cgcladera/Projects/ws-v4/webcastudio-wms/node_modules/angular-cli/models/webpack-config.js:23:42)
    at Class.run (/Users/cgcladera/Projects/ws-v4/webcastudio-wms/node_modules/angular-cli/tasks/serve-webpack.js:20:22)
    at /Users/cgcladera/Projects/ws-v4/webcastudio-wms/node_modules/angular-cli/commands/serve.js:102:26
    at process._tickCallback (internal/process/next_tick.js:103:7)

Mention any other details that might be useful.

I'm having this issue since I upgraded my project from beta.18 to beta.22.

After following i18n documentation your main.ts gets like this:

import './polyfills.ts';

import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import {
  enableProdMode
} from '@angular/core';
import { AppModule } from './app/';
import {environment} from './environments/environment';
import {getTranslationProviders} from './app/i18n-providers';

if (environment.production) {
  enableProdMode();
}

getTranslationProviders(environment.i18n.supportedLanguages).then(providers => {
  const options = { providers: providers };
  platformBrowserDynamic().bootstrapModule(AppModule, options);
});

The problem seems to be with bootstrapping the application asynchronously. If I do not get the translation providers and directly bootstrap the module ng serve runs normally.

cladera avatar Dec 13 '16 11:12 cladera

Same here:

OS?

Debian 8

Versions.

angular-cli: 1.0.0-beta.22-1 node: 6.9.2 os: linux x64

Repro steps.

Was this an app that wasn't created using the CLI? What change did you do on your code? etc. Update to beta.22-1

The log given by the failure.

Error: Tried to find bootstrap code, but could not. Specify either statically analyzable bootstrap code or pass in an entryModule to the plugins options. at Object.resolveEntryModuleFromMain (/home/user/workspace/project/node_modules/@ngtools/webpack/src/entry_resolver.js:121:15) at AotPlugin._setupOptions (/home/user/workspace/project/node_modules/@ngtools/webpack/src/plugin.js:129:58) at new AotPlugin (/home/user/workspace/project/node_modules/@ngtools/webpack/src/plugin.js:37:14) at Object.exports.getWebpackNonAotConfigPartial (/home/user/workspace/project/node_modules/angular-cli/models/webpack-build-typescript.js:20:13) at new NgCliWebpackConfig (/home/user/workspace/project/node_modules/angular-cli/models/webpack-config.js:23:42) at Class.run (/home/user/workspace/project/node_modules/angular-cli/tasks/serve-webpack.js:20:22) at /home/user/workspace/project/node_modules/angular-cli/commands/serve.js:102:26 at process._tickCallback (internal/process/next_tick.js:103:7)

Mention any other details that might be useful.

import './polyfills.ts';

import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { enableProdMode } from '@angular/core';
import { environment } from './environments/environment';
import { AppModule } from './app/';

if (environment.production) {
  enableProdMode();
}

platformBrowserDynamic().bootstrapModule(AppModule)
  .then(success => console.log(`Bootstrap success`))
  .catch(err => console.error(err));

I guess the .then() function is the reason for this behaviour. If I delete the .then and .catch functions it works as expected. Is there another suggested way for handling bootstrapping errors?

janis91 avatar Dec 13 '16 12:12 janis91

I think #2887 is about the same error and proposes different work arounds.

n0v1 avatar Dec 14 '16 07:12 n0v1

Upgraded to beta.23 and still having this issue

cladera avatar Dec 16 '16 09:12 cladera

I'm getting this on my build machine. Same problem.

skybluerunner avatar Dec 19 '16 02:12 skybluerunner

Same for me.

jakpaw avatar Dec 22 '16 12:12 jakpaw

Same for me. +1 for a bugfix.

derdeka avatar Dec 22 '16 12:12 derdeka

beta.24. Getting the same error when coded the AppModule and AppComponent inside the main.ts.

Didn't have this issue in beta.21.

yfain avatar Dec 26 '16 18:12 yfain

Hit the same issue, but after I removed

.then(success => console.log(`Bootstrap success`))
.catch(err => console.error(err));

from platformBrowserDynamic().bootstrapModule(AppModule) the issue disappeared.

Anastasoff avatar Dec 28 '16 06:12 Anastasoff

@Anastasoff I'm getting the same error #3744 without using then().

yfain avatar Dec 28 '16 09:12 yfain

Same problem because I use Polymer with angular and I need to bootstrap my app like that:

document.addEventListener('WebComponentsReady', function() {
  platformBrowserDynamic().bootstrapModule(AppModule);
});

florent1933 avatar Dec 28 '16 23:12 florent1933

I write "entryModule: 'app/app.module#AppModule'" to the AotPlugin options in "webpack-build-typescript.js", it works. So give a way to config the entryModule? :)

misty000 avatar Dec 31 '16 12:12 misty000

update to "version": "1.0.0-beta.24", and the same problem with

platformBrowserDynamic().bootstrapModule(AppModule);

angular-cli: 1.0.0-beta.24
node: 7.3.0
os: win32 x64
@angular/common: 2.4.1
@angular/compiler: 2.4.1
@angular/core: 2.4.1
@angular/forms: 2.4.1
@angular/http: 2.4.1
@angular/platform-browser: 2.4.1
@angular/platform-browser-dynamic: 2.4.1
@angular/router: 3.4.1
@angular/compiler-cli: 2.4.1

erscoder avatar Jan 04 '17 17:01 erscoder

solved! :)

when move files in webstorm, refact with extension and this is the problem.

change this: import { AppModule } from 'app/app.module.ts';

to: import { AppModule } from 'app/app.module';

erscoder avatar Jan 04 '17 19:01 erscoder

same for me.

heyauri avatar Jan 05 '17 01:01 heyauri

This AoT detection bug should have been solved in a post beta.24 release. See #2887.

For now, you can use following horrible hack snippet. You have to add following code PAST YOUR bootstrap sequence:

// <-- your custom init platformBrowserDynamic().bootstrapModule(AppModule) is located here

// HACK: horrible workaround for AoT bootstrap detection bug
let hackThis = false; 
if (hackThis) {
  platformBrowserDynamic().bootstrapModule(AppModule);
}

yuri1969 avatar Jan 05 '17 11:01 yuri1969

@yuri1969 doesn't work for me. Following is the bootstrap code. `import './polyfills.ts';

import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; import { enableProdMode } from '@angular/core'; import { environment } from './environments/environment';

import { BAppModule } from './bApp/'; import { BSystemService } from './bGlobal/BSystemService';

let bSystemService : BSystemService = BSystemService.getInstance();

if (environment.production) { enableProdMode(); }

if(bSystemService.deviceType == 'MOBILE'){ document.addEventListener("deviceready", function() { platformBrowserDynamic().bootstrapModule(BAppModule); }, false); } else{ platformBrowserDynamic().bootstrapModule(BAppModule); }

let hackThis = false; if (hackThis) { platformBrowserDynamic().bootstrapModule(BAppModule); } `

Error Tried to find bootstrap code, but could not. Specify either statically analyzable bootstrap code or pass in an entryModule to the plugins options. Error: Tried to find bootstrap code, but could not. Specify either statically analyzable bootstrap code or pass in an entryModule to the plugins options. at Object.resolveEntryModuleFromMain (/home/jwuser/projects/Repo/bitbucket/ghs/node_modules/@ngtools/webpack/src/entry_resolver.js:121:15) at AotPlugin._setupOptions (/home/jwuser/projects/Repo/bitbucket/ghs/node_modules/@ngtools/webpack/src/plugin.js:158:54) at new AotPlugin (/home/jwuser/projects/Repo/bitbucket/ghs/node_modules/@ngtools/webpack/src/plugin.js:17:14) at Object.exports.getWebpackNonAotConfigPartial (/home/jwuser/projects/Repo/bitbucket/ghs/node_modules/angular-cli/models/webpack-build-typescript.js:20:13) at new NgCliWebpackConfig (/home/jwuser/projects/Repo/bitbucket/ghs/node_modules/angular-cli/models/webpack-config.js:26:42) at Class.run (/home/jwuser/projects/Repo/bitbucket/ghs/node_modules/angular-cli/tasks/serve-webpack.js:20:22) at /home/jwuser/projects/Repo/bitbucket/ghs/node_modules/angular-cli/commands/serve.js:108:26 at process._tickCallback (internal/process/next_tick.js:103:7)

tulinisarg avatar Jan 16 '17 08:01 tulinisarg

Same here.

I used to get things works based on @yuri1969 "hack" but since this morning, i get the same error as @tulinisarg

See code below.

import './polyfills.ts';

import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; import { enableProdMode } from '@angular/core'; import { environment } from './environments/environment'; import { AppModule } from './app/app.module';

import { getTranslationProviders } from './app/i18n-provider';

if (environment.production) { enableProdMode(); }

getTranslationProviders().then(providers => { const options = { providers }; platformBrowserDynamic().bootstrapModule(AppModule, options);

});

let hackThis = false; if (hackThis) { platformBrowserDynamic().bootstrapModule(AppModule); }

There is my package.json :

"dependencies": { "@angular/common": "^2.4.0", "@angular/compiler": "^2.4.0", "@angular/compiler-cli": "^2.4.0", "@angular/core": "^2.4.0", "@angular/forms": "^2.4.0", "@angular/http": "^2.4.0", "@angular/platform-browser": "^2.4.0", "@angular/platform-browser-dynamic": "^2.4.0", "@angular/platform-server": "^2.4.0", "@angular/router": "~3.4.0", "@types/lodash": "^4.14.39", "@types/rx": "^2.5.33", "concurrently": "^3.1.0", "core-js": "^2.4.1", "lodash": "^4.17.2", "rx": "4.1.0", "ts-helpers": "^1.1.1", "zone.js": "^0.7.2" }, "devDependencies": { "@reactivex/rxjs": "^5.0.0-rc.4", "@types/jasmine": "^2.2.30", "@types/node": "^6.0.42", "angular-cli": "1.0.0-beta.24", "codelyzer": "1.0.0-beta.1", "graceful-fs": "4.1.11", "jasmine-core": "2.4.1", "jasmine-spec-reporter": "2.5.0", "karma": "1.2.0", "karma-chrome-launcher": "^2.0.0", "karma-cli": "^1.0.1", "karma-jasmine": "^1.0.2", "karma-remap-istanbul": "^0.2.1", "protractor": "4.0.9", "ts-node": "1.2.1", "tslint": "3.13.0", "typescript": "~2.0.3" }

yroul avatar Jan 16 '17 10:01 yroul

@yroul Can you try writing the bootstrap code in a function? It works for me.

here are the dependencies "dependencies": { "@angular/common": "^2.4.1", "@angular/compiler": "^2.4.1", "@angular/core": "^2.4.1", "@angular/forms": "^2.4.1", "@angular/http": "^2.4.1", "@angular/platform-browser": "^2.4.1", "@angular/platform-browser-dynamic": "^2.4.1", "@angular/router": "^3.4.1", "angular2-onsenui": "^1.0.0-rc.3", "core-js": "^2.4.1", "onsenui": "^2.0.5", "rxjs": "^5.0.2", "ts-helpers": "^1.1.2", "zone.js": "^0.7.5" }

main.ts

`import './polyfills.ts';

import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; import { enableProdMode } from '@angular/core'; import { environment } from './environments/environment';

import { BAppModule } from './bApp/'; import { BSystemService } from './bGlobal/BSystemService';

let bSystemService: BSystemService = BSystemService.getInstance();

if (environment.production) { enableProdMode(); }

if (bSystemService.deviceType == 'MOBILE') { document.addEventListener('deviceready', function() { bootstrapNow(); }); } else { bootstrapNow(); }

function bootstrapNow() { platformBrowserDynamic().bootstrapModule(BAppModule); }`

tulinisarg avatar Jan 16 '17 11:01 tulinisarg

@tulinisarg @yroul I've tried that hack of mine on beta.24. It needs retesting on any newer Angular-cli.

I can look into it later.

yuri1969 avatar Jan 16 '17 12:01 yuri1969

@tulinisarg @yuri1969

I was able to make it work using @tulinisarg method (putting bootstrap code in function) or by simply removing @yuri1969 's hack.

Here is my main.ts (without yuri's hack)

import './polyfills.ts';

import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; import { enableProdMode } from '@angular/core'; import { environment } from './environments/environment'; import { AppModule } from './app/app.module';

import { getTranslationProviders } from './app/i18n-provider';

if (environment.production) { enableProdMode(); } /** *

  • It looks like the "hack" below does not work anymore since 01/16/2017
  • Disable I18n provider and async loading */ getTranslationProviders().then(providers => { const options = { providers }; platformBrowserDynamic().bootstrapModule(AppModule, options);

});

my package.json

"dependencies": { "@angular/common": "^2.4.0", "@angular/compiler": "^2.4.0", "@angular/compiler-cli": "^2.4.0", "@angular/core": "^2.4.0", "@angular/forms": "^2.4.0", "@angular/http": "^2.4.0", "@angular/platform-browser": "^2.4.0", "@angular/platform-browser-dynamic": "^2.4.0", "@angular/platform-server": "^2.4.0", "@angular/router": "~3.4.0", "@types/lodash": "^4.14.39", "@types/rx": "^2.5.33", "concurrently": "^3.1.0", "core-js": "^2.4.1", "lodash": "^4.17.2", "rx": "4.1.0", "ts-helpers": "^1.1.1", "zone.js": "^0.7.2" }, "devDependencies": { "@reactivex/rxjs": "^5.0.0-rc.4", "@types/jasmine": "^2.2.30", "@types/node": "^6.0.42", "angular-cli": "1.0.0-beta.24", "codelyzer": "1.0.0-beta.1", "graceful-fs": "4.1.11", "jasmine-core": "2.4.1", "jasmine-spec-reporter": "2.5.0", "karma": "1.2.0", "karma-chrome-launcher": "^2.0.0", "karma-cli": "^1.0.1", "karma-jasmine": "^1.0.2", "karma-remap-istanbul": "^0.2.1", "protractor": "4.0.9", "ts-node": "1.2.1", "tslint": "3.13.0", "typescript": "~2.0.3" }

Then

rm -rf node_modules && npm cache clean && npm install && ng build --prod

yroul avatar Jan 16 '17 12:01 yroul