An Angular module downloaded from npm seems to be lacking the required @NgModule declaration

There seems to be a missing @NgModule and @Directive declarations in an NPM module, even though they exist in the source code on Github. This is causing an issue with importing a directive for databinding from an HTML attribute.


I am attempting to utilize the angular-gtag package to log custom dimensions to Google Analytics using the [params] attribute as described in the readme here: https://github.com/codediodeio/angular-gtag

<div gtagEvent trackOn="click" action="myAction" [params]="{ myDimension: myDimensionValue}"></div>

Where myDimensionValue is a variable within the containing component.

This results in an error:

Template parse errors:
Can't bind to 'params' since it isn't a known property of 'div'.

After researching this error (like here: Angular 4 Can't bind to <property> since it isn't a known property of <component> or here: Can't bind to 'x' since it isn't a known property of 'y'), it appears that adding the GtagEventDirective class to the declarations in app.module.ts could resolve the issue.

Yet, doing so triggers the error:

Unexpected module 'GtagModule' declared by the module 'AppModule'. Please add a @Pipe/@Directive/@Component annotation

Upon inspection of the package's source in node_modules, there are no @Component or @Directive annotations present. Interestingly, these annotations do exist in the module's source code on Github: https://github.com/codediodeio/angular-gtag/blob/master/lib/src/gtag-event.directive.ts and https://github.com/codediodeio/angular-gtag/blob/master/lib/src/gtag.module.ts

What steps can I take next? Modifying the code in node_modules doesn't seem ideal, and any changes may not be recognized as there are already transpiled files in the package.

I have attempted to reinstall the package. It appears that I am using the latest version (1.0.3) as indicated in the Github source. However, the issue persists.

While creating an issue in the Github repository is an option, the source code there is accurate. Any necessary changes are already incorporated. The problem appears to lie between NPM and my local environment.

Answer №1

The npm library is comprehensive and covers all necessary components. However, without access to your specific code, I can only share what I have implemented:

package.json

...
  "dependencies": {
    "@angular/common": "^8.0.0",
    "@angular/compiler": "^8.0.0",
    "@angular/core": "^8.0.0",
    "@angular/forms": "^8.0.0",
    "@angular/platform-browser": "^8.0.0",
    "@angular/platform-browser-dynamic": "^8.0.0",
    "@angular/router": "^8.0.0",
    "angular-gtag":"1.0.3",
    "core-js": "2",
    "rxjs": "^6.5.2",
    "zone.js": "^0.9.1"
  },
...

index.html

<html>

<head>
    <script async src="https://www.googletagmanager.com/gtag/js?id=GTM-5FJXX6">
    </script>
    <script>
        window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());
  gtag('config', 'GTM-5FJXX6', { 'send_page_view': false });

    </script>
</head>

<body>
    <my-app>loading</my-app>
</body>

</html>

app.module.ts

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { GtagModule } from 'angular-gtag';
import { AppComponent } from './app.component';
import { HelloComponent } from './hello.component';
import { RouterModule, Routes } from '@angular/router';
@NgModule({
  imports:
    [
      GtagModule.forRoot({ trackingId: 'GTM-5FJXX6', trackPageviews: true }),

      BrowserModule,
      FormsModule,
      RouterModule.forRoot(
        [
          { path: "", component: HelloComponent }
        ]
      ),

    ],
  declarations: [AppComponent, HelloComponent],
  bootstrap: [AppComponent]
})
export class AppModule { }

app.component.ts

import { Component } from '@angular/core';
import { Gtag } from 'angular-gtag';
@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent  {
  name = 'Angular';
   constructor(gtag: Gtag) {}
}

app.component.html

<hello name="{{ name }}"></hello>
<div gtagEvent
     trackOn="dragstart"
     action="product_dragged"
     category="ecommerce"
     [params]="{ event_label: 'Something cool just happened' }">

   Some Product...

</div>

demo If you still don't find where. replicate your issue on stackblitz

To identify the error, ensure that the gtagEvent attribute is present in the div element.

Answer №2

After incorporating a custom RouterModule in my application, I found that importing the GtagModule was necessary for it to function correctly.

import { GtagModule } from 'angular-gtag';

@NgModule({
  imports: [GtagModule]
})
export class CustomRoutingModule

An error message pointed to ng://CustomRouterModule/MyComponent.html as the source of the issue, indicating that a custom router may be causing the problem.

Answer №3

In cases where the source information is accurate but some files are absent from the release, a feasible solution would be to link your dependency directly to GitHub.

Consider using:

npm install --save https://github.com/codediodeio/angular-gtag#master

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

A guide to implementing unit tests for Angular directives with the Jest testing framework

I am currently integrating jest for unit testing in my Angular project and I am relatively new to using jest for unit tests. Below is the code snippet for DragDropDirective: @HostListener('dragenter',['$event']) @HostListener(& ...

Is it possible for users to customize the window size in an Angular 8 application?

Hello everyone, I'm new to Angular and this is my first time posting on stackoverflow. So please be kind! ...

I attempted to simulate the mat-dialog in Angular that contains the ngOnInit method, but unfortunately, it is not being successfully tested

In my TypeScript file, I am trying to unit test a component that includes a mat-dialog and a form. So far, the constructor code is covered in the tests, but I'm having trouble calling the rest of the methods. Below is my spec file where I have mocked ...

Obtain values from a specific set, and then filter out values from an array that correspond to the index of that set into distinct arrays

The Problem I'm Facing Currently, I am dealing with a large data table that contains a mix of relevant and irrelevant data. My goal is to filter out the information I care about and display it in a more concise table. Using RegEx, I have managed to i ...

What is the best way to connect Classes and Objects in Angular5?

Picture a study tool with flashcards and different categories. Each category has a title, while each card contains content, is linked to only one category, and is also connected to another card. How can I establish these connections in Angular 5 and Types ...

Maintain the default type for the generic type parameter

I am attempting to work around a few optional generic type parameters and keep them as defaults that have already been set. Here is a sample code snippet: interface Foo<T, T1 = string, T2 = boolean> { ID: T Name: T1 IsActive: T2 } There ...

Navigating the complexities of applying CSS exclusively to child grids within Kendo Angular may seem challenging at first

This image illustrates the grid layout I have created an angular UI using kendo that features a nested grid. I am trying to apply CSS specifically to the child grid without affecting the parent grid. However, no matter what CSS I write, it ends up being a ...

You are unable to declare an Angular component as an abstract class within a module

In my coding project, there is a fundamental component class known as BaseComponent with its unique markup referred to as <base-component-fun>. The structure of this component's markup looks like this: <div> ..Base markup </div> Thi ...

What methods are available to enhance the appearance of a string with TypeScript Angular in Python?

Looking to enhance the appearance of a Python string for display on an HTML page. The string is pulled from a database and lacks formatting, appearing as a single line like so: for count in range(2): global expression; expression = 'happy'; sto ...

Angular 2 ngx-modal: A User-Friendly Modal Component

I have encountered an issue while trying to implement a modal form in my Angular application. Even though my code seems correct and I have installed ngx-modal as well as imported the ModalModule in my app.module.ts, the modal form is not responding. Any ...

Union types discriminate cases within an array

Creating a union type from a string array: const categories = [ 'Category A', 'Category B' ] as const type myCategory = typeof categories[number] myCategory is now 'Category A' | 'Category B' Now, the goal is ...

Resolving the active tab problem within Angular 2 tab components

Can anyone assist in resolving the active tab problem within an angular 2 application? Check out the Plunker link I am using JSON data to load tabs and their respective information. The JSON format is quite complex, but I have simplified it here for cla ...

Trouble installing Angular: ENOENT Error causing issues

Currently in the process of setting up angular. I have been diligently following the provided installation instructions, but when executing npm install -g @angular/cli, I encounter the error shown in the screenshot below. https://i.sstatic.net/LSP7R.jpg H ...

Error: The function type 'Dispatch<SetStateAction<string>>' cannot be assigned to the type '(value: OptionValue) => void'

I recently dove into the world of integrating Typescript generics with React after reading this insightful article. I followed the code provided in the article, but encountered the following error: Type 'Dispatch<SetStateAction<string>>&ap ...

How to Verify Username in Angular2 and Sails Framework

Currently, I am implementing Angular2 on the front end and Sails.js on the back end. When a user registers, it is necessary to validate whether the chosen username already exists in the database. The backend system developed using Sails will respond with J ...

How does the highlighting feature in Fuse.js includeMatches function operate?

Currently, in my Next JS/Typescript web application, I am using the Fuse.js library. However, I am still uncertain about how the includeMatches option functions for highlighting purposes. Whenever I enable this option, I receive a matches object within the ...

Encountering a Prettier error with React Native 0.71 and Typescript

https://i.stack.imgur.com/h9v5X.pngThe app runs smoothly, but these red warnings are really bothering me. How can I resolve this issue?https://i.stack.imgur.com/NebzJ.png ...

"Uploading user profile images with Angular, Express, Multer, and Mongoose made easy

Hey there, I'm currently using multer to upload images. When I select an image, it gets saved in the backend folder called uploads. However, I would like to store it in a MongoDB database and then display that image on the frontend using Angular. I&ap ...

The favicon appears broken upon opening a new tab

Usually, I use a favicon PNG file for my website. It works perfectly and I can see my favicon on the browser tab. However, when I open a PDF document in a new page from my Angular app, I notice that there is a broken icon on the browser tab. Here is how I ...

Guide to create a React component with passed-in properties

Currently in the process of transitioning a react project from redux to mobx, encountering an issue along the way. Previously, I utilized the container/presenter pattern with redux and the "connect" function: export default connect(mapStateToProps, mapDi ...