Services do not work in production build
- OS? Windows 10
- Versions. 1.0.0-beta.1
3.Created a sample app using angular cli and created a service under shared folder. Run ng build -prod Run ng serve -prod Get error
"Error loading http://localhost:4200/app/shared/index.js as "./app/shared" from http://localhost:4200/main.js ; Zone:
; Task: Promise.then ; Value: Error: Error: XHR error (404 Not Found) loading http://localhost:4200/app/shared/index.js(…)"
- Not happening in dev build/
Heya @unnijeevan, I tried this on 1.0.0-beta.4 and it seems to be fixed. Can I ask you to update your version and try again?
My commands were these:
ng new testproj
cd testproj
ng generate service my-test-service
ng serve -prod
Hi filip
Thank you very much for the reply,it works now but it seems prod build doesn't bundle things anymore...Previously it used to be just 4 or 5 miles bundled.Not sure how is it different from dev build now
The app itself is bundled, even though all vendor files are being copied over to dist/ - this seems to be a bug, which I'll look at in https://github.com/angular/angular-cli/issues/829.
Check your main.ts - it should have all code for vendor files bundled there. app/index.js should also have a bundle of all your app code.
Hi @filipesilva I also encountered this issue today. If I create the service in the "app/shared" folder and then add it to main.ts, I will have the same 404 error for missing shared/index.js. However, if the service is placed in the "app" folder as you did, it will build and load correctly via ng build --prod.
ng new testproj
cd testproj
ng g service shared/my-test-service
main.js
import { bootstrap } from '@angular/platform-browser-dynamic';
import { enableProdMode } from '@angular/core';
import { TestprojAppComponent, environment } from './app/';
import { MyTestServiceService } from './app/shared';
if (environment.production) {
enableProdMode();
}
bootstrap(TestprojAppComponent, [
MyTestServiceService
]);
Error:
GET http://localhost:8000/app/shared/index.js 404 (File not found)
My system:
angular-cli: 1.0.0-beta.5
node: 6.2.0
os: win32 x64
Hi @lifan0127, beta.6 should have a broader fix for that issue via https://github.com/angular/angular-cli/pull/1028, but meanwhile there is a solution in https://github.com/angular/angular-cli/issues/953.
Thanks @filipesilva I did come up with the same solution of importing this service from 'app/index.ts' which fixed the problem. Look forward to beta 6!
This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.