Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

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?

share|improve this question
up vote 1 down vote accepted

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

share|improve this answer
    
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. stackoverflow.com/questions/33332394/… – Yohsuke Inoda Feb 22 at 16:33

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.