Using System.import in my code is triggering a cascade of errors in my console

I incorporate the System module in my component.

export class LoginComponent {
    constructor() {
        System.import('app/login/login.js');
    }
}

The file loads successfully, however, TypeScript compiler raises an error

Error:(10, 9) TS2304: Cannot find name 'System'.

In addition, my browser console displays

EXCEPTION: Error: Uncaught (in promise): Error: Error: http://localhost:3000/app/login/login.js detected as register but didn't execute.
        at ZoneDelegate.invoke (http://localhost:3000/node_modules/angular2/bundles/angular2-polyfills.js:332:29)
        at Object.NgZoneImpl.inner.inner.fork.onInvoke (http://localhost:3000/node_modules/angular2/bundles/angular2.dev.js:2111:31)
        at ZoneDelegate.invoke (http://localhost:3000/node_modules/angular2/bundles/angular2-polyfills.js:331:35)
        at Zone.run (http://localhost:3000/node_modules/angular2/bundles/angular2-polyfills.js:227:44)
    Error loading http://localhost:3000/app/login/login.js

Error: Uncaught (in promise): Error: Error: http://localhost:3000/app/login/login.js detected as register but didn't execute.(…)

What steps can I take to resolve this issue?

Answer №1

An issue arises due to the lack of typings for the SystemJS library.

Typically, we don't explicitly use System.import in TypeScript files since it can be handled behind the scenes. For example, when you write code like this, the TypeScript compiler will automatically convert the import statement into something compatible with SystemJS:

import {...} from 'app/login/login';

export class LoginComponent {
  constructor() {
  }
}

You may also notice that I have removed the js extension because SystemJS allows customization of the default extension for resolving modules:

<script>
  System.config({
    packages: {        
      app: {
        format: 'register',
        defaultExtension: 'js'
      }
    }
  });

Answer №2

How can I utilize ES6 syntax to import my .js file?

Here's a simple two-step process:

  • Start by declaring the JS file in your vendor.d.ts

    declare module 'app/login/login' { var foo:any; export = foo; }

  • Next, integrate it into your TypeScript code

    import * as login from 'app/login/login';

Finally, compile your code using '--module system' 🌹

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

Retrieve the previous value of the selection change with the Mat Select function

In my current project, I have a form with a mat-select dropdown. Whenever the user makes a selection, a dialog pops up to confirm the choice. However, I am facing an issue: The selectionChange() event is triggered when the value changes and provides the n ...

extract objects from an array based on a specific property that meets a given condition

Currently, I am facing the challenge of filtering an array of objects based on a specific property that matches what the user has entered. Here is the array I am working with: peopleList = [ {name: "john bee", age:23}, {name: "john woo", age:43}, ...

I'm experiencing an issue in Angular 2 where I am unable to call a function within another function

I am working with an Angular 2 client and have created an ngOnInit function in the component.ts file. ngOnInit(){ var grid = document.getElementById('devpro'); grid.addEventListener('selected-items-changed', function() ...

What causes the cursor in an editable div to automatically move to the front of the div?

<div className="min-w-[600px] min-h-[36.8px]" > <div id={`editableDiv-${Object.keys(item)}-${index}`} className="p-3" contentEditable suppressContentEditableWarning onInput={(e) => onChange(e)} > ...

Design a unique login portal separate from all other website pages

I'm a beginner with Angular and I'm currently working on creating a login page. My issue is that I want the login page to be the only component displayed initially, and once the response is 200, I want to redirect to another component. Here&apos ...

How can you determine the number of times a particular digit appears around a specific position in a TypeScript array

(Utilizing Typescript for assistance) I am looking to determine the number of occurrences of the digit 2 surrounding a specific position within an array. The function takes in the position coordinates as parameters - param 1 for row and param 2 for column. ...

Looking to organize, refine, and establish a default value with the help of rxjs

In my Angular application, I have an observable that is linked to a reactive forms dropdown control. My goal is to filter, sort, and display the default value. I've created two separate implementations - one for filtering and sorting without displayin ...

Error encountered by Angular's Injector in the AppModule when attempting to access the HttpHandler component

I have been successfully running an app for the past few months with no issues. Now, I am exploring the idea of moving some common services into a library that can be utilized by other applications. For this project, I decided to avoid using Angular CLI t ...

Angular 15 is unfortunately not compatible with my current data consumption capabilities

I'm currently facing an issue with Angular 15 where I am trying to access the "content" element within a JSON data. However, when attempting to retrieve the variable content, I am unable to view the elements it contains. import { Component, OnInit } ...

The response of the Typescript Subscription function

I'm struggling with retrieving the subscribe array in NG2. Being new to typescript, I find it difficult to understand how to pass variables between functions and constructors. This is what my code currently looks like: export class RosterPage exten ...

Develop a novel object framework by merging correlated data with identical keys

I am trying to organize the related data IOrderData by grouping them based on the productId and brandId. This will help create a new array of objects called IOrderTypeData, where the only difference between the objects is the delivery type. Each product id ...

Saving a user with a BLOB avatar in Angular 5: Tips and Tricks for Success

As a newcomer to Angular, I am trying to figure out how to properly save a new user with an avatar. Can someone help me understand how to pass the Blob value of the avatar to my user Model for successful saving? Below is the code snippet I am working with ...

Implementing ng-bootstrap popovers using dynamic data from a database

I am looking to incorporate nb-bootstrap popovers for specific text segments retrieved from a database. While I have successfully integrated ng-popovers in other parts of the code base within the .ts files, attempting to do so with content sourced from th ...

Is there a way to revert my Ionic CLI back to the previous version that I had installed?

Having just updated to version 3.2.0, I am encountering numerous issues, such as the malfunctioning of the ionic serve command. ...

Ways to prevent the use of the JavaScript increment (++) or decrement (--)

I have created two functions for a multi-step configuration on a webpage. protected clickNext(event:any, config:any) : any{ this.activeIndex++; } protected clickPrev(event:any, config:any) : any{ this.activeIndex--; } Here are the buttons: < ...

Guide on converting enums in Angular 6

I have been working on translating enums in Angular, but I'm facing a challenge. While I can read and display them in a dropdown menu, translating them is proving to be difficult. Here is an example of my code: export enum test { test1 = '1 - ...

What is the reason behind the absence of compile time errors when using 'string' functions on an 'any' field type variable in TypeScript?

Looking at the following typescript code snippet: let a; a = "number"; let t = a.endsWith('r'); console.log(t); It is worth noting that since variable 'a' is not declared with a specific type, the compiler infers it as ...

Performing unit tests in Angular 2 with karma

Trying to grasp the concept of unit testing in Angular has been quite a challenge for me, especially since I am still navigating through Angular 2 and its syntax. Understanding testing becomes even more intricate. I attempt to follow the examples provided ...

Animating with Angular 2

As I delve into this informative resource, my goal is to incorporate some animations into my applications, but I find myself grappling with understanding how the animations are triggered. HTML Component <div class="navbar navbar-default navbar-fixed-t ...

How can I display 4 react components, such as custom buttons, in a manner that ensures the most recently pressed button appears on top?

I've been attempting to solve this problem, but I'm struggling to find a solution. My current approach involves grouping the 4 button components in an array and shifting their positions based on user input. Is there a more efficient way to accomp ...