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

This happens because static parser founds multiple bootstrap code. We moved them into another file and called from main.ts. And to satisfy parser, we added a fake bootstrap code which is like that:

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

But notice that parser looks for exactly "bootstrapModule" naming. So, the extracted method (moved to another file) should have a different named method!

I hope angular-cli team finds a good way of this. Because it's very natural that we can have multiple startup modules and use one of them conditionally.

hikalkan avatar Jan 16 '17 13:01 hikalkan

Yes, they have added a check for multiple bootstraps.

As @hikalkan says you need to separate your "real" bootstrap to another class/method in different file from main.ts and leave only the hacky bootstrap there. Then you need to call that method from the main.ts.

yuri1969 avatar Jan 18 '17 10:01 yuri1969

I also ran in to this problem. And while it may not be the solution to this case, what caused it for me was this addition:

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

This is recommended in the official style guide however resulted in the same error as the OP. Removing the .then and .catch allowed for successful ng serve.

BnSmth avatar Jan 22 '17 12:01 BnSmth

@BnSmth I have this same issue. I only have one bootstrap call and if I add a catch to it it fails to compile. This has been this way for a while. I'm currently on CLI.24 but have had this problem since I started using the CLI at ver 19. Not sure why they leave this bug version after version after version.

buddyackerman avatar Jan 24 '17 20:01 buddyackerman

Same issue here with 1.0.0-beta.24.

I'm commenting here to hopefully get notified when this issue is closed so we can put the recommended .then and .catch back.

ukaaa avatar Jan 27 '17 09:01 ukaaa

Workaround https://github.com/angular/angular-cli/issues/3540#issuecomment-270627460 works for me both on ng serve and build --aot. Looks like this now:

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

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

MickL avatar Jan 28 '17 15:01 MickL

So i did an npm update and it was not working anymore. Then i updated angular-cli from beta24 to beta26 and it is working fine. No workaround required!

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

MickL avatar Jan 30 '17 15:01 MickL

I still getting this on beta26. I don't even have the .then and .catch. I upgraded an older project.

Probotect0r avatar Feb 01 '17 01:02 Probotect0r

@Probotect0r Updating to beta.26 solved the problem for me. Is your project too large to maybe create a new project using the beta.26 CLI and then move your "old" code to it to see if it breaks?

buddyackerman avatar Feb 01 '17 16:02 buddyackerman

There is no posible to have N calls to "bootstrapModule" or "bootstrapModuleFactory" in your main file

the entry_resolver.js file, in @ngtools/webpack/src/, is limited to have only one bootstrap mode, so if you need to do something like this


if(isUserLoggedIn)  {
    platformBrowserDynamic().bootstrapModule(AppModule);
} else {
    platformBrowserDynamic().bootstrapModule(LoginModule);
}

// or 

var moduleToBootstrap = null

if(isUserLoggedIn)  {
    moduleToBootstrap  = AppModule;
} else {
    moduleToBootstrap  = LoginModule;
}

platformBrowserDynamic().bootstrapModule(moduleToBootstrap);


// or 

import { AppModule as FooBar } from './app/app.module';

platformBrowserDynamic().bootstrapModule(FooBar);

or any other logic to bootstrap your application, you have to find another way to do that.

I hope this information is of use to you.

fernandolguevara avatar Feb 01 '17 20:02 fernandolguevara

@buddyackerman Yea thats what I ended up doing.

Probotect0r avatar Feb 01 '17 21:02 Probotect0r

@MickL I am on beta.24. The hack thing was working untill I reinstall npm_moudles. So there is some internal module that breaking the thing. So if you do fresh npm install on beta.24 the hack won't work any more.

my devDependencies:

"devDependencies": {
    "@angular/compiler-cli": "2.4.0",
    "@types/jasmine": "2.5.38",
    "@types/node": "6.0.52",
    "@types/whatwg-fetch": "0.0.33",
    "angular-cli": "1.0.0-beta.24",
    "codelyzer": "2.0.0-beta.4",
    "jasmine-core": "2.5.2",
    "jasmine-spec-reporter": "2.5.0",
    "karma": "1.2.0",
    "karma-chrome-launcher": "2.0.0",
    "karma-cli": "1.0.1",
    "karma-jasmine": "1.1.0",
    "karma-remap-istanbul": "0.2.2",
    "protractor": "4.0.14",
    "ts-node": "1.2.1",
    "tslint": "4.1.1",
    "typescript": "2.0.10"
  }

Will try to migrate to beta.28 and see if it will help.

UPDATE: I can confirm that with beta.28.3 all working without a hack. That is the list of packages that beta.28.3 is bringing in:

+-- @angular/[email protected]
+-- @angular/[email protected]
+-- @angular/[email protected]
+-- @angular/[email protected]
+-- @angular/[email protected]
+-- @angular/[email protected]
+-- @angular/[email protected]
+-- @angular/[email protected]
+-- @angular/[email protected]
+-- @types/[email protected]
+-- @types/[email protected]
+-- [email protected]
+-- [email protected]
+-- [email protected]
+-- [email protected]
+-- [email protected]
+-- [email protected]
+-- [email protected]
+-- [email protected]
+-- [email protected]
+-- [email protected]
+-- [email protected]
+-- [email protected]
+-- [email protected]
+-- [email protected]
+-- [email protected]
+-- [email protected]
`-- [email protected]

kuncevic avatar Feb 01 '17 23:02 kuncevic

thanks @hikalkan for explaining the issue but i did a little more hacky solution than moving it to separate file. looks like cli is not able to detect the below and it works :)

switch (id) {
  case 'left':
    platform['bootstrapModule'](App1Module); break;
  case 'right':
    platform['bootstrapModule'](App2Module);
}

nagarsuresh avatar Mar 05 '17 02:03 nagarsuresh

@snagar78 what imports are you using for that to work?

tavelli avatar Mar 10 '17 19:03 tavelli

@tavelli there are no special imports for that the just the ones which are needed, anyways you can look at the sample at https://github.com/snagar78/Ng2-MultRoot-App

let me know if that helps.

nagarsuresh avatar Mar 10 '17 22:03 nagarsuresh

I need to bootstrap the app like this, but don't works

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

mmenik avatar Mar 21 '17 14:03 mmenik

working with angular polymer I need to bootstrap this way but it is not working

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

africhild avatar Mar 27 '17 03:03 africhild

double check your { AppModule } import in main.ts file . I had a wrong path to the app module in the import statement.

sjselvan avatar Mar 31 '17 04:03 sjselvan

Is their an update for this issue after "@angular/cli": "1.0.0" release?

ahashem avatar Apr 09 '17 10:04 ahashem

I just got the latest Angular-CLI:

    _                      _                 ____ _     ___
   / \   _ __   __ _ _   _| | __ _ _ __     / ___| |   |_ _|
  / △ \ | '_ \ / _` | | | | |/ _` | '__|   | |   | |    | |
 / ___ \| | | | (_| | |_| | | (_| | |      | |___| |___ | |
/_/   \_\_| |_|\__, |\__,_|_|\__,_|_|       \____|_____|___|
               |___/
@angular/cli: 1.0.0
node: 7.5.0
os: darwin x64
@angular/animations: 4.0.2
@angular/common: 4.0.2
@angular/compiler: 4.0.2
@angular/compiler-cli: 4.0.2
@angular/core: 4.0.2
@angular/forms: 4.0.2
@angular/http: 4.0.2
@angular/platform-browser: 4.0.2
@angular/platform-browser-dynamic: 4.0.2
@angular/platform-server: 4.0.2
@angular/router: 4.0.2
@angular/cli: 1.0.0

And then I tried to look at using AOT via instructions on https://angular.io/docs/ts/latest/cookbook/aot-compiler.html

I run

npm run build:aot

> [email protected] build:aot /Users/magleahy/Desktop/projects/angular-latest
> ngc -p tsconfig-aot.json && rollup -c rollup-config.js

⚠️   'default' is imported from external module 'rollup' but never used

and then

ng serve

and get

Tried to find bootstrap code, but could not. Specify either statically analyzable bootstrap code or pass in an entryModule to the plugins options.

magleahy avatar Apr 12 '17 20:04 magleahy