Developing an Angular application and deploying it onto the server

I've been working on an angular6 application and I need to create a build to test it on my server. Currently, when I use ng server, the application runs without any errors in my browser.

c:\Users\emiry\Desktop\Angular\Projects\StartingNewProject

After running the ng build command, the build is generated in the /dist folder without any errors.

On my local machine, I have Wamp64 installed, so I take the build generated by ng and place it in my www folder:

C:\wamp64\www\StartingNewProject

Would this setup be sufficient for me to run the application on the server?

However, when I try to access the application on the server through http:// localhost/startingnewproject/, I encounter the following error in the browser console:

Failed to load resource: the server responded with a status of 404 (Not Found) polyfills.js:1 Failed to load resource: the server responded with a status of 404 (Not Found) styles.js:1 Failed to load resource: the server responded with a status of 404 (Not Found) vendor.js:1 Failed to load resource: the server responded with a status of 404 (Not Found) main.js:1 Failed to load resource: the server responded with a status of 404 (Not Found)

angular.json

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  // Remaining content omitted for brevity
}

package.json

{
  "name": "starting-new-project",
  
  // Remaining content omitted for brevity
}

I attempted to access the site using the following paths:

file:///C:/wamp64/www/StartingNewProject/index.html

5index.html:13 GET file:///C:/runtime.js 0 () /C:/favicon.ico:1 GET file:///C:/favicon.ico 0 () translate.google.com/gen204?nca=te_li&client=te_lib&logld=vTE_20180625_00:1 GET file://translate.google.com/gen204?nca=te_li&client=te_lib&logld=vTE_20180625_00 0 ()

file:///C:/wamp64/www/StartingNewProject/index.html

GET file:///C:/runtime.js 0 () /C:/favicon.ico:1 GET file:///C:/favicon.ico 0 ()

While I can easily access the root directory to view folders and files, the problem arises specifically when loading the index.html file.

Answer №1

The main challenge the project is currently facing involves locating its dependencies. Your Angular project's index.html file should include a tag that resembles:

<base href="/">

This indicates that when searching for dependencies, it should start from the root. However, in your situation, they are not located directly at the root (e.g., localhost), but rather within StartingNewProject.

To resolve this issue, you can create a new build using

ng build --prod --base-href=/StartingNewProject/
, which will automatically add the base href tag for you. Remember to include both the leading and trailing slashes.

For more information, refer to the Angular documentation available here

Answer №2

Dealing with a similar issue led me to discover a solution by implementing ./.

<base href="/"> was changed to <base href="./">

Make sure to rebuild and deploy once again.

Answer №3

When using Angular 11, you might encounter a "deprecated" warning.

ng build --prod --base-href=/StartingNewProject/

To resolve this issue, make adjustments to your angular.json file.

Within the angular.json file, add a "baseHref" property as shown below:

 "projects": {
   "your-project-name" : {
          "architect": {
             "build": {
               "options": {
                   "baseHref": "/your-relative-path/"

This change will update the URL in both production and development environments.

Remember to always use relative paths in your links, like this (instead of "/"):

 <img class="top-bar__logo" src="./assets/images/picture1.svg" />

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Creating custom components in AngularJS 2 allows you to define methods unique to each component. Want to learn

I created my component (A) by combining multiple html elements, but I have two questions. How do I define methods (such as get, etc.) on my component? I have tried @Output, @ViewChild, etc. but they are not working. I am looking for an alternative way ...

Internationalization of deferred-loaded modules within the JHipster application

I created My App using JHipster, which utilizes the JhiLanguageService in the ng-jhipster library. This service relies on the JhiConfigService to configure ngx-translate without requiring manual imports and configuration of the TranslateModule in my app.mo ...

Serve a customized directory in production with Next.js

The issue at hand: Whenever I execute the command next build on my live server, the page appears unstyled during the building process. What I aim to achieve: Compile the application into a directory named build Transfer the contents of build to a director ...

With Next.js14, an error occurs when building with Nodejs20. The application runs smoothly on localhost, but encounters errors during the deployment process, resulting in a 500 internal server error

I recently updated my Node.js version to the latest, which is version 20. After that, I created a new project using Next.js. While it runs fine on my localhost, I encountered errors during the build process even though I didn't change any code. I&apos ...

Sending various data from dialog box in Angular 8

I have implemented a Material Design dialog using Angular. The initial example had only one field connected to a single parameter in the parent view. I am now trying to create a more complex dialog that collects multiple parameters and sends them back to t ...

The ngIf directive in Ionic 2 does not refresh after a user logs in

I'm facing an issue with the *ngIf directive in Ionic 2. Below is my code: <div *ngIf="isLogin" class="profile-info ng-binding padding text-center" (click)="openPage(accountPage)"> {{userEmail}} <span menu-toggle="menu-togg ...

Streamlining the deployment process of Angular applications on IIS through automation tools such as Docker and Jenkins

I am currently manually deploying my Angular application on an IIS server by copying the build from my system to a specific location on the server. The code for my application is stored in a TFS repository. My application's backend is powered by Mul ...

Improving event observation efficiency with Observable.fromEvent on the browser window

Within my file manager UI, each individual file item is currently set to monitor the window's wheel event. As soon as a file item comes into view on the page, its corresponding image loading process will be initiated by the file item component. While ...

angular pipe and tap methods fail to execute the designated function

I am encountering a problem when calling a function in my login service. I have tried using a pipe and tap. Interestingly, when I use res => console.log(res), it outputs the desired result. However, when I attempt to call a function, it seems that the ...

How can Angular hide a global component when a particular route is accessed?

Is it possible to hide a global component in Angular when a specific route is opened? For instance, consider the following code: app.component.html <app-header></app-header> <app-banner></app-banner> <!-- Global Component I w ...

Having difficulty resolving all parameters for the component: (?, [object Object]) in the Jasmine component Unit Test

While defining a UT for a component with an extended class using i8nService and ChangeDetectionRef, I encountered an error preventing me from instantiating it: Failed: Can't resolve all parameters for BrandingMultiselectComponent: (?, [object Object] ...

ErrorHookWebpack: When executing npm build in the node container, the service is detected as inactive

Encountering an issue when executing npm run build within a container on Kubernetes during a gitlab-ci job. It's worth noting that npm install, npm run lint, and npm run test all completed without any problems. Error: node@runner-7gbsh-sz-project-966 ...

Issue with PrimeNG Calendar month picker functionality not functioning as expected

I recently integrated a PrimeNG code snippet for a month picker. Although it is functioning correctly, I noticed a discrepancy compared to the PrimeNG example - specifically, the year does not change when clicking on the arrow buttons. The ngModel, howev ...

A guide to submitting forms within Stepper components in Angular 4 Material

Struggling to figure out how to submit form data within the Angular Material stepper? I've been referencing the example on the angular material website here, but haven't found a solution through my own research. <mat-horizontal-stepper [linea ...

What is the best way to reset a dropdown list value in angular?

Is there a way to erase the selected value from an Angular dropdown list using either an x button or a clear button? Thank you. Code <div fxFlex fxLayout="row" formGroupName="people"> <mat-form-field appearance=&quo ...

I am disappointed with the lack of functionality in Angular's HTML type inference

When working inside an Angular component, I want to select a div element by id or class. This method works menuDisplayDiv = document.getElementsByClassName("some_class")[0] as HTMLDivElement; menuDisplayDiv = document.getElementById("some ...

Facing issues while trying to deploy my Angular 5 application on Heroku

I've encountered an issue while attempting to deploy my Angular 5 project on Heroku. While I've successfully deployed other projects before, this one seems to have a dependency problem that is hindering the process. Locally, running ng build pos ...

Angular 2 - Changes in component properties not reflected in view

I'm currently delving into Angular 2 and as far as I know, interpolated items in the view are supposed to automatically update when their corresponding variable changes in the model. However, in the following code snippet, I'm not observing this ...

Ensuring the completion of all validations in Angular 4

Currently, I'm working on a project that involves 3 FormControls. I am in need of a validation process that ensures either all three have values selected or none at all. Is there a method to achieve this type of validation? ...

Access SCSS variable values in Angular HTML or TypeScript files

So, I've been looking into whether it's feasible to utilize the SCSS variable value within HTML or TS in Angular. For instance: Let's say I have a variable called $mdBreakpoint: 992px; stored inside the _variable.scss file. In my HTML cod ...