13

I have project angular-cli

~root~/src/typings.json

{
  "globalDevDependencies": {
    "angular-protractor": "registry:dt/angular-protractor#1.5.0+20160425143459",
    "jasmine": "registry:dt/jasmine#2.2.0+20160621224255",
    "selenium-webdriver": "registry:dt/selenium-webdriver#2.44.0+20160317120654"
  },
  "globalDependencies": {
    "es6-shim": "registry:dt/es6-shim#0.31.2+20160602141504",
    "google.maps": "registry:dt/google.maps#3.20.0+20160914131659"
  }
}

~root~/typings/index.d.ts

/// <reference path="globals/angular-protractor/index.d.ts" />
/// <reference path="globals/es6-shim/index.d.ts" />
/// <reference path="globals/google.maps/index.d.ts" />
/// <reference path="globals/hammerjs/index.d.ts" />
/// <reference path="globals/jasmine/index.d.ts" />
/// <reference path="globals/selenium-webdriver/index.d.ts" />

~root~/src/tsconfig.json

{
  "compilerOptions": {
    "declaration": false,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "lib": ["es6", "dom"],
    "mapRoot": "./",
    "module": "es6",
    "moduleResolution": "node",
    "outDir": "../dist/out-tsc",
    "sourceMap": true,
    "target": "es5",
    "typeRoots": [
      "../node_modules/@types",
      "../typings"
    ],
    "files": [
      "../typings/index.d.ts"
    ]
  }
}

After run ng serve I have error message in console

ERROR in [default] F:~root~\src\app\ui\google-map\map-marker\map-marker.directive.ts:7:26

Cannot find namespace 'google'

and

ERROR in [default] ~root~\src\app\trip-entry-page\trip-entry-page.component.ts:188:21

Cannot find name 'google'

~root~\src\app\ui\google-map\map-marker\map-marker.directive.ts:7:26

... 
@Input() veyoMapMarker: google.maps.MarkerOptions 
...

~root~\src\app\trip-entry-page\trip-entry-page.component.ts:188:21

... 
if (status === google.maps.DirectionsStatus.OK) { 
...

After build app correct working

How me resolve this Error messages?

4
  • Did you find a solution to this issue? I am having the same problem. Commented Dec 12, 2016 at 20:35
  • Did the below answer help you? If so can you mark it as the accepted answer so others can find it. Commented Jan 27, 2017 at 10:26
  • Please refer to my answer here: stackoverflow.com/a/42733315/1087131 I'm using CLI RC0. Commented Mar 11, 2017 at 9:01
  • "ERROR TS6137: Cannot import type declaration files. Consider importing 'googlemaps' instead of '@types/googlemaps'." Commented Apr 12, 2021 at 17:47

9 Answers 9

41

A bit of a late response but I had a similar issue using Angular CLI RC.0.

It turned out that I hadn't install and imported the typings, which can be done as follows:

npm install --save-dev @types/googlemaps

import {} from '@types/googlemaps';
Sign up to request clarification or add additional context in comments.

3 Comments

Aren't typings NOT supposed to be imported? I mean I haven't had to import any other typings I have installed.
Usually I don't import typings, because I have the npm library in my node_modules. This time, we need to download the google maps script at runtime so that's why the typings need to separately installed imported. See my complete answer here: stackoverflow.com/questions/36064697/…
Cannot import type declaration files. Consider importing 'googlemaps' instead of '@types/googlemaps'. But the folder exists node_modules/@types/googlemaps with the index.d.ts file. -.-
10

I was facing same problem and what I did was, I just removed these

import { } from 'googlemaps';
declare var google: any;

from my component.ts and add "types": [ "googlemaps" ] in my tsconfig.app.json . . Now my tsconfig.app.json looks like this.

{
    "extends": "../tsconfig.json",
    "compilerOptions": {
        "outDir": "../out-tsc/app",
        "module": "es2015",
        "types": [
            "googlemaps"
          ]
    },
    "exclude": [
        "test.ts",
        "**/*.spec.ts"
    ]
}

And its working perfectly.

1 Comment

This solved the issue for me in vuejs
7

I run on the same issue but I installed the recommended package from the documentation

npm i -D @types/google.maps

and then in my file or in the main file you can import it as follows

import '@types/google.maps';

this approach solves my issue with the google.maps namespace and its types.

Regards.

2 Comments

Using Vue3 + Vite I had to add import '@types/google.maps' in my src/types/global.d.ts file and it solved the issue. Thanks! Before, I added "types": ["google.maps"] in my tsconfig.app.json file but it only worked for .vue files.
For me none of these solutions worked. Vue3 + Vite on ubuntu. Failed to resolve entry for package "@types/google.maps". The package may have incorrect main/module/exports specified in its package.json. when trying to import from anywhere.
4

For anyone in Vue js facing this issue, you can add the following line at the top of your script section. Make sure you have installed @types/googlemaps.

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

1 Comment

Yes, it works! Thanks. The only difference is that I'm using the @types/google.maps package. As soon as I dropped this comment at the top of my util file, the errors were gone.
3

In IONIC 4 I fixed it by installing @types/google-maps not @types/googlemaps

just run this

npm install @types/google-maps --save

and import google in your component using

import { google } from "google-maps";

Comments

2

For Angular 9.****

I tried installing @types/googlemaps, It did not work for me.

I downgrade it to "@types/googlemaps": "3.39.12" then worked perfectly fine.

this is my code

In tsconfig.app.json (add googlemaps to types array)

"types": [
      "googlemaps"
    ]

In app module.ts

import { AgmCoreModule } from '@agm/core';
.
.
.
.
 imports: [
    BrowserModule,
    AppRoutingModule,
    AgmCoreModule.forRoot({
      apiKey: '...KEY...',
      libraries: ['places']
    })
  ],

To downgrade @types/googlemaps, go to pakage.json file in the project root folder and change @types/googlemaps version to "3.39.12". it's better if you delete files form

node_module -> @types -> googlemaps

Then in the terminal enter

npm install

Comments

2
  1. All you need to do is import the package by:
    npm install @types/google-maps --save
    
  2. Add "types": [ "googlemaps" ] in my tsconfig.app.json . . Now my tsconfig.app.json looks like this:
    {
        "extends": "../tsconfig.json",
        "compilerOptions": {
            "outDir": "../out-tsc/app",
            "module": "es2015",
            "types": [
                "googlemaps"
              ]
        },
        "exclude": [
            "test.ts",
            "**/*.spec.ts"
        ]
    }
    

Comments

1

Try to run the below command in a node prompt...

typings install dt~google.maps --global --save

Comments

0

I had the same problem with angular 7. I did not need to import anything just run npm install @types/googlemaps again and if there is any vulnerabilities, run npm audit fix or npm audit fix --force if needed.

After I did so, everything worked fine for me...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.