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: https://i.sstatic.net/yFSdH.png

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: https://i.sstatic.net/jeFWs.png

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

https://i.sstatic.net/8rZiN.png

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

https://i.sstatic.net/mpWGx.png

And those served up from the development server: https://i.sstatic.net/p5edy.png

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

Preventing Memory Leaks in Single Page Applications (SPAs) Using Google DFP with Angular and Vue: A Guide to Properly Destroying Ads and Their References

I recently encountered an issue while trying to implement Google's DFP in both Vue.js and Angular single-page applications (SPAs) where it appears to be leading to a memory leak. For Angular: I have created a proof of concept which can be found here. ...

When you find that the plugins on pub.dev do not offer web support, consider utilizing npm packages for Flutter web development

I am currently working on developing a cross-platform app using Flutter for iOS, Android, and web. However, some plugins do not support web. Fortunately, I came across npm packages that provide the same functionality and I am considering integrating them. ...

Methods for setting a global instance within a local function

Hello, I'm fairly new to using TypeScript and Angular2. Let me quickly explain what I'm trying to accomplish with my method. I have some hard-coded XML data that I need to parse into JSON format. Once parsed, each JSON object needs to be assigned ...

How to make a div stand out when clicked in an Angular application

Within my code, there is a booking-list div that I am utilizing to showcase booking timings. When hovering over this div, the background-color changes, as depicted in the image below. https://i.stack.imgur.com/Iz1r6.png My current dilemma is that when se ...

Vue HeadlessUI Error: The function vue.defineComponent is not recognized

Trying to incorporate @headlessui/vue into my nuxt project has been a challenge. My attempt at using it looks like this: <template> <Menu> <MenuItems> <MenuItem>Item</MenuItem> </MenuItems> </Menu&g ...

Discovering the proper way to namespace or target a variable input within a component

Consider the following: mat-button directive is affected by a disabled attribute / input. matTooltip directive is also impacted by a disabled attribute / input. Can you design a material button that appears disabled, but still has an active tooltip asso ...

Having trouble accessing undefined properties? Facing issues with the latest Angular version?

Why am I encountering an error and what steps can be taken to resolve it? Currently using the latest version of Angular. ERROR TypeError: Cannot read properties of undefined (reading 'id') Here is the JSON data: { "settings": [ { ...

Guide to manually triggering a re-render of a component in Angular 2

While working with ng Smarttable, I encountered a situation where I needed to change the datasource array through an event, specifically by altering an ID within the array. Strangely, Angular did not detect these changes until I interacted with the page by ...

Bring in a class with an identical name to a namespace

Currently, I am utilizing a third-party library that comes with a separate @types definition structured as follows: declare namespace Bar { /* ... */ } declare class Bar { /* ... */ } export = Bar; How should I go about importing the Bar class into my ...

Preview of Azure AD Consent Scopes and Access Token Version Error in Msal Angular

Currently in the process of updating my web application with the new MSAL library, @azure/msal-angular, from previously using the old ADAL library. The frontend of the web app is built with Angular 5 and communicates with a backend ASP.NET Core Web API. ...

ngc encountering issues when compiling project with untyped third-party libraries

Utilizing a third-party library without defined types, the project is being developed using Angular CLI (version 1.0.0-beta.29) and the library is declared in typings.d.ts. In this instance: declare module ‘xml2js-es6-promise’; The project compiles an ...

Unwrapping the Angular ngForm Mystery: Resolving

I was attempting to retrieve values using ngOnInit and initializing the form with default values, but for some reason the form is returning as undefined. I also tried using patchValue, but since the form is undefined, it doesn't work. It's intere ...

An error with code -4058 is preventing the installation of npm packages when running the 'npm install' command after using 'ng new'

Embarking on a new Angular project, I encountered an issue while trying to install packages listed in my package.json file. Despite multiple attempts on both of my computers, the same error persisted, leaving me baffled. Determined not to give up, I seek g ...

Issue found: Passing a non-string value to the `ts.resolveTypeReferenceDirective` function

Encountering the following error: Module build failed (from ./node_modules/ts-loader/index.js): Error: Debug Failure. False expression: Non-string value passed to ts.resolveTypeReferenceDirective, likely by a wrapping package working with an outdated res ...

Tips for customizing the appearance of ng-bootstrap accordion

Looking to enhance the appearance of ng-bootstrap accordion with some unique fade styling. Any suggestions on how to accomplish this? ...

The ngModel in AngularJS 2 fails to update when the Jquery Date Picker date is changed

Having an issue with the Jquery UI datepicker within an angularjs 2 component. The datepicker is displaying the changed date, but it's not updating the underlying ngModel: dateSelected. dateSelected: string = ''; The date picker input fie ...

Tips for telling the difference between typescript Index signatures and JavaScript computed property names

ngOnChanges(changes: {[paramName: string]: SimpleChange}): void { console.log('Any modifications involved', changes); } I'm scratching my head over the purpose of 'changes: {[propName: string]: SimpleChange}'. Can someone cl ...

How can I ensure that all the text is always in lowercase in my Angular project?

Is there a way to ensure that when a user enters text into an input field to search for a chip, the text is always converted to lowercase before being processed? Currently, it seems possible for a user to create multiple chips with variations in capitaliza ...

When trying to use TypeScript with next.js, encountering an unexpected token `?` error is not

Having an issue with next.js, the command npm run dev keeps failing due to a syntax error related to an optional property in a tsx file: Syntax error: Unexpected token 44 | 45 | type State<T_HT> = { > 46 | ghostHighlight: ?{ | ...

Gatsby struggles with generating Contentful pages using TypeScript

I have been working on creating dynamic pages with Contentful in Gatsby + Typescript. While I am able to fetch data successfully using GraphQL in a browser, I encounter issues when trying to fetch data in gatsby-node.ts. The pages seem to be generated part ...