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?
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.