23

I am developing an node app with angular2 and gulp. I have written a component file login.ts as follows:

import {Component, View} from 'angular2/angular2';
import {FormBuilder,  formDirectives } from 'angular2/forms';
@Component({
  selector: 'login',
  injectables: [FormBuilder]
})
@View({
  templateUrl: '/scripts/src/components/login/login.html',
  directives: [formDirectives]
})

export class login {

}

And my bootstrap.ts file is:

import {bootstrap} from 'angular2/angular2';

import {login} from './components/login/login';

bootstrap(login);

but when I compile these files it gives me the following errors:

client\bootstrap.ts(1,25): error TS2307: Cannot find module 'angular2/angular2'.
client\components\login\login.ts(1,31): error TS2307: Cannot find module 'angular2/angular2
client\components\login\login.ts(2,45): error TS2307: Cannot find module 'angular2/forms'.

Here is my tsconfig.json:

{
    "compilerOptions": {
        "target": "es5",
        "module": "commonjs",
        "sourceMap": true,
        "watch": true,
        "removeComments": true,
        "moduleResolution": "node",
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true
    }
}

I have installed the typing for angular2 using:

tsd install angular2 --save

Please help to fix the error.

2
  • 1
    @View was dropped so this code is no longer valid in Angular2 Commented Apr 7, 2016 at 10:32
  • 1
    I think its important to mention... There is a difference between importing from 'angular2' vs '@angular'. If you are using cdn (i.e., <script src="https://code.angularjs.org/<version>angular2.dev.js">), then you would import from 'angular2`. If you're installing angular2 as a dependency from npm, then you would import '@angular'. Also, Angular2 is currently in rc4 and there are a lot of changes that are happening. To get the latest changes for Angular2, I would recommend taking a look at the 5 Min Quickstart on the angular.io website. Commented Jul 21, 2016 at 21:27

11 Answers 11

34

yups as said by @simon error is in imports. but for further imports i have posted this answer may be this is useful for others too.

import {Component, View, Directive, Input, Output, Inject, Injectable, provide} from 'angular2/core'; 

import {bootstrap} from 'angular2/platform/browser';

import {CORE_DIRECTIVES, FORM_DIRECTIVES, NgClass, NgIf  NgForm, Control, ControlGroup, FormBuilder, Validators} from 'angular2/common';

import {RouteConfig, ROUTER_DIRECTIVES, ROUTER_PROVIDERS, Router, LocationStrategy, HashLocationStrategy} from 'angular2/router';

import {Http, HTTP_PROVIDERS, RequestOptions, Headers, Request, RequestMethod} from 'angular2/http'

update -

@View is no more in angular2 now, so no need to import

import {view} from 'angular2/core'

Update 2

As of angular2 is in RC so there is breaking change in all imports here is the list if all updated imports -

angular2/core -> @angular/core
angular2/compiler -> @angular/compiler
angular2/common -> @angular/common
angular2/platform/common -> @angular/common
angular2/common_dom -> @angular/common
angular2/platform/browser -> @angular/platform-browser-dynamic
angular2/platform/server -> @angular/platform-server
angular2/testing -> @angular/core/testing
angular2/upgrade -> @angular/upgrade
angular2/http -> @angular/http
angular2/router -> @angular/router
angular2/platform/testing/browser -> @angular/platform-browser-dynamic/testing
Sign up to request clarification or add additional context in comments.

5 Comments

if i do so /node_modules/angular2/typings/node/node.d.ts getting conflicted with typings\node\node.d.t
typings\node\node.d.ts is need for the express, mongoose
sorry i don't get you, but if you are talking about typings according to me there is no need to use typings in angular2 now because this is predefined in the librarys of angular2
I'm pretty sure you don't need to import NgClass and NgIf since these are built into Angular2.
yeah definitely @kimbaudi you don't need to import those but posting just for reference purpose.
24

It changed module just ahead of going beta to

import {Component, View} from 'angular2/core';

FYI: bootstrap also changed to

import {bootstrap} from 'angular2/platform/browser';

as a result a lot of the blog posts on the net are out of date :-(

4 Comments

if i do so /node_modules/angular2/typings/node/node.d.ts getting conflicted with typings\node\node.d.ts
typings\node\node.d.ts is need for the express, mongoose
You may want to edit and repost your question, as your original question has been solved and your title won't attrat the typescript experts you need
i think it changed from import {bootstrap} from 'angular2/platform-browser'; to `import {bootstrap} from 'angular2/platform-browser-dynamic';
4

As per the new release of Angular2 rc1, you can update your package.json to

"dependencies": {
    "@angular/common": "2.0.0-rc.1",
    "@angular/compiler": "2.0.0-rc.1",
    "@angular/core": "2.0.0-rc.1",
    "@angular/http": "2.0.0-rc.1",
    "@angular/platform-browser": "2.0.0-rc.1",
    "@angular/platform-browser-dynamic": "2.0.0-rc.1",
    "@angular/router": "2.0.0-rc.1",
    "@angular/router-deprecated": "2.0.0-rc.1",
    "@angular/upgrade": "2.0.0-rc.1",
    .....
 }

In addition to this also import as following in your component or container:

import { Component } from '@angular/core';
import {ROUTER_DIRECTIVES, Router, RouteParams} from '@angular/router-deprecated';

Comments

3

Please note that as form Angular2 final release the correct answer is this.

import {Component, View, Directive, Input, Output, Inject, Injectable, provide} from 'angular/core'; 

import {bootstrap} from 'angular/platform/browser';

import {CORE_DIRECTIVES, FORM_DIRECTIVES, NgClass, NgIf  NgForm, Control, ControlGroup, FormBuilder, Validators} from 'angular/common';

import {RouteConfig, ROUTER_DIRECTIVES, ROUTER_PROVIDERS, Router, LocationStrategy, HashLocationStrategy} from 'angular/router';

import {Http, HTTP_PROVIDERS, RequestOptions, Headers, Request, RequestMethod} from 'angular/http'

This is true as said in yups update 2 from above

angular2/core -> @angular/core
angular2/compiler -> @angular/compiler
angular2/common -> @angular/common
angular2/platform/common -> @angular/common
angular2/common_dom -> @angular/common
angular2/platform/browser -> @angular/platform-browser-dynamic
angular2/platform/server -> @angular/platform-server
angular2/testing -> @angular/core/testing
angular2/upgrade -> @angular/upgrade
angular2/http -> @angular/http
angular2/router -> @angular/router
angular2/platform/testing/browser -> @angular/platform-browser-dynamic/testing

Comments

3

You are trying to access Component from wrong/old directory of node_modules. Please access Component from

import { Component, OnInit } from '@angular/core';

instead of : import { Component, View } from 'angular2/angular2';

AND

Access bootstrap from bellow path :

import { bootstrap } from 'angular2/platform/browser';

Comments

2

you have to Update Angular2 version in final version --

And then use like ----

import { Component } from '@angular/core';

there is a updated library like --- '@angular/core'

Comments

0

'angular2/angular2' is not a valid dependencies of angular2

you have to first check package.json file "@angular/core" exist or not

"dependencies": {
    "@angular/common": "2.0.0",
    "@angular/compiler": "2.0.0",
    "@angular/core": "2.0.0",
    "@angular/http": "2.0.0",
    "@angular/platform-browser": "2.0.0",
    "@angular/platform-browser-dynamic": "2.0.0",
    "@angular/router": "2.0.0",
    "@angular/router-deprecated": "2.0.0",
    "@angular/upgrade": "2.0.0",
    .....
 }

see the component file like this and also you have too use formGroup as belove

    import { Component, OnInit, DoCheck } from '@angular/core'
    import { Router } from '@angular/router'

    import { FormBuilder, FormGroup, Validators, FormControl } from '@angular/forms'

    @Component({
      selector: 'app-user-profile',
      templateUrl: './user-profile.component.html',
      styleUrls: ['./user-profile.component.scss'],
    })
    export class UserProfileComponent implements OnInit, DoCheck {
      profileForm: FormGroup

      constructor(private fb: FormBuilder) {
        this.profileForm = this.fb.group({
          firstName: ['', Validators.required],
          lastName: ['', Validators.required],
          email: ['', Validators.required],
          mobileNo: ['', Validators.required]
        });
    }

than you have to import ReactiveFormsModule in app.module.ts file
import { ReactiveFormsModule } from '@angular/forms';

without ReactiveFormsModule formGroup not work it make error

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { UserProfileComponent } from './user-profile.component';

import { UserProfileRoutingModule } from './user-profile-routing.module';
import { ReactiveFormsModule } from '@angular/forms';

@NgModule({
  imports: [
    CommonModule,
    UserProfileRoutingModule,
    ReactiveFormsModule
  ],
  declarations: [UserProfileComponent]
})

Comments

0

The best way to kick of angular2 project is using Angular CLI.

The Angular CLI makes it easy to create an application that already works, right out of the box. It already follows our best practices!

please check this for more details.

Comments

0

import from ''angular2/angular2' was in previous version which no longer supported now .So Now we have to import the same from 'angular2/core'.For detail reference use (https://angular.io/guide/quickstart)

1 Comment

yup, quickstart is the best way to stay up-to-date with changes in Angular2
0
import {Component} from "@angular/core";

@Component({
  selector: "userForm",
  template: '<div><input type="text" name="name" [disabled]="flag"/></div>'
});

export class userForm{
 public flag = false; //boolean value: true/false
}

Comments

-1
  • First update your angular2 liberies at packege.json.
  • Second

     import {Component} from "@angular/core";
     import {FormBuilder } from "@angular/forms";
    
     @Component({
      selector: "login",
      providers: [FormBuilder],
      templateUrl: "/scripts/src/components/login/login.html"
     })
    
    
     export class login {
    
     }
    

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.