Setting up SystemJS with Angular 2 on Visual Studio 2015

I am currently working on integrating the ng2-material components into my Angular2 app. However, I am facing some challenges with setting up the SystemJS configuration correctly. Upon running the app, Chrome's console displays the following errors:

GET http://localhost:12630/ng2-material/all 404 (Not Found)

as well as

angular2-polyfills.js:332 Error: Error: XHR error (404 Not Found) loading http://localhost:12630/ng2-material/all
    at XMLHttpRequest.wrapFn [as _onreadystatechange] (http://localhost:12630/node_modules/angular2/bundles/angular2-polyfills.js:771:30)
    at ZoneDelegate.invokeTask (http://localhost:12630/node_modules/angular2/bundles/angular2-polyfills.js:365:38)
    at Zone.runTask (http://localhost:12630/node_modules/angular2/bundles/angular2-polyfills.js:263:48)
    at XMLHttpRequest.ZoneTask.invoke (http://localhost:12630/node_modules/angular2/bundles/angular2-polyfills.js:431:34)
Error loading http://localhost:12630/ng2-material/all as "ng2-material/all" from http://localhost:12630/app/app.component.js

The application functions properly until I attempt to reference anything from the ng2-material module, at which point these errors occur. Here is the relevant code snippet:

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <title>Testing</title>

    <script src="node_modules/es6-shim/es6-shim.min.js"></script>
    <script src="node_modules/systemjs/dist/system-polyfills.js"></script>
    <script src="node_modules/angular2/es6/dev/src/testing/shims_for_IE.js"></script>

    <script src="node_modules/angular2/bundles/angular2-polyfills.js"></script>
    <script src="node_modules/systemjs/dist/system.src.js"></script>
    <script src="node_modules/rxjs/bundles/Rx.js"></script>
    <script src="node_modules/angular2/bundles/angular2.dev.js"></script>

    <!-- 2. Configure SystemJS -->
    <script>
      System.config({
        packages: {
          app: {
            format: 'register',
            defaultExtension: 'js'
          }
        }
      });
      System.import('app/main')
            .then(null, console.error.bind(console));
    </script>

</head>
<body>
    <hud>Loading...</hud>
</body>
</html>

main.ts

import {bootstrap} from 'angular2/platform/browser';
import {AppComponent} from './app.component';

bootstrap(AppComponent);

app.component.ts

import {Component} from 'angular2/core';
import {MATERIAL_DIRECTIVES} from 'ng2-material/all';

@Component({
    selector: 'hud',
    template: `
      <button md-raised-button class="md-raised md-primary">Primary</button>
    `
    directives: [MATERIAL_DIRECTIVES]   //uncomment this line to get it to work
})

export class AppComponent {

}

The line

import {MATERIAL_DIRECTIVES} from 'ng2-material/all';

does not show any syntax error indication for 'ng2-material/all' in TypeScript, indicating that it can locate that part successfully. However, upon using MATERIAL_DIRECTIVES, an error occurs. This leads me to believe that there may be an issue with the SystemJS configuration. Below is the structure of my files:

Root:
|-app
|----app.component.ts
|----main.ts
|-Scripts
|----[some nuGet package stuff]
|-node_modules
|----[all npm packages: ng2-material is located in here]
|-index.html
|-package.json
|-tsconfig.json

EDIT:

package.json

   {
    "version": "1.0.0",
    "name": "ASP.NET",
    "private": true,
  "devDependencies": {
    "angular2": "2.0.0-beta.12",
    "systemjs": "0.19.24",
    "es6-promise": "^3.1.2",
    "es6-shim": "^0.35.0",
    "reflect-metadata": "0.1.2",
    "rxjs": "5.0.0-beta.2",
    "zone.js": "^0.6.6",
    "ng2-material": "0.3.4"
  }
}

Answer №1

To properly set up ng2-material in your SystemJS configuration, follow these steps:

<script>
  System.config({
    packages: {        
      app: {
        format: 'register',
        defaultExtension: 'js'
      },
      'ng2-material': {
        format: 'register',
        defaultExtension: 'js'
      }
    },
    map: {
      'ng2-material': 'node_modules/ng2-material/source'
    }
  });
  (...)
</script>

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

Guarding against Typescript type errors when passing a parameter to a function by using a type guard

Apologies for the misleading title. I'm attempting to implement a lookup type similar to the setProperty example found in https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-1.html#keyof-and-lookup-types The lookup type is correc ...

Steps for Adding a JS file to Ionic 3

I'm trying to figure out how to access a variable from an external JS file that I included in the assets/data folder. Here's what I've attempted: I placed test.js in the assets/data folder. In test.js, I added a variable testvar = "hello ...

Improving memory management in Angular by utilizing NgRx store for efficient memory usage and preventing potential

I am currently working on an enterprise application using Angular 11. Our application heavily relies on the NgRx store, and I am interested in optimizing our memory footprint (which is currently around ~200-220 MB) and live JS Heap size (~76-85 MB). Ensur ...

Updating my Angular application using `ng update` did successfully get it to version 11.0.0-next.6. However, I am aiming to revert back

One of my clients has a project using Angular version 8.x.x. I am keen on updating it to the stable version 10.x.x for better performance and features. I followed all the steps mentioned in the Angular update instructions page: https://update.angular.io/? ...

Sorting an Observable by date in Angular components can be accomplished by utilizing the appropriate methods to define whether the sorting should

I'm trying to figure out how to sort the data by date within my array in the component. Here is the current structure: Template: <div> <button (click)="sortByDate()"> Sort by date </button> </div> <div *ngFor="let ...

Angular NgClass Issue (Unable to bind to 'ngClass' as it is not recognized as a property of 'a')

Hi there! I am relatively new to using Angular and I am currently facing an issue while attempting to dynamically add a Bootstrap class to my HTML based on the active tab. Unfortunately, I am encountering an error. Can anyone provide assistance? The error ...

Display the list of cities associated with the country chosen in the form

I am currently working with the repository found at https://github.com/country-regions/country-region-data/blob/master/data.js to create a mapping of countries and their related cities. I'm seeking guidance on how to achieve this task effectively. My ...

Utilizing Angular to parse a JSON string generated with json.dumps

I am having trouble finding a solution that works for me. Currently, I am using python 3.6 (Django Rest Framework) on the server side and Angular 5 on the client side. This is the code on the server: class TypesView(APIView): def get(self,request): ...

Android PWA - When app is pinned, pressing the back button causes it to crash

Our team created a cutting-edge progressive web app using Angular that is easily installable and runs smoothly on mobile devices. We have a specific requirement for our customers to be able to pin the PWA on their Android tablets, ensuring that the app st ...

Angular 2 file upload encountering CORS issue leading to 401 unauthorized error

Encountered a "401 Unauthorized" error in Chrome and Firefox while attempting to upload files using angular 2 CLI to an apache2-server with a PHP backend. Despite trying three different node modules, the issue persists from the OPTIONS-preflight stage, ...

Encountered a TypeError when using Angular 2 with ng-bootstrap and NgbTabset: Uncaught (in promise) error due to inability to read property 'templateRef'

I am attempting to use NgTabset but encountering an error that reads TypeError: Cannot read property 'templateRef' of undefined. Strangely, when I replace the ng-template with the template tag, everything works smoothly. Can anyone help me identi ...

How to require 2 out of 4 input fields in Angular using typescript

exploring the intricacies of design decisions, sub systems, frameworks, and libraries Hello! I am relatively new to Angular and have encountered a puzzling issue that I can't quite figure out. I could easily create a lengthy if statement to address i ...

Is it better to import from a <variable> rather than a hardcoded string in TypeScript?

https://www.example.com/typescript-dynamic-import Can Typescript handle dynamically setting the import path into a variable? For example, can we transform this: import {HomeComponent} from './dashboard/home/home.component'; Into something lik ...

Exploring Typescript: Combining types (rather than intersecting them)

Let's analyze the scenario below type MergeFn = <K1 extends string, V1, K2 extends string, V2>( k1: K1, v1: V1, k2: K2, v2: V2 ) => ??? let mergeFn: MergeFn // actual implementation doesn't matter for this question What should b ...

How to trigger the external opening of a md-select in Angular 2

Is there a way to trigger the opening of an md-select menu from a button using code similar to this? <md-select placeholder="Cuestionario" (ngModelChange)="set_survey($event)"> <option [ngValue]='undefined' disabled selected>Selec ...

Obtaining an array of objects filled with values, but encountering issues when attempting to access them resulting in undefined values

I'm encountering an issue with an array of objects that appear as non-null in debug mode, but are showing up as undefined when trying to access them. Here's a more detailed explanation: Here's the code snippet: export interface ICollectPoin ...

Angular2 with Webpack causes duplication of styles in the header

I'm currently working on integrating Angular2 with universal + webpack, but I have encountered an issue where styles are being loaded twice in the head element. I haven't made any changes to the git repo that I forked from. You can find it here: ...

Retrieving the user's choice from a list generated by ngFor

After attempting to implement this code, I'm running into some issues. It seems like I might be missing a key step. HTML: <ng-container *ngFor='let parts of parts$'> <a class="list-group-item list-group-item-action" ...

Tips for ensuring that an HTTP request receives a response before moving on to the next step in Angular 7

I am currently developing an app where I need to make a GET request to retrieve data from a component. The issue I am facing is that the next step in the process is being executed before I receive a response from the service. Below is my service file: ...

Is it possible for entities to transition between different use cases by means of the controller?

Currently, I am tasked with developing a route for processing ticket purchases for events. My approach involves utilizing a controller to handle incoming requests containing order data, client details, ticket information, and payment specifics. These reque ...