Incorrect JS paths when ng build -prod is deployed in a subfolder
Please provide us with the following information:
- OS?
Windows 10
- Versions: angular-cli: 1.0.0-beta.5 node: 4.4.5 os: win32 x64
- Repro steps:
- Fresh app created by "ng new datamatic"
- Then build "ng build prod"
- /dist folder deployed to http://datamatic.co/angular2-test/
- The log given by the failure:
Browser log when accessing http://datamatic.co/angular2-test/:

Files are loaded relative to the domain, not the folder
@filipesilva adding --deploy-url /static/ does add /static/ before all *.budled.js files but it doesn't do so with assets. How to solve this problem?
@rishabhiitbhu it will add it for assets referenced in css, but not for images referenced in html. The tracking issue for that is https://github.com/angular/angular-cli/issues/6666, but discussion on it can be found in https://github.com/angular/angular-cli/issues/4517.
Hey, I am trying to host a static page on github pages. using
ng build --prod --base-href "https://kartik1607.github.io/angularHeader/"
ngh
All the js and css load up fine but assets give 404.
Angular tried to find assets at https://kartik1607.github.io/assets/wallpaper.jpg instead of https://kartik1607.github.io/angularHeader/assets/wallpaper.jpg
Solved the issue using ng build --prod --base-href "https://kartik1607.github.io/angularHeader/" -d "/angularHeader" command.
Updating the base href works but when I refresh the page, I get 404
@simonatorw Are you getting 404 on bundles generated or on assets? Are you hosting your application inside some sub directory? eg: In my case subdirectory was "/angularHeader"
@filipesilva I am facing similar issue, while running ng build command it is deleting dist folder.
Angular CLI: 1.6.6 Node: 6.11.0 OS: win32 x64 Angular: 5.2.5 ... animations, common, compiler, compiler-cli, core, forms ... http, language-service, platform-browser ... platform-browser-dynamic, router
@angular/cli: 1.6.6 @angular-devkit/build-optimizer: 0.0.42 @angular-devkit/core: 0.0.29 @angular-devkit/schematics: 0.0.52 @ngtools/json-schema: 1.1.0 @ngtools/webpack: 1.9.6 @schematics/angular: 0.1.17 typescript: 2.5.3 webpack: 3.10.0
the error is...
chunk {inline} inline.bundle.js, inline.bundle.js.map (inline) 5.83 kB [entry] [rendered] chunk {main} main.bundle.js, main.bundle.js.map (main) 303 bytes [initial] [rendered] chunk {polyfills} polyfills.bundle.js, polyfills.bundle.js.map (polyfills) 323 bytes [initial] [rendered] chunk {styles} styles.bundle.js, styles.bundle.js.map (styles) 11.4 kB [initial] [rendered]
ERROR in TypeError: Cannot read property 'loadChildren' of undefined at _collectLoadChildren (C:\Users\ANIMESH\Desktop\notes\ngApp\node_modules@angular\compiler\bundles\compiler.umd.js:29034:20) at listLazyRoutes (C:\Users\ANIMESH\Desktop\notes\ngApp\node_modules@angular\compiler\bundles\compiler.umd.js:29009:49) at visitLazyRoute (C:\Users\ANIMESH\Desktop\notes\ngApp\node_modules@angular\compiler\bundles\compiler.umd.js:31176:47) at AotCompiler.listLazyRoutes (C:\Users\ANIMESH\Desktop\notes\ngApp\node_modules@angular\compiler\bundles\compiler.umd.js:31144:20) at AngularCompilerProgram.listLazyRoutes (C:\Users\ANIMESH\Desktop\notes\ngApp\node_modules@angular\compiler-cli\src\transformers\program.js:156:30) at Function.NgTools_InternalApi_NG_2.listLazyRoutes (C:\Users\ANIMESH\Desktop\notes\ngApp\node_modules@angular\compiler-cli\src\ngtools_api.js:44:36) at AngularCompilerPlugin._getLazyRoutesFromNgtools (C:\Users\ANIMESH\Desktop\notes\ngApp\node_modules@ngtools\webpack\src\angular_compiler_plugin.js:248:66) at Promise.resolve.then.then (C:\Users\ANIMESH\Desktop\notes\ngApp\node_modules@ngtools\webpack\src\angular_compiler_plugin.js:565:50) PS C:\Users\ANIMESH\Desktop\notes\ngApp>
developers typically (at least I do for convenience and just to have a tomcat server running with ability to go there and launch any project of convenience from anywhere from home office or mobile phone) develop a project off of tomcat root here is the path
C:\TOMCAT\WEBAPPS\ROOT\ngapp
angular-cli bundles and builds out to
C:\TOMCAT\WEBAPPS\ROOT\ngapp\dist
thats where index.html and all the bundles are located
furthermore an assets dir lives under there too
C:\TOMCAT\WEBAPPS\ROOT\ngappt\dist\assets
I run my tomcat on 8080
http://localhost:8080/ngapp
app runs but paths to assets and bundles are frigged
setting
<base href="">
results in weird paths to bundles and assets
I am seeing (depending on what we set href to) paths ahead or after resulting in inaccessible paths to bundles and assets.
currently my app runs but cannot retrieve assets no matter what I try
currently
if I set
<base href="/ngapp/dist">
my bundles are not found 404
if I set
<base href="/ngapp/dist/">
my bundkles are found and app runs but cannot find assets with 404 on all assets
???
Saved my day.
Is there a way to avoid setting the base-href in the ng build? I'm working with a project and the war file can be renamed to whatever. So I don't know what is the real base href. It will be so helpful to avoid it and still load the fonts, because there is no way to know the project name. Thanks
@filipesilva you're brighter then a brain pie! Thank you so much - Was troubleshooting my Node Express + Angular6 app and for the life of me couldn't figure out why /app references were all routing to my 404 handler. base-href was it! 💯
Github!! Code Savers thanks @filipesilva
@filipesilva your solution saved me. Thanks a lot!!
Am getting the same issue of following
assets/img/brands/max.png 502 (Bad Gateway)
all the assets folder taking root path. Other files are working as expected. Please help me out.
Versions are: Angular CLI: 6.0.8 Node: 8.11.3 OS: darwin x64 Angular: 6.1.1 ... animations, common, compiler, compiler-cli, core, forms ... http, language-service, platform-browser ... platform-browser-dynamic, router, service-worker
Package Version
@angular-devkit/architect 0.6.8
@angular-devkit/build-angular 0.6.8
@angular-devkit/build-optimizer 0.6.8
@angular-devkit/core 0.6.8
@angular-devkit/schematics 0.6.8
@angular/.DS_Store
When you're deploying to non-root path within a domain, you'll need to manually update the
<base href="/">tag in yourdist/index.html.In this case, you will need to update to
<base href="/angular2-test/">I believe.
yeah its working but when i refresh a page then through page not found error
For anyone still having issues with assets, you should reference your images etc using a relative path e.g "./assets/image-1.png". This resolved my issue.
I can confirm what @tyronboyd said. There are three ways to get this done with both assets and all other files i tried.
Nr 1: Change index.html default
<base href="/">
to
<base href="./">
Nr 2: You can run:
ng build --prod --base-href ./
Nr 3: You can change your build process, found in angular.json, to automate this by adding:
"baseHref": "./"
To your production configuration.
Obviously you can use fixed paths as well with either of these, but adding this single dot to the default uses relative paths, which I would guess is the default behavior one would expect. No idea why absolute paths are still a thing in something as advanced as Angular, but oh well. :-)
@MarcoHeumann had a similar issue fixed it by changing the index.html default to
To understand this, I urge you to just look at the error when you don't do the change. It will show whatever file you tried to access in a wrong folder. The base Path seems to be defined "somewhere else" as well and unless you overwrite it, it seems to make zero sense (at least to me).
Basically the absolute path is wrong, like all the time, while making it relative gives you full control over your structure, while also working as expected. We have come to expect relative paths everywhere and that is for a reason, but a reason Angular does not seem to follow.
Hello, I tried all the solutions but to no avail. My Angular and Backend application both are packaged in one jar. So, is that the reason it is not working or something else.?