1

I want to use Angular 2 with TypeScript and compile to JavaScript using gulp-typescript. But I got error Cannot find module 'angular2/angular2'.. Then, I tried to change code like the following.

Code

/// <reference path="../../../node_modules/angular2/core.d.ts" />
import {bootstrap, Component} from 'angular2/angular2';

@Component({
  selector: 'my-app',
  template: '<h1>My First Angular 2 App</h1>'
})

class AppComponent { }

bootstrap(AppComponent);

I got below stacktrace

app/assets/scripts/application.ts(2,36): error TS2307: Cannot find module 'angular2/angular2'.
/Users/xxxx/app/node_modules/angular2/src/core/application_ref.d.ts(83,60): error TS2304: Cannot find name 'Promise'.
/Users/xxxx/app/node_modules/angular2/src/core/application_ref.d.ts(83,146): error TS2304: Cannot find name 'Promise'.
/Users/xxxx/app/node_modules/angular2/src/core/application_ref.d.ts(96,51): error TS2304: Cannot find name 'Promise'.
/Users/xxxx/app/node_modules/angular2/src/core/application_ref.d.ts(96,147): error TS2304: Cannot find name 'Promise'.
/Users/xxxx/app/node_modules/angular2/src/core/application_ref.d.ts(133,90): error TS2304: Cannot find name 'Promise'.
/Users/xxxx/app/node_modules/angular2/src/core/application_ref.d.ts(171,81): error TS2304: Cannot find name 'Promise'.

The following are configuration for gulp-typescript and typings (instead of tsd), tsconfig.

// gulpfile.js

...
gulp.task('ts', function() {
  var tsconfig = require('./tsconfig.json');

  gulp
    .src(path.ts)
    .pipe($.typescript(tsconfig.compilerOptions))
    .pipe($.concat('application.js'))
    .pipe(gulp.dest('dist/assets/'));
});
...

// typings.json
{
  "dependencies": {},
  "devDependencies": {},
  "ambientDependencies": {
    "es6-shim": "github:DefinitelyTyped/DefinitelyTyped/es6-shim/es6-shim.d.ts#6697d6f7dadbf5773cb40ecda35a76027e0783b2"
  }
}

// tsconfig.json
{
  "compilerOptions": {
    "target"                : "es5",
    "module"                : "commonjs",
    "sourceMap"             : false,
    "emitDecoratorMetadata" : true,
    "experimentalDecorators": true,
    "removeComments"        : false,
    "noImplicitAny"         : false
  },
  "exclude": [
    "node_modules"
  ]
}

How to compile TypeScript using gulp-typescript in order to use Angular 2?

Pankaj Parkar
  • 134,766
  • 23
  • 234
  • 299
Yohsuke Inoda
  • 521
  • 1
  • 6
  • 20

1 Answers1

1

Error caused because module loader doesn't find angular2/angular2 module. May be while developing an app, you have referred to old tutorial, where alpha version is used & you are using beta version while developing it.

As per new Angular2 beta releases, you should use angular2/core module instead of angular2/angular2. Because most of the has been separated out into different modules.

/// <reference path="../../../node_modules/angular2/core.d.ts" />
import { Component } from 'angular2/core';
import {bootstrap}   from 'angular2/platform/browser';

@Component({
  selector: 'my-app',
  template: '<h1>My First Angular 2 App</h1>'
})

class AppComponent { }

bootstrap(AppComponent);

Also refer this answer for more information

Plunkr in action

Community
  • 1
  • 1
Pankaj Parkar
  • 134,766
  • 23
  • 234
  • 299
  • Thank you! I can transpile it by now, but I noticed Angular 2 beta 7 which has bugs cause compiling errors in es5 mode. So when I changed compiling mode to es6 from es5, I can it. http://stackoverflow.com/questions/33332394/angular-2-typescript-cant-find-names – Yohsuke Inoda Feb 22 '16 at 16:33