239

I am using Angular (version 2) with TypeScript (version 1.6) and when I compile the code I get these errors:

Error TS2304: Cannot find name 'Map'.
    node_modules/angular2/src/core/change_detection/parser/locals.d.ts(4,42): Error TS2304: Cannot find name 'Map'.
    node_modules/angular2/src/core/facade/collection.d.ts(1,25): Error TS2304: Cannot find name 'MapConstructor'.
    node_modules/angular2/src/core/facade/collection.d.ts(2,25): Error TS2304: Cannot find name 'SetConstructor'.
    node_modules/angular2/src/core/facade/collection.d.ts(4,27): Error TS2304: Cannot find name 'Map'.
    node_modules/angular2/src/core/facade/collection.d.ts(4,39): Error TS2304: Cannot find name 'Map'.
    node_modules/angular2/src/core/facade/collection.d.ts(7,9): Error TS2304: Cannot find name 'Map'.
    node_modules/angular2/src/core/facade/collection.d.ts(8,30): Error TS2304: Cannot find name 'Map'.
    node_modules/angular2/src/core/facade/collection.d.ts(11,43): Error TS2304: Cannot find name 'Map'.
    node_modules/angular2/src/core/facade/collection.d.ts(12,27): Error TS2304: Cannot find name 'Map'.
    node_modules/angular2/src/core/facade/collection.d.ts(14,23): Error TS2304: Cannot find name 'Map'.
    node_modules/angular2/src/core/facade/collection.d.ts(15,25): Error TS2304: Cannot find name 'Map'.
    node_modules/angular2/src/core/facade/collection.d.ts(94,41): Error TS2304: Cannot find name 'Set'.
    node_modules/angular2/src/core/facade/collection.d.ts(95,22): Error TS2304: Cannot find name 'Set'.
    node_modules/angular2/src/core/facade/collection.d.ts(96,25): Error TS2304: Cannot find name 'Set'.
    node_modules/angular2/src/core/facade/lang.d.ts(1,22): Error TS2304: Cannot find name 'BrowserNodeGlobal'.
    node_modules/angular2/src/core/facade/lang.d.ts(33,59): Error TS2304: Cannot find name 'Map'.
    node_modules/angular2/src/core/facade/promise.d.ts(1,10): Error TS2304: Cannot find name 'Promise'.
    node_modules/angular2/src/core/facade/promise.d.ts(3,14): Error TS2304: Cannot find name 'Promise'.
    node_modules/angular2/src/core/facade/promise.d.ts(8,32): Error TS2304: Cannot find name 'Promise'.
    node_modules/angular2/src/core/facade/promise.d.ts(9,38): Error TS2304: Cannot find name 'Promise'.
    node_modules/angular2/src/core/facade/promise.d.ts(10,35): Error TS2304: Cannot find name 'Promise'.
    node_modules/angular2/src/core/facade/promise.d.ts(10,93): Error TS2304: Cannot find name 'Promise'.
    node_modules/angular2/src/core/facade/promise.d.ts(11,34): Error TS2304: Cannot find name 'Promise'.
    node_modules/angular2/src/core/facade/promise.d.ts(12,32): Error TS2304: Cannot find name 'Promise'.
    node_modules/angular2/src/core/facade/promise.d.ts(12,149): Error TS2304: Cannot find name 'Promise'.
    node_modules/angular2/src/core/facade/promise.d.ts(13,43): Error TS2304: Cannot find name 'Promise'.
    node_modules/angular2/src/core/linker/element_injector.d.ts(72,32): Error TS2304: Cannot find name 'Map'.
    node_modules/angular2/src/core/linker/element_injector.d.ts(74,17): Error TS2304: Cannot find name 'Map'.
    node_modules/angular2/src/core/linker/element_injector.d.ts(78,184): Error TS2304: Cannot find name 'Map'.
    node_modules/angular2/src/core/linker/element_injector.d.ts(83,182): Error TS2304: Cannot find name 'Map'.
    node_modules/angular2/src/core/linker/element_injector.d.ts(107,37): Error TS2304: Cannot find name 'Map'.
    node_modules/angular2/src/core/linker/proto_view_factory.d.ts(27,146): Error TS2304: Cannot find name 'Map'.
    node_modules/angular2/src/core/linker/view.d.ts(52,144): Error TS2304: Cannot find name 'Map'.
    node_modules/angular2/src/core/linker/view.d.ts(76,79): Error TS2304: Cannot find name 'Map'.
    node_modules/angular2/src/core/linker/view.d.ts(77,73): Error TS2304: Cannot find name 'Map'.
    node_modules/angular2/src/core/linker/view.d.ts(94,31): Error TS2304: Cannot find name 'Map'.
    node_modules/angular2/src/core/linker/view.d.ts(97,18): Error TS2304: Cannot find name 'Map'.
    node_modules/angular2/src/core/linker/view.d.ts(100,24): Error TS2304: Cannot find name 'Map'.
    node_modules/angular2/src/core/linker/view.d.ts(103,142): Error TS2304: Cannot find name 'Map'.
    node_modules/angular2/src/core/linker/view.d.ts(104,160): Error TS2304: Cannot find name 'Map'.
    node_modules/angular2/src/core/render/api.d.ts(281,74): Error TS2304: Cannot find name 'Map'.
    node_modules/angular2/src/core/zone/ng_zone.d.ts(1,37): Error TS2304: Cannot find name 'Zone'.

This is the code:

import 'reflect-metadata';
import {bootstrap, Component, CORE_DIRECTIVES, FORM_DIRECTIVES} from 'angular2/core';
@Component({
  selector: 'my-app',
  template: '<input type="text" [(ng-model)]="title" /><h1>{{title}}</h1>',
  directives: [ CORE_DIRECTIVES ]
})
class AppComponent {
  title :string;

  constructor() {
    this.title = 'hello angular 2';
  }
}
bootstrap(AppComponent);
live-love
  • 48,840
  • 22
  • 240
  • 204
user233232
  • 6,067
  • 9
  • 29
  • 37
  • looks like there is an issue for that here https://github.com/angular/angular/issues/4902 – rob Oct 26 '15 at 18:33
  • 1
    Try adding the following import `import {ROUTER_PROVIDERS} from 'angular2/router';` I'm getting the same issue and for some reason this fixes it for me...?!? – rob Oct 26 '15 at 19:01

25 Answers25

111

There is a work-around mentioned in the changelog for 2.0.0-beta.6 (2016-02-11) (listed under breaking changes):

If you use --target=es5, you will need to add a line somewhere in your application (for example, at the top of the .ts file where you call bootstrap):

///<reference path="node_modules/angular2/typings/browser.d.ts"/>

(Note that if your file is not in the same directory as node_modules, you'll need to add one or more ../ to the start of that path.)

make sure you have the correct reference path, i needed to add ../ to the start to get this working.

mvdluit
  • 1,144
  • 2
  • 8
  • 6
  • 3
    This fixed a lot of errors for me, but I still get errors like `TS2304: Cannot find name 'module'` and `TS2339: Property 'find' does not exist on type 'MyThing[]'.`... Any ideas? – mwijnands Feb 24 '16 at 22:02
  • This worked for me, no errors using angular2 beta.8 version. – Tiberiu Popescu Mar 07 '16 at 01:07
  • 1
    What if you use angular in a package meant for reuse? Any application I try to use a package with a file with this reference complains at tsc build time that it cannot find this reference. – Hans Mar 08 '16 at 17:58
  • try to add /// reference for the `angular.d.ts` too – Ceylan Mumun Kocabaş Apr 25 '16 at 12:56
  • the missing ../ was what was getting me. Thanks! – James Apr 28 '16 at 13:59
  • 6
    Is there a solution for the new package structure in Angular, I can't find a browser.d.ts in the new @angular/.... structure – I.devries Jun 15 '16 at 10:01
  • 2
    @I.devries Correct, they have removed browser.d.ts. The files are now index.d.ts, and you need one for each angular component, as each is now installed separately in rc.0 and later. You also need to install "typings" -- see answers by Khaled Al-Ansari and theUtherSide above. – Vern Jensen Jun 23 '16 at 02:31
  • 1
    I solved it by adding: /// – Sako73 Aug 31 '16 at 15:24
  • I solved it by adding /// (please note the dots ../../ (you can try with only ../ and if this does work then try adding more (.../) until this works – angel Sep 29 '16 at 21:00
  • This is ridiculous as it assumes you are using typings. – Léon Pelletier Oct 21 '16 at 21:00
83

ES6 features like promises aren't defined when targeting ES5. There are other libraries, but core-js is the javascript library that the Angular team uses. It contains polyfills for ES6.

Angular 2 has changed a lot since this question was asked. Type declarations are much easier to use in Typescript 2.0.

npm install -g typescript

For ES6 features in Angular 2, you don't need Typings. Just use typescript 2.0 or higher and install @types/core-js with npm:

npm install --save-dev @types/core-js

Then, add the TypeRoots and Types attributes to your tsconfig.json:

{
  "compilerOptions": {
    "target": "es5",
    "module": "es6",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "removeComments": false,
    "noImplicitAny": false,
    "typeRoots": [
      "../node_modules/@types"
    ],
    "types" : [
      "core-js"
    ]
  },
  "exclude": [
    "node_modules"
  ]
}

This is much easier than using Typings, as explained in other answers. See Microsoft's blog post for more info: Typescript: The Future of Declaration Files

David Rinck
  • 6,637
  • 4
  • 45
  • 60
  • 1
    I think the "types" array also belongs into the "compilerOptions" Object. But other than that, thanks alot, I was struggling with this fot the whole day. – plexus Sep 27 '16 at 18:09
  • @plexus Thanks! You are right about the compilerOptions. I fixed the answer. – David Rinck Sep 27 '16 at 20:26
  • Thanks for great post, but according to the URL you provided it is not even necessary to augment tsconfig.json, it just really works anyway :) At least it did for me. So the only thing you need to do is to install the required package – Ilya Chernomordik Oct 05 '16 at 08:21
  • You can even remove `typeRoots` if the only entry you have is `node_modules/@types`. – Morgan Touverey Quilling Jan 03 '17 at 23:49
  • Like a charm. As @ilya-chernomordik had mentioned, this also worked for me without the addition of `TypeRoots` and `Types` to tsconfig.json. In my case I also had to update my Gulp gulp-typescript 2.x plugin which was transpiling using and embedded version of TypeScript 1, but once I updated so I was using TypeScript 2 and added the @types/core-js polyfill I was set. Many thanks. – rscarter Jan 31 '17 at 20:54
57

A known issue: https://github.com/angular/angular/issues/4902

Core reason: the .d.ts file implicitly included by TypeScript varies with the compile target, so one needs to have more ambient declarations when targeting es5 even if things are actually present in the runtimes (e.g. chrome). More on lib.d.ts

basarat
  • 261,912
  • 58
  • 460
  • 511
50

For those following the Angular2 tutorial on angular.io just to be explicit, here is an expansion of mvdluit's answer of exactly where to put the code:

Your main.ts should look like this:

/// <reference path="../node_modules/angular2/typings/browser.d.ts" />

import {bootstrap} from 'angular2/platform/browser'
import {AppComponent} from './app.component'
// Add all operators to Observable
import 'rxjs/Rx'

bootstrap(AppComponent);

Note that you leave in the /// forward slashes, don't remove them.

ref: https://github.com/ericmdantas/angular2-typescript-todo/blob/master/index.ts#L1

Rots
  • 5,506
  • 3
  • 43
  • 51
  • I use VS2015 MVC6 and Angular2 beta.14, If you place the reference line in `_references.js` it won't work. Has to be inside of component like this answer suggests. Additionally, use `../../` instead of `../` for the path. –  Apr 12 '16 at 22:55
  • @Hani did you find a way around it yet? I'll try upgrade to rc1 and try it out – Rots May 10 '16 at 23:42
  • So the typings are gone. I guess you'll need to install that separately and when i do, i get a lot of useless packages added as well. Anyhow, even after doing all of that, there is no more `browser.d.ts` under typings folder. Just `.js` files under `dist`. There is a file named `typings.d.ts` so i thought that's the new one, but it didn't solve the issue. Building for 'es6' works, but IE complains. So stuck with 'es5' build and no typings! :/ –  May 11 '16 at 14:54
  • @Hani There is a new answer. Perhaps it will work? `npm install -g typings typings install` – Rots May 15 '16 at 10:38
  • Downgraded to beta.15 and used the same `.d.ts` from typings package. Also to meet IE9+ requirement, had to build for es3. –  May 15 '16 at 19:11
49

I was able to fix this with the following command

typings install es6-promise es6-collections --ambient

Note that you need typings to make the above command work, if you do not have it run the following command to install it

npm install -g typings

UPDATE

typings update doesn't read --ambient it became --global also for some people you need to install the definitions of the above libraries, just use the following command

typings install dt~es6-promise dt~es6-collections --global --save

Thanks to @bgerth for pointing this out.

Khaled Al-Ansari
  • 3,910
  • 2
  • 24
  • 27
  • 9
    For me it was `typings install dt~es6-promise dt~es6-collections --global --save` – bgerth Jul 13 '16 at 04:33
  • 1
    @bgerth I guess it changed because of the `typings` update, I'll add your solution to the answer – Khaled Al-Ansari Jul 13 '16 at 08:07
  • typings install dt~es6-promise dt~es6-collections --global --save worked for me. This problem is recurring and intermittent... thx for this fix lets hope it works longer than a day. –  Jul 19 '16 at 14:52
  • typings install dt~es6-promise dt~es6-collections --global --save worked for me too – Rikku121 Jul 20 '16 at 18:09
32

When having Typescript >= 2 the "lib" option in tsconfig.json will do the job. No need for Typings. https://www.typescriptlang.org/docs/handbook/compiler-options.html

{
    "compilerOptions": {
        "target": "es5",
        "lib": ["es2016", "dom"] //or es6 instead of es2016(es7)
    }
}
Niels Steenbeek
  • 4,692
  • 2
  • 41
  • 50
  • Yup...adding "lib": ["es2016", "dom"] to my tsconfig.json file fixed it – Marvel Moe Apr 16 '17 at 19:37
  • This is correct. Just use caution because some of the types declared therein may _not_ be present. – Aluan Haddad Apr 24 '17 at 05:39
  • @Niels Steenbeek has it correct here. I encountered the same errors doing the exercises for Angular 2 & 4 courses. However, my lib contents where slightly different: "lib" : ["es2015", "es2015.iterable", "dom"] – BoiseBaked Oct 06 '17 at 16:45
15

For Angular 2.0.0-rc.0 adding node_modules/angular2/typings/browser.d.ts won't work. First add typings.json file to your solution, with this content:

{
    "ambientDependencies": {
        "es6-shim": "github:DefinitelyTyped/DefinitelyTyped/es6-shim/es6-shim.d.ts#7de6c3dd94feaeb21f20054b9f30d5dabc5efabd"
    }
}

And then update the package.json file to include this postinstall:

"scripts": {
    "postinstall": "typings install"
},

Now run npm install

Also now you should ignore typings folder in your tsconfig.json file as well:

 "exclude": [
        "node_modules",
        "typings/main",
        "typings/main.d.ts"
    ]

Update

Now AngularJS 2.0 is using core-js instead of es6-shim. Follow its quick start typings.json file for more info.

VahidN
  • 18,457
  • 8
  • 73
  • 117
14

you can add the code at the beginning of .ts files.

/// <reference path="../typings/index.d.ts" />
user3543469
  • 198
  • 2
  • 6
10

I was getting this on Angular 2 rc1. Turns out some names changed with typings v1 vs the old 0.x. The browser.d.ts files became index.d.ts.

After running typings install locate your startup file (where you bootstrap) and add:

/// <reference path="../typings/index.d.ts" /> (or without the ../ if your startup file is in the same folder as the typings folder)

Adding index.d.ts to the files list in tsconfig.json did not work for some reason.

Also, the es6-shim package was not needed.

mbursill
  • 2,911
  • 1
  • 32
  • 44
8

I was able to fix this by installing the typings module.

npm install -g typings
typings install

(Using ng 2.0.0-rc.1)

theUtherSide
  • 3,338
  • 4
  • 36
  • 35
  • 1
    "'typyings.json' is missing. -- (No dependencies)". It returns this error when I run the command in the same folder of the app.ts file. Where am I supposed to find this *typings.json* file, and when should I run the command? – Ulysses Alves Feb 24 '17 at 16:49
7

I had the same problem and I solved it using the lib option in tsconfig.json. As said by basarat in his answer, a .d.ts file is implicitly included by TypeScript depending on the compile targetoption but this behaviour can be changed with the lib option.

You can specify additional definition files to be included without changing the targeted JS version. For examples this is part of my current compilerOptions for Typescript@2.1 and it adds support for es2015 features without installing anything else:

"compilerOptions": {
    "experimentalDecorators": true,
    "lib": ["es5", "dom", "es6", "dom.iterable", "scripthost"],
    "module": "commonjs",
    "moduleResolution": "node",
    "noLib": false,
    "target": "es5",
    "types": ["node"]
}

For the complete list of available options check the official doc.

Note also that I added "types": ["node"] and installed npm install @types/node to support require('some-module') in my code.

Community
  • 1
  • 1
Jiayi Hu
  • 2,210
  • 1
  • 14
  • 12
5
 typings install dt~es6-shim --save --global

and add the correct path to index.d.ts

///<reference path="../typings/index.d.ts"/>

Tried on @angular-2.0.0-rc3

Ali Salehi
  • 6,899
  • 11
  • 49
  • 75
4

If npm install -g typings typings install still does not help, delete node_modules and typings folders before executing this command.

tibersky
  • 39
  • 4
4

This is that think how everyone is trying to do something different. I believe these systems still far from the final version.

In my case, I updated from rc.0 to rc.5 this error appeared.

My fixed was change the tsconfig.json.

{
    "compilerOptions": {
        "target": "es6", <-- It was es5
        "module": "system",
        "moduleResolution": "node",
        "sourceMap": true,
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "removeComments": false,
        "noImplicitAny": false,
        "outDir": "../wwwroot/app"
    },
    "compileOnSave": true,
    "exclude": [
        "../node_modules",
        "../typings/main"
    ]
}
AFetter
  • 3,355
  • 6
  • 38
  • 62
4

add typing.d.ts in main folder of the application and over there declare the varible which you want to use every time

declare var System: any;
declare var require: any;

after declaring this in typing.d.ts, error for require will not come in the application..

NARGIS PARWEEN
  • 1,489
  • 3
  • 16
  • 26
4

I am new to Angular but for me the solution was found by simply importing Input. Can't take credit for this one, found it on another board. This is a simple fix if you're having the same problem but if your issues are more complex I'd read the stuff above.

Mukesh:

you have to import input like this at top of child component

import { Directive, Component, OnInit, Input } from '@angular/core';
Manfred Radlwimmer
  • 13,257
  • 13
  • 53
  • 62
Tripp Cannella
  • 1,094
  • 1
  • 8
  • 9
3

I found this very helpful doc at Angular 2's website. It finally let me get things working properly, whereas the other answers here, to install typings, failed me with various errors. (But helped lead me in the right direction.)

Instead of including es6-promise and es6-collections, it includes core-js, which did the trick for me... no conflicts with Angular2's core ts definitions. Additionally the document explained how to set up all this to happen automatically when installing NPM, and how to modify your typings.json file.

Vern Jensen
  • 3,449
  • 6
  • 42
  • 59
3

I was getting this error after merging my dev branch to my current branch. I spent sometime to fix the issue. As you can see in the below image, there is no problem in the codes at all.

enter image description here

So the only fix worked for me is that Restarting the VSCode

Community
  • 1
  • 1
Sibeesh Venu
  • 18,755
  • 12
  • 103
  • 140
2

Angular 2 RC1 renamed the node_modules/angular2 directory to node_modules/angular.

If you're using a gulpfile to copy files to an output directory you probably still have node_modules/angular sitting in there which may be getting picked up by the compiler and confusing the hell out of itself.

So (carefully) wipe out what you have in node_modules that is for the beta versions, and also delete any old typings and re-run typings install.

Simon_Weaver
  • 140,023
  • 84
  • 646
  • 689
  • or alternatively... work on other stuff for 4 months and wait for the final release to start over from scratch (which is what I'm doing) – Simon_Weaver Sep 24 '16 at 20:56
2

The accepted answer doesn't provide a viable fix, and most of the other ones suggest the "triple-slashes" workaround which is not viable anymore, since the browser.d.ts has been removed by the Angular2 latest RC's and thus is not available anymore.

I strongly suggest to install the typings module as suggested by a couple solutions here, yet it's not necessary to do it manually or globally - there's an effective way to do that for your project only and within VS2015 interface. Here's what you need to do:

  • add typings in the project's package.json file.
  • add a script block in the package.json file to execute/update typings after each NPM action.
  • add a typings.json file in the project's root folder containing a reference to core-js (overall better than es6-shim atm).

That's it.

You can also take a look to this other SO thread and/or read this post on my blog for additional details.

Darkseal
  • 9,205
  • 8
  • 78
  • 111
2

Do import the below statement in your JS file.

import 'rxjs/add/operator/map';
Shivang Gupta
  • 3,139
  • 1
  • 25
  • 24
  • I'm putting off upgrading a project to latest version, so added `import './rxjs-extensions';` to my `app.component.ts` with the rxjs-extensions file from the angular2 tutorial (including your suggested line) and it seems to have removed those errors. – James Oct 07 '16 at 13:23
  • Could you please be more specific. Which JS file? the one compiled by Typescript? main? – mm_ May 15 '17 at 16:04
1

Good call, @VahidN, I found I needed

    "exclude": [
        "node_modules",
        "typings/main",
        "typings/main.d.ts"
    ]

In my tsconfig too, to stop errors from es6-shim itself

Koopakiller
  • 2,838
  • 3
  • 32
  • 47
1

This could be because you are missing an import.

Example:

ERROR in src/app/products/product-list.component.ts:15:15 - error TS2304: Cannot find name 'IProduct'.

Make sure you are adding the import at the top of the file:

import { IProduct } from './product';

...

export class ProductListComponent {
    pageTitle: string = 'product list!';
    imageWidth: number = 50;
    imageMargin: number = 2;
    showImage: boolean = false;
    listFilter: string = 'cart';
    products: IProduct[] = ... //cannot find name error
live-love
  • 48,840
  • 22
  • 240
  • 204
0

Now you must import them manually.

import 'rxjs/add/operator/retry';
import 'rxjs/add/operator/timeout';
import 'rxjs/add/operator/delay';
import 'rxjs/add/operator/map';




this.http.post(url, JSON.stringify(json), {headers: headers, timeout: 1000})

         .retry(2)

         .timeout(10000, new Error('Time out.'))

         .delay(10)

         .map((res) => res.json())
        .subscribe(
          (data) => resolve(data.json()),
          (err) => reject(err)
        );
Jithesh Chandra
  • 860
  • 7
  • 12
-3

Update tsconfig.json, change

"target": "es5"

to

"target": "es6"
General Failure
  • 2,421
  • 4
  • 23
  • 49