@types/googlemaps/index.d.ts' is not a module

AngularTypescriptAngular CliAngular Cli-V6

Angular Problem Overview


I want to use the Google Maps API with my Angular project, so I used these two commands to install npm packages:

npm install @agm/core --save-dev
npm install @types/googlemaps --save-dev

I added this line to my component:

import {} from "@types/googlemaps";

But I see these 2 errors in VS Code:

[ts] File 'h:/Angular Projects/Breakfast/client/breakfast/node_modules/@types/googlemaps/index.d.ts' is not a module.
[ts] Cannot import type declaration files. Consider importing 'googlemaps' instead of '@types/googlemaps'.

I added these lines

"types": ["googlemaps"]
"moduleResolution": "node"

to tsconfig.json and tsconfig.spec.json, but still no luck. On Chrome Dev Tools, I see the below error:

Error: Uncaught (in promise): TypeError: Cannot read property 'Autocomplete' of undefined
TypeError: Cannot read property 'Autocomplete' of undefined

Angular version 6 Typescript Version 2.9.2

I tried from Angular 5, too.

Angular Solutions


Solution 1 - Angular

Thanks to this documentation link : https://www.typescriptlang.org/docs/handbook/triple-slash-directives.html

[Angular 6+] You only have to add this line at the beginning (meaning line 1, with nothing before) of your Typescript file :

/// <reference types="@types/googlemaps" />

[Angular 5-] You only have to add this line anywhere in your Typescript file imports :

import {} from "googlemaps";

Thanks to the answer below, you may also need to add a file <root>/index.d.ts containing (didn't need it though in my case) :

declare module 'googlemaps';

Solution 2 - Angular

The import can be simplified as follows:

import {} from "googlemaps";

Create a file at your projects root directory named index.d.ts and paste the following:

declare module 'googlemaps';

The created file needs to be located directory in the src folder

I found this article about what is the purpose of that file

https://www.bennadel.com/blog/3169-adding-custom-typings-files-d-ts-in-an-angular-2-typescript-application.htm

Solution 3 - Angular

In my Angular 7+ project

$ npm install @types/googlemaps --save-dev
In tsconfig.app.json

"types": [
      "googlemaps"
]

Thank you the link below https://www.freakyjolly.com/angular-7-6-add-google-maps-in-angular-2-plus-applications-using-angular-google-maps-module-agm-core-easily/#more-2316

Solution 4 - Angular

I just created a index.d.ts in my src folder and added

> declare module 'googlemaps';

It solved the issue

Solution 5 - Angular

It works fine

npm install --save-dev @types/googlemaps
At the beggining of your component file, type:
/// <reference types="@types/googlemaps" />

Solution 6 - Angular

For me in Angular 6, it worked when I only used

/// <reference types="@types/googlemaps" />

Solution 7 - Angular

In my angular 6+ project I've solved the problem declaring the googlemaps namespace in the top of the typescript file with this line:

/// <reference path="../../../../../../node_modules/@types/googlemaps/index.d.ts"/>

with this done you must not import googlemaps in other ways and then it works. Use the correct path to your node_modules folder.

For further information and references about namespace usage in Typescript here the documentation.

Solution 8 - Angular

I have tried this solution I think it is the best because I didn't need to edit at packages and someone writes it without classification

  1. npm install @types/googlemaps --save-dev
  2. add "compilerOptions": { "types": ["googlemaps"] } in tsconfig.app.json file
  3. Dont forget to remove import {} from 'googlemaps'; from your code.

FYI: you must restart the server ng serve

Solution 9 - Angular

You can avoid this error next way:

After you have installed >npm install @types/googlemaps --save-dev

Go to src/tsconfig.app.json and add next line:

"compilerOptions": {
    ...,
    "types": [
         "googlemaps"
    ],
    ...,
},

Solution 10 - Angular

It is not on the root. You just need to add the code below on this file: node_modules/@types/googlemaps/index.d.ts

declare module 'googlemaps';

Solution 11 - Angular

We already had

"typeRoots": [
  "node_modules/@types"
],

So adding

declare var google;

Was all we needed to add to the module

Solution 12 - Angular

    import { MapsAPILoader } from '@agm/core';
    declare var google;
    
    
     constructor(private mapsAPILoader: MapsAPILoader) {
         this.mapsAPILoader.load().then(() => {
              var mapProp = {
                center: new google.maps.LatLng(9.93040049002793, -84.09062837772197),
                zoom: 15,
                mapTypeId: google.maps.MapTypeId.ROADMAP
              };
              this.map = new google.maps.Map(this.gmapElement.nativeElement, mapProp);
            });
        
          }
    
    //Hope it helps

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAMendisView Question on Stackoverflow
Solution 1 - AngularJsctiView Answer on Stackoverflow
Solution 2 - AngularStephen PaulView Answer on Stackoverflow
Solution 3 - AngularAnurak BoonyaritpanitView Answer on Stackoverflow
Solution 4 - AngularCool CoderView Answer on Stackoverflow
Solution 5 - AngularRaghulraj PalanisamyView Answer on Stackoverflow
Solution 6 - AngularCamQuestView Answer on Stackoverflow
Solution 7 - Angularbertonc96View Answer on Stackoverflow
Solution 8 - AngularAlbazView Answer on Stackoverflow
Solution 9 - AngularMykhailo BurdaView Answer on Stackoverflow
Solution 10 - AngularLeon GrinView Answer on Stackoverflow
Solution 11 - AngularIanView Answer on Stackoverflow
Solution 12 - AngularJeremy Bacca BarqueroView Answer on Stackoverflow