Error message: "Lazy-loaded modules encounter a TypeError stating that '' is not a function when accessed through NGINX."

Hey, I've got some distribution files for you to check out:

  • AOT-enabled dist with Lazy Modules
  • No AOT Lazy Modules dist
  • AOT without Lazy Modules dist

Here's what's been going on:

  • When served locally with webpack-dev-server or live-server, my AOT build with Lazy Loaded modules runs smoothly.
  • But as soon as these files are hosted by NGINX, I encounter JavaScript errors in Firefox and Chrome.
  • I've experimented with various webpack configurations but the issue persists.
  • Notably, I haven't imported any Lazy Loaded modules in my TypeScript files.
  • With AOT turned off, everything works seamlessly from NGINX.
  • The error "TypeError: '' is not a function" specifically arises when serving Lazy Loaded Modules through NGINX.

I'm utilizing the official Angular package @ngtools/webpack for incorporating AOT compilation into my Angular 5 app. You can find a helpful guide here on using @ngtools/webpack for AOT integration in a Webpack project. Be sure to follow this step to include Lazy Load module file paths in your tsconfig-aot.json, as it's crucial for successful AOT compilation.

All functions flawlessly on localhost with:

npm run serve

During development server deployment, compiled files are stored on disk and served via NGINX.

The issue stems from specific strange errors produced by loaded Lazy modules, such as this in Firefox:

TypeError: i0.\u0275crt is not a function

And this in Chrome:

ERROR TypeError: i0.ɵcrt is not a function

Diving deeper into the Chrome error, here's the source mapping throwing it:

Notice how var i0 is created at this line:

var i0 = __webpack_require(/* @angular/core */ "./node_modules/@angular/core/esm5/core.js");

On the dev server, node_modules reside at the parent level alongside my dist folder:

Here's a glance at my resource files on the dev server:

Additionally, here's a comparison of resources served up from localhost:

And those served up from the development server:

Now let's delve into my configuration and npm package versions:

webpack.config

 // Code snippet

package.json

 // Package information

tsconfig-aot.json (files array includes lazy loaded module paths)

 // Configuration details

Lastly, here's my NGINX setup:

 // NGINX configuration

If needed, take a look at my main.ts and app.module.ts files:

main.ts

 // main.ts code

app.module.ts

 // app.module.ts code

Answer №1

The reason behind this is that when AOT is utilized, the code becomes obfuscated with unique UTF-8 symbols like ɵ, as seen in i0.ɵcrt in your scenario

To resolve this issue, it is essential to configure nginx to support UTF-8 encoding

server {
  listen 80 default_server;
  listen [::]:80 default_server;
  server_name _;

  charset UTF-8; #<==== Add this

Answer №2

To update your existing nginx.conf file located in the /etc/nginx directory, please replace it with the code snippet below:

#worker_processes 2;

events {
    worker_connections 1024;
}
http {
   sendfile on;

   gzip on;
   gzip_disable "msie6";

   gzip_vary on;
   gzip_proxied any;
   gzip_comp_level 6;
   gzip_buffers 16 8k;
   gzip_http_version 1.1;
   gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

    log_format timed_combined '$remote_addr - $remote_user [$time_local] '
    '"$request" $status $body_bytes_sent '
    '"$http_referer" "$http_user_agent" '
    '$request_time $upstream_response_time $pipe';

    access_log /var/log/nginx/access.log timed_combined;
    error_log /var/log/nginx/error.log;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    server {
        listen 80;
        server_name _;
        return 404;
    }

   server {
        listen 80;
        server_name www.example.com;

        location / {
           root /home/pokusr/propok-frontend/dist;
           try_files $uri /index.html;
        }
        location ~* "^/[a-z0-9]{40}.(css|js)$" {
            root /home/usr/proj_path/dist/;
            access_log off;
            expires max;
        }
    }
}

For lazy loaded routes that contain hash values, utilize the code

location ~* "^/[a-z0-9]{40}.(css|js)$" {
to address the issue.

Answer №3

Transitioning to Angular 8 requires updating the traditional lazy loading approach to utilize dynamic imports and adjusting the module settings in the tsconfig.json file from 'esnext' to 'es2015'.

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

How to Restrict the Number of Rows Displayed in an Angular 4 Table

Currently, I am faced with a situation where I have a lengthy list of entries that I need to loop through and add a row to a table for each entry. With about 2000 entries, the rendering process is slowing down considerably. Is there a way to limit the disp ...

Typescript interface created specifically for React Higher Order Component Props

Consider the React HOC provided below which adds sorting state to a component: import React, {Component, ComponentClass, ComponentType} from 'react' interface WithSortState { sortOrder: string } interface WithSortInjectedProps { sortO ...

Angular Routing can be a powerful tool for managing multiple article posts in an efficient and organized way

I am in the process of building a website with Angular that features numerous articles. Whenever a user clicks on an article, I want it to navigate to a new URL using routing. To achieve this, I have created a new Article component and here is how my app- ...

Currently in the process of modernizing an outdated method to a more up-to-date version in Jasmine, encountering issues related to

Currently working to update the method throwOnExpectationFailure to the newer one oneFailurePerSpec. According to the Jasmine documentation, this can be achieved by: Use the `oneFailurePerSpec` option with Env#configure After conducting research, I came ...

How can the Calendar Ant Design showcase events that have fluctuating dates?

Seeking a solution to display events on an Ant Design Calendar using dateCellRender with dates stored in a variable object. Here's an example of the object: [ { "id": 1, "content": "Example", & ...

Setting the type of a prop dynamically based on another prop value

Consider the following scenario with an interface: interface Example { Component: React.ReactElement; componentProperties: typeof Example.Component; } Is there a way to determine the type of properties expected by a passed-in custom component? For ...

Unable to transfer variable from a function to the test in Protractor

Currently, I am working on a test to verify the amount of gold in my possession. The test is being conducted using TypeScript and Protractor. Within this testing scenario, I have a method named GetAmountOfChips: public static GetAmountOfChips(): PromiseL ...

Struggling to integrate the Animejs library into my TypeScript and webpack project

Transitioning from ES5 to TypeScript and webpack, I decided to incorporate the Three.js library seamlessly. Alongside this, I wanted to utilize the Anime.js library for its impressive timeline animation features. However, my efforts yesterday were fraught ...

Potential Issue: TypeScript appears to have a bug involving the typing of overridden methods called by inherited methods

I recently came across a puzzling situation: class A { public method1(x: string | string[]): string | string[] { return this.method2(x); } protected method2(x: string | string[]): string | string[] { return x; } } class B extends A { prot ...

Struggling to store information in a MongoDB database within my MEAN Stack project

After successfully creating a collection for user LOGIN/LOGOUT and managing to store and retrieve data using Express app and mongoose, I encountered an issue when trying to create another collection. Despite successfully creating the collection, the data w ...

When trying to console log a selected date, the output displays as undefined

<div class='col-sm-6'> <input [(ngModel)]="date" id="date" name="date" class="form-control" required/> </div> $(function () { $('#date').datetimepicker({ format: 'DD/MM/YYYY hh:mm' } ...

Issue with Stenciljs custom event not triggering using @Listen decorator

I've been trying to grasp the workings of the custom event emitter. While my mouse events seem to be functioning properly, I'm encountering issues with the custom events. Despite emitting correctly, it appears that the listener is not capturing t ...

An ambient module will not be successfully resolved through a relative import operation

As per the typescript documentation (https://www.typescriptlang.org/docs/handbook/module-resolution.html): A relative import is resolved in relation to the importing file and does not resolve to an ambient module declaration. However, it also states: ...

No issues raised by Typescript/tslint regarding this in arrow function

After making some creative adjustments, this is what I came up with: const TopBar = () => ( <Button onPress={this.onPress} // No errors shown /> ) Although all my other rules in tslint.json are functioning properly. Is there a way to ma ...

Polling database from various browser tabs

Working on a .NET/angular application that regularly checks the SQL Server database for updates, with the user able to customize the polling interval starting from 10 seconds (as per business requirements). The issue arises when each new tab opened by a ...

Using Typescript: Compiling specific files within a directory

According to the documentation for typescript, we have the option in tsconfig.json to manage input files using either the files property where all files are listed, or with the exclude property. I have organized all my source files within a directory named ...

Issues with path in ngx-cookie-service while using Angular 9

I'm currently utilizing the ngx-cookie-service package to store important data for my application. It is crucial for me to save this cookie on the base path '/' so that I can easily retrieve it when needed. The cookie requires periodic updat ...

What is the purpose of mapping through Object.keys(this) and accessing each property using this[key]?

After reviewing this method, I can't help but wonder why it uses Object.keys(this).map(key => (this as any)[key]). Is there any reason why Object.keys(this).indexOf(type) !== -1 wouldn't work just as well? /** * Checks if validation type is ...

Angular drag and drop functionality combined with interactive buttons for list management

Looking to create an Angular component for re-sorting objects by dragging from one list to another? I also need this component to include buttons for additional functionality. I've explored the various drag and drop components available on the Angula ...

Creating a TypeScript interface that inherits properties from another interface is a powerful way to define

My question pertains to a programming interface I have created called PersonInterface. Within this interface, I have included a property called 'address' which has a type of AddressInterface - another interface that I have defined. I am wondering ...