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

--aot fails to build with no entryModule error

Open zackarychapple opened this issue 10 years ago • 41 comments

OS?

osX El Capitan

Versions.

angular-cli: 1.0.0-beta.18
node: 6.3.1
os: darwin x64

Repro steps.

When trying to use --aot on an app without the standard app.module.ts pattern the build fails with the below error. As per the documentation tried adding the following to the tsconfig and that did not resolve the issue.

  "angularCompilerOptions": {
    "entryModule": "app/wfa-ng2.module#WfaNg2Module"
  }

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/zchapple/code/wfa-ts/node_modules/@ngtools/webpack/src/entry_resolver.js:143:15)
    at AotPlugin._setupOptions (/Users/zchapple/code/wfa-ts/node_modules/@ngtools/webpack/src/plugin.js:105:77)
    at new AotPlugin (/Users/zchapple/code/wfa-ts/node_modules/@ngtools/webpack/src/plugin.js:34:14)
    at Object.exports.getWebpackAotConfigPartial (/Users/zchapple/code/wfa-ts/node_modules/angular-cli/models/webpack-build-typescript.js:57:13)
    at new NgCliWebpackConfig (/Users/zchapple/code/wfa-ts/node_modules/angular-cli/models/webpack-config.js:18:42)
    at Class.exports.default.Task.extend.run (/Users/zchapple/code/wfa-ts/node_modules/angular-cli/tasks/build-webpack.js:17:22)
    at Class.Command.extend.run (/Users/zchapple/code/wfa-ts/node_modules/angular-cli/commands/build.js:50:26)
    at Class.<anonymous> (/Users/zchapple/code/wfa-ts/node_modules/angular-cli/lib/models/command.js:152:17)
    at tryCatch (/Users/zchapple/code/wfa-ts/node_modules/rsvp/dist/lib/rsvp/-internal.js:215:12)
    at invokeCallback (/Users/zchapple/code/wfa-ts/node_modules/rsvp/dist/lib/rsvp/-internal.js:230:13)
    at publish (/Users/zchapple/code/wfa-ts/node_modules/rsvp/dist/lib/rsvp/-internal.js:198:7)
    at flush (/Users/zchapple/code/wfa-ts/node_modules/rsvp/dist/lib/rsvp/asap.js:85:5)
    at _combinedTickCallback (internal/process/next_tick.js:67:7)
    at process._tickCallback (internal/process/next_tick.js:98:9)

zackarychapple avatar Oct 25 '16 19:10 zackarychapple

Ran into the same issue. With the current configuration, auto discovery takes precedence over tsconfig.

clydin avatar Oct 25 '16 21:10 clydin

Did some more debugging on this and found that the issue is around the parsing of the main.ts file. @clydin is right in that the CLI tries to resolve first. Which it does resolve my main.ts file correctly but because I have my bootstrap module wrapped in a DOMContentLoaded block it cannot parse the bootstrapModule. Putting your bootstrap in this block breaks.

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

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

document.addEventListener('DOMContentLoaded', function () {
  platformBrowserDynamic().bootstrapModule(WfaNg2Module, []);
}, false);

zackarychapple avatar Oct 26 '16 13:10 zackarychapple

Have you found a solution/work-around? I'm experiencing a similar issue which is likely caused by a custom function wrapping the bootstrapModule call.

yuri1969 avatar Oct 31 '16 19:10 yuri1969

It would seem that the only the top-level of the main.ts file is searched for the bootstrapModule call. By allowing it to search into nested calls(change false to true) it picks it up properly, and works for me.

Source: https://github.com/angular/angular-cli/blob/master/packages/webpack/src/entry_resolver.ts#L151.

ferdis avatar Nov 09 '16 10:11 ferdis

I have a similar setup that leads to this error:

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

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

declare var fetch: any;

fetch('config.json', {method: 'get'}).then(res => {
  res.json().then(settings => {
    AppSettings.settings = settings;
    platformBrowserDynamic().bootstrapModule(AppModule);
  });
});

binarious avatar Nov 10 '16 08:11 binarious

When can we expect a fix for that to be available through NPM?. For now I manually changed the file as @uitgewis suggested.

ptomaszi avatar Nov 29 '16 15:11 ptomaszi

Even I am waiting for the fix through npm. Please update once it is available.

muralikrrish279 avatar Nov 30 '16 22:11 muralikrrish279

I get this same error. If I remove the catch handler it works.

platformBrowserDynamic().bootstrapModule(AppModule);/*.catch(err => {
    console.error(err);
});*/

pietie avatar Dec 02 '16 10:12 pietie

can confirm @pietie's issue. Seems like an issue in the entry_resolver.ts here.

Actually, if I manage to, I'll try and fix this thing. Just have to figure out how to properly build the CLI locally and run it against my broken project.

juristr avatar Dec 03 '16 17:12 juristr

Apparently..

// works
platformBrowserDynamic().bootstrapModule(AppModule);

// works
var promise = platformBrowserDynamic().bootstrapModule(AppModule);
promise.then(() => console.log('test'));

// doesn't work
platformBrowserDynamic().bootstrapModule(AppModule).then(() => console.log('test'));

juristr avatar Dec 03 '16 19:12 juristr

I got this error wen I use polymer with angular cli . to use that I had to creat main-polymer.ts

document.addEventListener('WebComponentsReady', () => {
  require('./main.ts');
}
);

and main.ts

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);

and in angular-cli.json

I have to set 'main' : 'main-polymer.ts'

How I'm gone workaround this problem

rumes avatar Dec 06 '16 12:12 rumes

Also breaks a hybrid app (angular 1 and 2) using the upgradeAdapter to bootstrap:

upgradeAdapter.bootstrap(document.body, ['app']);

scottlepp avatar Dec 08 '16 19:12 scottlepp

@rumes, in what directory did you place the main-polymer.ts file you created?

Chris-Vere avatar Dec 09 '16 04:12 Chris-Vere

@Chris-Vere /src

rumes avatar Dec 09 '16 08:12 rumes

i used @juristr 's suggestion and it worked! Thanks,

largeDachshund avatar Dec 09 '16 19:12 largeDachshund

I have the same config that @rumes and it is working fine with angular-cli: 1.0.0-beta.21, but breaking in 1.0.0-beta.22-1.

sebastientessot avatar Dec 11 '16 20:12 sebastientessot

I'm not certain this is related to the original problem, but I encountered this same error in beta.22-1 when attempting to move my app component files from the angular-cli default of src/app/app.* to src/app/containers/app/app.*.

I tracked the problem to src/app/index.ts, which exports the app component. I found that the entry component (app.component#AppComponent) must be exported from here for main.ts or it goes missing during compilation. Changing this file made the error go away.

I changed src/app/index.ts from this:

export * from './app.component';
export * from './app.module';

To this:

export * from './containers/app/app.component';
export * from './app.module';

Is it possible that other reports have also placed their entry component in a location that is not exported in the same barrel as AppModule? I still think it's a little weird that this breaks the build, since AppComponent IS imported and set as the entry component in AppModule, so it ought not be pruned during build.

johnchristopherjones avatar Dec 12 '16 03:12 johnchristopherjones

Correction: nixing the first line in index.ts also fixes this. Weirdly, I do not get a compilation error about an invalid import, just the Tried to find bootstrap code, but could not.… error.

johnchristopherjones avatar Dec 12 '16 04:12 johnchristopherjones

I have a similar setup to @binarious. The only solution that seems to work for that scenario is the suggestion by @uitgewis to change https://github.com/angular/angular-cli/blob/master/packages/%40ngtools/webpack/src/entry_resolver.ts#L133 to pass true instead of false.

bradyisom avatar Dec 12 '16 17:12 bradyisom

Recently I upgraded my angular-cli to the latest version to this date. I ran into the same error as OP while creating a migration-project. The project failed to install all the right dependencies and then I tried to start the server by angular-cli anyhow.

Resolution My fix was simply to cd into the new project folder, npm install once again (which basically installed every dependency defined in package.json) and ng build, which did the trick.

Update If you do happen to use Docker to build and serve your Angular2 application, you now have to specifically specify your container's IP address while launching your application.

ng serve --host CONTAINER_ADDRESS

kevto avatar Dec 14 '16 13:12 kevto