Simmetric
Simmetric

Reputation: 1651

Angular module from npm missing @NgModule declaration

A module from NPM is missing @NgModule, @Directive declarations, though it is in the source code on Github. This is preventing me from importing a directive to databind from an HTML attribute.


I'm trying to use the angular-gtag package to log custom dimensions to Google Analytics using the [params] attribute as defined 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 of the containing component.

This causes an error:

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

Reading up on this error (such as 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') leads to the suggestion that I need to add the GtagEventDirective class to the declarations in app.module.ts.

However, doing that leads to the error

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

Looking in the source of the package in node_modules, any @Component or @Directive annotations are absent. Strangely enough, they are present 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

So what can I do here? Editing the code in node_modules doesn't seem like the best idea and changes here may not even be picked up as there are already transpiled files in the package.

I tried reinstalling the package. I'm certain I have the latest version (1.0.3) and the source in Github also lists that version as the latest.

Of course I could create an issue in the Github repo, but the source code there is correct. Any change I could ask for is already in there. My problem seems to be somewhere between NPM and my machine.

Upvotes: 1

Views: 41005

Answers (3)

Simmetric
Simmetric

Reputation: 1651

My application has a custom RouterModule. I had to import the GtagModule there and then it worked.

import { GtagModule } from 'angular-gtag';

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

In the error message, the source is given as ng://CustomRouterModule/MyComponent.html which indicates there is a custom router that's causing the problem.

Upvotes: 1

SGalea
SGalea

Reputation: 722

The library on npm is not missing anything. But since we cant see your code. I'm going to post what I did.

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

The only way I can replicate your error is by removing gtagEvent from the div

Upvotes: 1

Romain Verclytte
Romain Verclytte

Reputation: 1

If the source is correct but files are missing in the release, you can link directly your depedendency to github.

try :

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

Upvotes: 0

Related Questions