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

Challenges with deploying an AngularJS application on a WebSphere Application Server (W

I have been attempting to package my spring boot webservice and Angular 6 app into a war file for deployment on WebSphere Application Server 8.5. I created a sample Angular app and ran "ng build --prod". Following the guidelines provided in this answer, I ...

Is there support for TypeScript in express-openid-connect?

Is there any documentation available for using express-openid-connect with TypeScript, or if it is supported at all? ...

Is it feasible to utilize AngularFire and Firebase in tandem?

Currently, I am working with Ionic2 and firebase. There are two methods to initialize firebase in app.ts that I have come across. I am wondering which one is the recommended approach and how can both functions be utilized together? When I initialized fireb ...

"Customizing text input colors in Angular Material: A step-by-step guide

Welcome to my User Form: https://i.stack.imgur.com/7eLp9.jpg I'm facing an issue where the color of the input field is white, blending with the background color. I've been searching for how to change the color of the input field without any luc ...

There seems to be an issue with the response type in the node.js environment

I am currently working on node.js and typescript, but I am encountering a minor issue. Below is the routeController I have created: public allUsers = (req: Request, res: Response) => { res.status(500).json({ status: "ERROR", ...

Incorporate a fresh element into an object after its initial creation

Hello, I am looking to create an object in JavaScript that includes an array-object field called "Cities." Within each city entry, there should be information such as the city's name, ID, key, and a District array object containing town data for that ...

Incorporating marker tags to different sections of text within a contenteditable div

The main objective of this feature is to enable users to select placeholders within message templates (variable names enclosed in %). Inspired by Dribbble The API provides strings in the following format: "Hello %First_Name% %Middle_Name% %Last_Name% ...

What causes old data to linger in component and how to effectively clear it out

Fetching data from NGXS state involves multiple steps. First, we define the state with a default list and loading indicator: @State<CollectionsStateModel>({ name: 'collections', defaults: { collectionList: [], (...), isListLoading: true, ...

What is the most effective method for importing the "exceljs" library into an Angular project?

In my Angular 7 app, I am utilizing exceljs (https://www.npmjs.com/package/exceljs) to handle importing and exporting of Excel files. Currently, I have imported it using the following code snippet: import {Workbook} from "exceljs";. While it functions perf ...

Poorly packaged library - Custom Angular library - Node Package Manager

Recently, I've been delving into the process of publishing a simple Angular library on NPM. Despite following various tutorials (like those found here, here, and here), I faced difficulties when attempting to use it in a test project. MY JOURNEY In s ...

Tips for testing an Angular 2 component that integrates a third-party JavaScript library

Seeking guidance on how to properly test the component below that utilizes a 3rd party JavaScript library - import * as Leaflet from "leaflet"; export class GeoFencingComponent { map: any; constructor() { this.map = Leaflet ...

Is it possible to create an Angular 2 service instance without relying on the constructor method?

I'm struggling to utilize my ApiService class, which handles API requests, in another class. Unfortunately, I am encountering a roadblock as the constructor for ApiService requires an HttpClient parameter. This means I can't simply create a new i ...

Typescript with Angular: Despite having 7 values in the map, Map.get is returning undefined

Why does Map.get always return undefined when using a number from a form element (extra1) in this code snippet? extraById = new Map<number,Extra>(); @Input() extra1: number = -1; formChanged(carConfigurationFormChanged : any) { const index ...

Can anyone provide guidance on incorporating lodash into an Ionic 2 project?

Recently, I began diving into a new project that involves Ionic 2. TypeScript is still fairly new to me, and I've been brainstorming ways to integrate lodash into my project. Have any of you tackled this before and can offer guidance on how to achiev ...

String values not being set as default in Angular Material's mat-button-toggle-group

<div class="navigation"> <mat-button-toggle-group class="button-toggle" [(ngModel)]="langSelected" (change)="onToggleGroupChange($event)" [value]="langSelected"> <mat-button-toggle value=& ...

Recording changes in SVG size in Angular 2

I am aiming to create an SVG canvas within an Angular 2 template that automatically scales with its parent element and triggers a redraw method when its size changes. While using the onresize property, I successfully receive events but encounter difficult ...

Selecting options in combobox for each row in a table using Angular 2 programmatically

I am currently working on an Angular 2 application using TypeScript. In one of the tables within the application, there is a select control in one of the columns. The contents of this select control are bound to another string array. <table ngContr ...

Error: Unable to locate sportsRecord due to missing function

updated answer1: Greetings, I have updated the question with some detailed records and console logs for better understanding. sportsRecord = { playerTigers:[ {TigerNo: 237, TigerName: "Bird Bay Area", TigerkGroupNo: 1, isDefault: true ...

The Angular 2+ page fails to load properly on Internet Explorer or SharePoint

I created an Angular 5 solution that functions well in both Chrome and Firefox within SharePoint 2013. However, I have run into issues when using IE11. The index.html file was inserted into the SharePoint page using a Content Editor web part. Even after di ...

Is it possible to implement typed metaprogramming in TypeScript?

I am in the process of developing a function that takes multiple keys and values as input and should return an object with those keys and their corresponding values. The value types should match the ones provided when calling the function. Currently, the ...