How can paths be made relative to the project's root in Angular 2?

My Visual Studio MVC project contains a systemjs.config.js file in the Scripts folder at root/Scripts/systemjs.config.js.

(function (global) {
  System.config({
    paths: {
      // paths serve as alias
      'npm:': 'libs/'
      },
    // map tells the System loader where to look for things
    map: {
      // our app is within the app folder
      app: '/Scripts/app',

      // angular bundles
      '@angular/core': 'npm:@angular/core/bundles/core.umd.js',
      '@angular/common': 'npm:@angular/common/bundles/common.umd.js',
      '@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.js',
      '@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.js',
      '@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js',
      '@angular/http': 'npm:@angular/http/bundles/http.umd.js',
      '@angular/router': 'npm:@angular/router/bundles/router.umd.js',
      '@angular/forms': 'npm:@angular/forms/bundles/forms.umd.js',

      // other libraries
      'rxjs':                      'npm:rxjs',
      'angular-in-memory-web-api': 'npm:angular-in-memory-web-api/bundles/in-memory-web-api.umd.js'
    },
    // packages tells the System loader how to load when no filename and/or no extension
    packages: {
      app: {
        defaultExtension: 'js'
      },
      rxjs: {
        defaultExtension: 'js'
      },
      '.': {
          defaultJSExtensions: 'js'
      }
    }
  });
})(this);

All Angular JavaScript files are located in Scripts/app, while modules are in libs.

In development, my website URL is www.mywebsite.com/firstpage, but in production it is www.mywebsite.com/user/firstpage.

The Angular app is loaded in the master layout of the page using:

<!-- Load libraries -->
    <script src="~/libs/core-js/client/shim.min.js"></script>
    <script src="~/libs/zone.js/dist/zone.js"></script>
    <script src="~/libs/systemjs/dist/system.src.js"></script>

    <!-- Configure SystemJS -->
    <script src="~/Scripts/systemjs.config.js"></script>
    <script>
        System.import('@Url.Content("~/Scripts/app/main")').catch(function (err)
        {
            console.error(err);
        });
    </script>

Angular loads fine on the home page locally, but I get a "main.js not found" error on the production home page.

www.mywebsite.com/user/Scripts/app/main 404 (Not Found)

Adjusting URLs allows me to load the main.js file, but the libs files still give 404 errors because they are looking from mywebsite.com instead of mywebsite.com/user in production.

How can I make the paths work consistently from the root of the website in both production and local environments?

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

When sending a Post Request in Angular to a Spring Boot backend, a status code of 500 is

Encountering a 500 status code error while making the request below: createPost(postPayload: CreatePostPayload): Observable<any> { return this.http.post('http://localhost:8080/api/posts/', postPayload); } The request is successful wh ...

Error: Trying to dispatch from an undefined property in Angular and Redux

In the process of developing a Shopping app using Angular + Redux, I encountered an issue. When attempting to trigger the "ADD_PRODUCT" action on click through a dispatcher function, I keep running into the error message: ERROR TypeError: Cannot read prop ...

Adjust the appearance attribute of FormField within the designated theme

Currently, I am collaborating within an NX Monorepo and utilizing a shared ui-components library that extends the capabilities of Angular Material Components. Despite using different themes for various applications, I am aiming to incorporate appearance=&a ...

Guide on organizing a multi-dimensional array of objects based on property value using javascript?

I am faced with the challenge of sorting a multidimensional array based on values, but the selection is dependent on the parentID. This is my current array: const result = [ [{value: 123, parentID: 1}, {value: 'string123', parentID: 2}], [{ ...

Encountering an error in Angular 12 with DomSanitizer when setting the src attribute dynamically for an iframe. The issue arises when trying to set a

Having trouble creating a component to handle YouTube embedded videos? It seems like passing the src as a variable isn't working properly, no matter what is tried. Does anyone have any ideas on what might be going wrong, or if it's a bug in Angul ...

Adding a fresh attribute to MongoDB using push model

I wrote a basic script that calculates the number of documents for a specific user and assigns it to externalUser.totalIcons. However, when I try to execute it, the model is not saved and the new property is not added to the database. My question is: wher ...

Can Angular Universal SSR be activated specifically for Googlebot User Agents?

I am aiming to activate Angular Universal SSR only when the User Agent is identified as Googlebot. However, I am uncertain about how to instruct Angular Universal SSR to deliver server side rendered HTML exclusively if the User Agent is Googlebot. server ...

The code continues to load content within a new page

Having an issue with my MVC application. I want to click on ActionLink, load data from the server, and insert it into a div on the same page. However, my code is still loading content on a new page for some reason. Can anyone help me figure this out? This ...

What Causes CORS Errors When Sending Encoded Information?

Dealing with poor data quality is a challenge I face. An example of this is when the primary key value of a table contains spaces and special characters, like 35581/H0034-100003 My goal is to retrieve a record by its primary key value using a GET request ...

Strategies for handling HTML content in the getProduct API call using Angular resolve

Components and Services Setup export class ProductComponent implements OnInit { constructor( private route: ActivatedRoute, private router: Router, ) { } ngOnInit() { this.route.data .subscrib ...

The Radio Button's value appears in a distinct way on Ionic Angular

I am currently working with the Ionic framework and I am trying to display data values on radio buttons. However, I am facing difficulties in retrieving the correct value and setting it appropriately. index.html <td> <label>{{learn ...

Error encountered during production build of Angular 6 Universal with server-side rendering (SSR

I have recently delved into Angular 6 and managed to nearly complete my app. However, I am encountering some struggles with server-side rendering (SSR). Following a tutorial on SSR at this link, I tried running the command npm run build:ssr && npm ...

What is the best method for accessing all the projected elements within a Component?

What is the best approach for accessing all projected elements within a Component? Consider an example where AppComponent projects various links and images into TestComponent: @Component({ selector: 'test', template: '<ng-content> ...

typescript mistakenly overlooked a potential undefined value in indexed records

In my code, I have defined an index-based type X. However, when using a non-existing key, TypeScript does not accurately infer the result type as ValueType | undefined. Is there a solution to correct this issue? type ValueType = { foobar:string; } t ...

Difficulty in both passing a value and accessing a child component

I am currently working on a form that includes various elements such as Company, Contact, and Date. Additionally, there is a custom component, a date picker, that is included in the form. When I fill out all the values in the form and submit it, I am able ...

The attribute 'elements' is not present within the data type 'Chart'

var canvas = document.getElementById("canvas"); var tooltipCanvas = document.getElementById("tooltip-canvas"); var gradientBlue = canvas.getContext('2d').createLinearGradient(0, 0, 0, 150); gradientBlue.addColorStop(0, '#5555FF'); grad ...

The image is malfunctioning in the production environment, but functions perfectly on the localhost server

Currently, I am in the process of creating a website utilizing Next.js and Mantine. In order to incorporate my logo into the Header section, I utilized the Image component from next/image. Unfortunately, upon deployment, the image does not display as inten ...

Angular 2 introduces one-way binding, which allows for modifications to be applied to both objects

How does one-way binding modify both objects? <component2 [obj]="obj1"></component2> Is there a way to make changes to obj without affecting obj1? It seems that only duplicating obj solves this issue. Is there another method in angular2 to a ...

Is it a commonly recommended method to nest fxLayout containers?

Recently, I ventured into using Angular Flex for the first time and I find myself a bit unsure about the nesting of layout containers. The issue arises when dealing with a navigation tag that triggers an angular-material sidenav opening to the left, pushin ...

Identifying and resolving each console error related to an array list while using a

ERROR TypeError: Cannot read property 'forEach' of undefined Hello, I'm encountering this error message. I've written a function that should translate an array based on JSON path. What could be causing this issue? menu = [ {tit ...