The bundling of Angular 2 and RxJS is not happening in Typescript

This is my tsconfig.json

{
  "compilerOptions": {
    "target": "es5",
    "module": "system",
    "moduleResolution": "node",
    "sourceMap": true,
    "removeComments": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "noImplicitAny": true,
    "suppressImplicitAnyIndexErrors": true,
    "outFile": "./js/app.js",
    "typeRoots": [
      "./node_modules/@types"
    ],
    "types" : [ "node", "core-js" ]
  },
  "include": [
    "./ts/**/*.ts"
  ],
  "exclude": [
    "**/*.spec.ts"
  ]
}

This is my system.config.js (Basically nothing)

System.import('main');

My goal is to consolidate all scripts into a single JS file for production use. TypeScript has helped with this, creating the bundled app.js.

However, I encountered

http://localhost/@angular/platform-browser-dynamic 404 (Not Found)

in the browser console.

Upon inspecting the app.js file, I noticed that all @angular and RxJS code were not included in the bundle. As a result, SystemJS could not locate them and attempted to request the script files conventionally.

According to my understanding, the line "moduleResolution": "node" in the tsconfig.json file should instruct TypeScript to load @angular and RxJS using the Node resolution method, which entails searching in the node_modules directory. However, this does not seem to be happening as expected.

Answer №1

Typescript serves as a language compiler rather than a module bundler. For bundling tasks, it is recommended to utilize tools like webpack, browserify, or rollup.

An example of a typical workflow with webpack would involve:

TS > es6 > babel > webpack > bundle

If additional modules need to be bundled along with TypeScript code, it is advised not to rely solely on the bundling capabilities of TypeScript. Instead, another module bundler should be used for this purpose while allowing TypeScript to focus on compiling the code effectively.

One way to configure rules in webpack is shown below. It's important to note that webpack applies loaders in reverse order, meaning that the ts-loader will be executed first:

module: {
        rules: [
            {
                test: /\.tsx?$/,

                use: [{loader:'babel-loader', options: { presets: ['es2015'], plugins: ['add-module-exports'] }},'ts-loader'],
                exclude: /node_modules/,
            },
        ]
    }

Answer №2

In the end, I approached this question in a different way. I realized that there is a more efficient method for handling Angular2 projects by utilizing Angular CLI, which is considered the official tool for building Angular2 projects. This also meant moving away from using SystemJS.

One drawback of using SystemJS is that it is not compatible with Webpack, making it incompatible with Angular CLI.

While it's true that TypeScript is not a module bundler on its own, there are indications that they are working towards meeting all requirements in the industry, possibly extending to becoming a module bundler in the future despite current limitations.

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

Has the Angular 2 community established a standardized ecosystem?

As a developer specializing in Angular 1, I am now eager to dive into the world of Angular 2. However, navigating through the changes and rewrites can feel like traversing a confusing maze. All of the comprehensive guides I have come across date back to l ...

Pressing a button will reset the Mat Radio Group's Mat Radio Buttons back to their default settings

I have a list of items that need to be rated using radio buttons. I utilized Mat radio group and Mat radio buttons for this purpose. In the scenario below, I need to rate games as good, bad, or none. Once the changes are implemented, I want all the radio b ...

Resolve the Typescript issue that occurs while modifying MuiDataGrid within createTheme

I am trying to customize my DataGridPro table, which is a MUI component, within the theme. However, when I add MuiDataGrid to the components object, I encounter a TypeScript error: Object literal may only specify known properties, and 'MuiDataGrid&apo ...

Sending data to back-end server using Multer, Node.js, and Express via a POST request

I have been facing an issue while trying to send a form from my local system to the backend server via a POST request. Every time, it seems to go with --binary-data instead of going with --form. Below is a sample curl command that I am looking to replicat ...

Guide to linking multiple images to a single Post instance

Is it possible to attach each array of images to their post using the foreign key placePlaceId? For example, in the first object with place_id:3, I want to attach all the images with the foreign key placePlaceId:3. This is my approach to achieving this go ...

Angular2 authguards encountering issues when trying to run asynchronous functions

I need a way to safeguard my routes by verifying if a user is logged in from the server, but I'm facing issues with asynchronous functions not executing properly. Below is the code snippet that's causing trouble: canActivate (route: ActivatedRo ...

I'm having trouble configuring the header in my Node/Express route

Using Node and the Express framework for my backend, along with React for my frontend, all coded in Typescript. The elastic search client is responsible for fetching data on the backend, but I don't believe that's where the issue lies. I'm ...

Authenticating with JWT in Deno

Can someone guide me on generating and authenticating JSON Web Token in Deno? Being a newbie to the Deno environment, I would greatly appreciate a code snippet to kickstart my journey with JWT in Deno. ...

Customize the appearance of the Material UI expansion panel when it is in its expanded

Is there a way to customize the height of an expanded expansion panel summary? Specifically, I am looking to remove the min-height property and set the summary panel's height to 40px instead of the default 64px. I have attempted to make this change in ...

What is the hierarchy of importance for routes in Angular - parent versus child levels?

Let's say I define a top-level route with the path '/some/childr': [{ path: '/some/childr'}] Then, I create another top-level route /some with a child route /childr: [{ path: '/some', children: ['/childr']}] ...

What is the most effective method for dividing a string in TypeScript?

Here is the scenario: receiving a string input that looks like Input text: string = "today lunch 200 #hotelname" Output subject: "today lunch" price: 200 tag: #hotelname My initial solution looks like this: text: string = "today lunch 200 #hotelname" ...

When downloading text using Angular, the file may not display new line characters correctly when opened in Notepad

When downloading text data as a .txt file in my Angular application using JavaScript code, I encountered an issue. Below is the code snippet: function download_text_as_file(data: string) { var element = document.createElement('a') eleme ...

Angular and Bootstrap combined to create a versatile navigation bar with a collapsible sidebar feature

Currently, I am in the process of developing a progressive web app using Angular and Bootstrap. One of the main challenges I am facing is implementing a navbar that not only looks great on desktop but also on mobile devices. So far, I am satisfied with my ...

Facing numerous "error TS1005" messages when performing a gulp build due to node_modules/@types/ [prop types] and [react] index.d.ts with SPFx Webpart

I am currently in the process of developing a custom spfx webpart that includes a feature to display link previews. In order to achieve this functionality, I integrated this specific library. However, I encountered some challenges during the implementation ...

The prototype's function doesn't pause for anything, carrying out its duty in a continuous cycle

I have been attempting to enhance the prototype of an object by adding an asynchronous function to be called later on. Here is my approach: const ContractObject = Object; ContractObject.prototype['getBalance'] = async function(userId: number ...

Unraveling the mystery of decoding a jwt token

Every time I attempt to validate a user token, I keep encountering Error 500. function verifyToken(req, res, next) { if(!req.headers.authorization){ return res.status(401).send('Unauthorized request') } let token = req.headers.authorization. ...

What could be causing these Typescript compilation errors I am experiencing?

I am puzzled by the appearance of these errors, especially since I copied the entire ClientApp folder from a running application where they did not exist. https://i.sstatic.net/tcO5S.png Here is the structure of my project: https://i.sstatic.net/P4Ntm.p ...

Tips for presenting a row that states "No data found" when the datasource is devoid in Angular using material2

Currently, I am utilizing angular/material2 to showcase a table. <mat-table class="table" #table [dataSource]="dataSource" matSort > <ng-container matColumnDef="{{col}}" *ngFor="let col of displayedColumns"> <mat-header-cel ...

What is the best way to eliminate commas from an array within an Angular project?

I am attempting to retrieve a list of actors from movies; however, in the database I created, each actor's name has a comma at the end of the string. When calling the array, the content shows up with double commas next to each other. I need help figur ...

Determine the output type of a function in Typescript using an input value specified by an enum

I am currently saving settings to local storage and want to be able to input responses when retrieving (and possibly inserting) values from/to the storage. After researching, it seems that using function overloading is the best approach. Here is what I ha ...