Tips for troubleshooting TypeScript files in Angular 2

It appears that the latest angular2 npm package does not provide a way to debug TypeScript sources. Previous solutions on Stack Overflow and Medium are outdated. I have raised an issue on GitHub, please show your support.

There are two main problems:

1) The TypeScript sources no longer point to the correct location in the npm. Instead, they reference a non-existent location within the Angular GitHub sources:


{
 "version":3,
 "file":"application_ref.js",
 "sourceRoot":"",
 "sources":["../../../../modules/@angular/core/src/application_ref.ts"] <-------
}

Although this path is irrelevant due to the usage of --inlineSources during compilation of Angular sources, the second problem persists.

2) When attempting to copy the 'modules' folder from the GitHub sources, another issue arises. The JavaScript files in the npm package are compiled into ES6 module syntax, which is not yet supported in browsers. A loader like SystemJS requires Traceur for compatibility. For instance, 'common/index.j' contains:


export { NgLocalization, CommonModule, NgClass, NgFor, NgIf, NgPlural, NgPluralCase, NgStyle, NgSwitch, NgSwitchCase, NgSwitchDefault, NgTemplateOutlet, AsyncPipe, DatePipe, I18nPluralPipe, I18nSelectPipe, JsonPipe, LowerCasePipe, CurrencyPipe, DecimalPipe, PercentPipe, SlicePipe, UpperCasePipe, VERSION, Version, PlatformLocation, LocationStrategy, APP_BASE_HREF, HashLocationStrategy, PathLocationStrategy, Location } from './src/common';

Unfortunately, using Traceur might disrupt existing source maps created for JavaScript files transpiled by Typescript compiler (tsc) so it's not a viable solution.

Answer №1

Update Recently, I raised an issue on GitHub requesting for source map support to be enabled in the Angular CLI. This discussion also covers the methods available to enable source map support immediately. Once source maps are enabled for the framework, debugging TypeScript sources will become much easier.

The transition from System.js to Webpack by Angular CLI eliminates the need for System.js, which is a personal preference of mine that aligns with not providing any information regarding System.js here.

Debugging TypeScript sources of the actual framework is not possible as source maps are only generated for compiled JavaScript, not TypeScript. However, the compiled code is easily readable and sufficient for debugging purposes. The positive aspect is that your own code can be mapped back to its original TypeScript sources.

Unfortunately, obtaining source maps for Angular against TypeScript sources without them being integrated into the framework may not be straightforward. Currently, the best option available is to debug the compiled JS code.

To put this into practice, you can create a new application using Angular CLI:

$ ng new my-app

Add a breakpoint somewhere in your code:

// src/app/app.component.ts
export class AppComponent {
  title = 'app works!';

  constructor() {
    debugger;
  }
}

Launch the development server:

$ ng serve

Access the following URL in your web browser: http://localhost:4200/. Notice how the execution pauses at the specified breakpoint and navigate through some of the framework calls in the call stack.

https://i.sstatic.net/MJROw.png https://i.sstatic.net/zCBtG.png

Answer №2

In the most recent version of Angular 2, Webpack is now being used instead of SystemJS. These two systems operate in distinct ways.

SystemJS loads sources as needed and also converts TypeScript to JavaScript directly within the browser. Webpack compiles the application from the sources first, making it faster. This means that your browser does not have to handle all the work.

When attempting to debug libraries like Angular, you may need to provide Webpack with some guidance by configuring your webpack file.

It's unclear if Angular CLI provides this support right off the bat. It's also worth exploring Webpack for debugging options.

I regret that I am unable to offer any further assistance on this topic.

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 a Build-Free Workflow in a TypeScript Monorepo

Imagine having this monorepo structure: /apps /apps/app1 /apps/app1/src (includes index.ts and various other files and subdirectories) /apps/app1/tsconfig.json /apps/app1/package.json /apps/app2 /apps/app2/src (contains index.ts and many other files an ...

Sticky header in React data grid

Is there a way to implement a sticky header for a data grid in react? I have tried various methods but haven't been able to figure it out. Any suggestions would be appreciated. You can find my code sandbox example here. #react code export const Styl ...

A step-by-step guide on integrating Azure Cognitive Search within a SharePoint Online SPFx webpart

I haven't had much experience with SPFx recently, so it looks like I'll need to brush up a bit. :) I'm interested in implementing this NPM package: https://www.npmjs.com/package/azure-search Within a simple new SPFx web part: The code ...

Having trouble with Vue i18n and TypeScript: "The '$t' property is not recognized on the 'VueConstructor' type." Any suggestions on how to resolve this issue?

Within my project, some common functions are stored in separate .ts files. Is there a way to incorporate i18n in these cases? // for i18n import Vue from 'vue' declare module 'vue/types/vue' { interface VueConstructor { $t: an ...

Global registration of router directives with Angular router RC3 is required

I'm trying to avoid registering the Router_Directives for each individual component. Instead, I would like to apply it globally similar to how I did before: import { RouterConfig ,Router, ActivatedRoute, ROUTE_PROVIDERS} from '@angular/router&a ...

Tips on pre-filling a form using ngModel bindings

I have designed a Modal page where I aim to display pre-populated data to the user. Whenever the user edits the data, it should be bound to my local variable collectDataInModalPage_afterEdit. Check out the demo here:: https://stackblitz.com/edit/ionic-jse ...

Error: Angular project unable to locate Auth0 module

I recently forked the Auth0 repository (specifically for Angular) based on their instructions found on GitHub. After running npm install in the root folder, I encountered an error at the end of the process where webpack outputted the message: "Cannot fin ...

What is the best way to set up an endpoint in Angular for image uploading?

Using the Kolkov Angular editor in my Angular application, I have successfully created a rich text editor. Currently, I am looking to upload images from the editor to the server. I already have a function in place that takes a file as an argument and send ...

"Troubleshooting a case where mongoDB's updateOne function is

I am in the process of removing certain references to objects within different sections (section1, section2, section3, others) in each file. Sample Document: { "_id": "64a3268474aa29e72b40c521", "name": "Test", ...

The problem with MUI SwipeableDrawer not being recognized as a JSX.Element

Currently, I am implementing the SwipeableDrawer component in my project. However, an issue arises during the build process specifically related to the typings. I have made the effort to update both @types/react and @types/react-dom to version 18, but unf ...

When is the AbortSignal in the TanStack useQuery function considered as undefined?

The TanStack React Query documentation (visit here) provides insights on utilizing the queryFn property with an object containing a signal property for query cancellation. Check out this example: const query = useQuery({ queryKey: ['todos'], ...

Any idea how to resolve this typescript typing issue: "The argument, either a string or number, cannot be assigned to the parameter of type 'SetStateAction<string>'"?

Just recently delving into TypeScript, I've encountered a persistent typing problem that has proven challenging to resolve despite numerous attempts. The error message causing me trouble reads as follows: Argument of type 'string | number' ...

How to implement Dragula and Angular to enable draggable elements to be dropped in any position within their container?

I am in the process of developing a web application using Angular, which incorporates dragula. While the current drag and drop functionality works well for moving items within a designated "bag", I am seeking to enhance it with more advanced features. Is ...

Error: The checkbox was clicked, but an undefined property (includes) cannot be read

Link to live project preview on CodeSandbox Visit the product page with checkbox I have developed a code snippet that allows users to filter products by checking a box labeled "Show Consignment Products Only", displaying only those products with the term ...

Display the notification list by utilizing the "ToastController" function

Upon entering the home screen, I want to notify the user by displaying a list of messages from a firebase collection. The field to be displayed is "text", but I only want to show messages with the "read" field set as false. Once a message is shown, its "re ...

Is it possible to import a custom module after an Angular 2 app has been bootstrapped?

I'm currently in the process of developing a business web application using Angular2 that is based on a web API. But I've encountered a challenge. My goal is to bootstrap my Angular2 app using AuthModule, which is a custom module I have created. ...

Creating a TypeScript function that utilizes generics to automatically infer the return type

How can I create a function with a generic argument that can return any type, and have the return type inferred from its usage? I attempted the following code: type Thing<T> = <U>(value: T) => U const shouldMakeStrings: Thing<string> ...

What is the method for adjusting the border color of an ng-select component to red?

I am having an issue with the select component in my Angular HTML template. It is currently displaying in grey color, but I would like it to have a red border instead. I have tried using custom CSS, but I haven't been able to achieve the desired resul ...

Feeling lost about arrow functions in JavaScript

Here is the code I am currently using to increment the value of intVariable using window.setInterval. var Arrow = (function () { function Arrow() { this.intVariable = 1; this.itemId = -1; this.interval = 25; } Arrow.p ...

Looking for help with setting up Nodemailer and installing it via NPM

I am currently developing a mobile application using Ionic with Angular/Typescript, and I'm in need of a front-end solution to dynamically send emails in the background to a specific email address. I tried using emailjs, but it requires JavaScript whi ...