62

I use angular-material in my project. I am getting this warnings:

Could not find HammerJS. Certain Angular Material components may not work correctly.

The "longpress" event cannot be bound because Hammer.JS is not loaded and no custom loader has been specified.

I know this is a duplicate of this question

In the answer they have mentioned that:

  1. We have to add "^2.0.8", to dependencies in package.json file.
  2. import 'hammerjs/hammer'; in polyfills.ts file.

In my case everything is fine, still I am getting the same warning in browser console.

Community
  • 1
  • 1
PGH
  • 2,164
  • 9
  • 34
  • 62

5 Answers5

95

There are 2 ways to solve this problem:

  1. Either include the (main) import in your main module's file or polyfills.ts:

    import 'hammerjs';
    
  2. Or include the script from a CDN into your index.html file:

    <head>
      <!-- ... -->
      <script src="https://cdnjs.cloudflare.com/ajax/libs/hammer.js/2.0.8/hammer.min.js"></script>
      <!-- ... -->
    </head>
    
Edric
  • 24,639
  • 13
  • 81
  • 91
  • `import 'hammerjs';` has solved my error thanks a lot for that, Actually previously i have imported **(hammerjs)** in main modules file like `import {} from 'hammerjs';`. Is this wrong?? – PGH Oct 03 '18 at 08:43
  • 1
    @PrashanthGH Nope. Just import the module itself. That syntax won't import anything for use with the project. However, importing the module (probably) imports all modules. – Edric Oct 03 '18 at 10:19
28

Install with

npm install --save hammerjs

or

yarn add hammerjs

After installing, import it on your app's entry point (e.g. src/main.ts).

import 'hammerjs';

Angular Material Getting Started Guide

NicuVlad
  • 2,491
  • 3
  • 28
  • 47
18

with angular6, you can include hammerjs path in node_modules in angular.json file.

Angular doc says that the purpose of angular.json file is

CLI configuration defaults for all projects in the workspace, including configuration options for build, serve, and test tools that the CLI uses, such as TSLint, Karma, and Protractor. For details, see Angular Workspace Configuration.

You can include hammerjs node module path in the script list. see below for an example:

 "projects": {
    "demo": {
      "root": "",
      "sourceRoot": "src",
      "projectType": "application",
      "prefix": "app",
      "schematics": {},
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "dist/demo",
            "index": "src/index.html",
            "main": "src/main.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "src/tsconfig.app.json",
            "assets": [
              "src/favicon.ico",
              "src/assets",
              "src/manifest.json"
            ],
            "styles": [
              "src/styles.css"
            ],
            "scripts": [
              "node_modules/hammerjs/hammer.min.js" <- add path to hammerjs
            ]
          },
          "configurations": {
          ....

Note that you have to restart ng serve for it to take effect.

Chris Stillwell
  • 10,266
  • 10
  • 67
  • 77
Jun
  • 2,942
  • 5
  • 28
  • 50
0

Project global level addition of hammer js

"scripts": [
              "node_modules/hammerjs/hammer.min.js" <- add path to hammerjs
            ]

Module level Addition

import * as Hammer from 'hammerjs';
sunilbaba
  • 441
  • 2
  • 9
0

For newer versions of Angular Material, when you install Material you just need to run

ng add @angular/material

It will ask you something like this

Set up HammerJS for gesture recognition?

You should answer "Y" and it will automatically configure HammerJS for you.

Angular Material Installation Guide: https://material.angular.io/guide/getting-started#install-angular-material