Cannot find module 'angular2/angular2' - node.js

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.

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

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 :-(

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';

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

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';

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'

'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]
})

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.

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)

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
}

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 {
}

Related

How to import AXIOS into NestJS-application the right way?

Importing HttpService from AXIOS into NestJS-Application seems to be a little mess at the moment.
When importing HttpModule like suggest on so many other SO-pages like
import { HttpService } from '#nestjs/axios';
it will state an error message
Potential solutions:
- If HttpService is a provider, is it part of the current WalletsModule?
- If HttpService is exported from a separate #Module, is that module imported within WalletsModule?
#Module({
imports: [ /* the Module containing HttpService */ ]
})
Instead, one should use the following import:
import { HttpService } from '#nestjs/common';
Even the import of HttpModule needs to be imported from the common-namespace. Otherwise it will still give the same error message.
However, this one is already marked as deprecated and says that one should import from #nestjs/axios instead, which leads to the already mentioned import error above.
What is happening here and why and which one should be actually used now?
package.json:
"#nestjs/axios": "^0.0.7",
"#nestjs/common": "^8.0.0",
"#nestjs/core": "^8.0.0",
"#nestjs/jwt": "^8.0.0",
"#nestjs/passport": "^8.2.1",
"#nestjs/platform-express": "^8.0.0",
"#nestjs/schedule": "^1.0.2",
"#nestjs/swagger": "^5.1.5",
"#nestjs/typeorm": "^8.0.2",
You need to import the HttpModule inside of FooModule like so,
import { HttpModule } from '#nestjs/axios'
#Module({
imports: [HttpModule], // HttpModule.register() works too
providers: [FooService],
})
export class FooModule {}
And now you can make use of HttpService inside of FooService,
import { HttpService } from '#nestjs/axios';
#Injectable()
export class FooService {
constructor(private readonly http: HttpService) {}
}
The HttpModule and HttpService from #nestjs/common are deprecated, and will be removed in the next major version of #nestjs/common.

Adding nguniversal/express-engine to Angular proj: "Cannot find BrowserModule import in /src/app/app.module.ts"

First question
Goal
I'm trying to add SSR to my Angular project with ng add #nguniversal/express-engine --clientProject [name] (so I can dynamically prerender meta tags)
Expected Result
I expected the command to execute successfully with all the scaffolding and necessary updates to my existing files, as demonstrated in this YouTube tutorial.
Actual Result
Instead, the console says this:
Installing packages for tooling via npm.
Installed packages for tooling via npm.
Cannot find BrowserModule import in /src/app/app.module.ts
But BrowserModule is imported in app.module.ts.
What I've Tried
Reinstalling package
I've tried uninstalling the package with npm uninstall #nguniversal/express-engineand re-running the ng add above, same issue.
Other posted questions about ng adding #nguniversal/express-server don't seem to apply here, as those guys actually got as far as creating some of the scaffolding and generating the new files - no files are created for me at all, but the module does get added to my node-modules folder.
Could it be an issue with simply reading the typescript in app.module.ts? The BrowserModule import is there, and in the imports array. This is the output for npm ls typescript:
+-- #angular-devkit/build-angular#0.901.8
| `-- #angular-devkit/build-optimizer#0.901.8
| `-- typescript#3.6.5
+-- #ng-toolkit/universal#1.1.21
| +-- #ng-toolkit/_utils#1.1.51
| | `-- #schematics/angular#7.3.10
| | `-- typescript#3.2.4
| `-- #schematics/angular#0.6.8
| `-- typescript#2.7.2
`-- typescript#3.8.3
Additional Info (for David)
app.module.ts
import { BrowserModule, Meta, Title } from '#angular/platform-browser';
import { NgModule } from '#angular/core';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { HeaderComponent } from './header/header.component';
import { FooterComponent } from './footer/footer.component';
import { HomeComponent } from './home/home.component';
import { FontAwesomeModule } from '#fortawesome/angular-fontawesome';
import { SoftwareComponent } from './software/software.component';
import { MediaComponent } from './media/media.component';
import { ShopComponent } from './shop/shop.component';
import { FilmDetailsComponent } from './film-details/film-details.component';
import { ShareModalComponent } from './share-modal/share-modal.component';
import { ShareModule } from 'ngx-sharebuttons';
import { ShareButtonModule } from 'ngx-sharebuttons/button';
import { ShareIconsModule } from 'ngx-sharebuttons/icons';
#NgModule({
imports: [
ShareButtonModule,
ShareIconsModule // Optional if you want the default share icons
]
})
#NgModule({
declarations: [
AppComponent,
HeaderComponent,
FooterComponent,
HomeComponent,
SoftwareComponent,
MediaComponent,
ShopComponent,
FilmDetailsComponent,
ShareModalComponent
],
imports: [
BrowserModule,
AppRoutingModule,
FontAwesomeModule,
ShareModule,
ShareButtonModule,
ShareIconsModule
],
providers: [Meta, Title],
bootstrap: [AppComponent]
})
export class AppModule { }
main.ts
import { enableProdMode } from '#angular/core';
import { platformBrowserDynamic } from '#angular/platform-browser-dynamic';
import { AppModule } from './app/app.module';
import { environment } from './environments/environment';
if (environment.production) {
enableProdMode();
}
platformBrowserDynamic().bootstrapModule(AppModule)
.catch(err => console.error(err));
This error is caused by multiple NgModules in the app.module, as the first NgModule imports doesn't contain BrowserModule.
app would still work fine if you remove first NgModule since the modules in the imports are already imported in the second one

The nativescript-localize do not translate or localize

I've create a new app using the template below command:
tns create my-blank-ng --template tns-template-blank-ng
I've installed the plugin doing
go to app folder:
cd my-blank-ng
open the vs code editor:
code .
inside the vs code open a terminal and execute the command below to instal the nativescript-localize plugin (https://market.nativescript.org/plugins/nativescript-localize#how-to-set-the-default-language)
tns plugin add nativescript-localize
following the instructions from here https://market.nativescript.org/plugins/nativescript-localize#how-to-set-the-default-language I've created a new folder i18n inside the folder app (src/app/i18n) and corresponding language files en.json and fr.default.js as described on the guide having the content:
en.json
{
"app.name": "My app EN",
"hello.world": "Hello world ! Hello world TRANSLATED!"
}
fr.default.json
{
"app.name": "My app FR",
"hello": {
"world": "Bonjour le monde ! Hello world !"
}
}
also, the home.module.ts looks like:
import { NgModule, NO_ERRORS_SCHEMA } from "#angular/core";
import { NativeScriptLocalizeModule } from "nativescript-localize/angular";
import { NativeScriptCommonModule } from "nativescript-angular/common";
import { HomeRoutingModule } from "./home-routing.module";
import { HomeComponent } from "./home.component";
#NgModule({
imports: [
NativeScriptCommonModule,
HomeRoutingModule,
NativeScriptLocalizeModule
],
declarations: [
HomeComponent
],
schemas: [
NO_ERRORS_SCHEMA
]
})
export class HomeModule { }
home.component.ts
import { Component, OnInit } from "#angular/core";
import { localize } from "nativescript-localize";
#Component({
selector: "Home",
templateUrl: "./home.component.html"
})
export class HomeComponent implements OnInit {
constructor() {
// Use the component constructor to inject providers.
console.log(localize("Hello world !"));
}
ngOnInit(): void {
// Init your component properties here.
}
}
home.component.html
<ActionBar title="{{ 'app.name' | L }}"></ActionBar>
<StackLayout>
<!-- Add your page content here -->
<Label text="{{ 'Hello world !' | L }}" ></Label>
<Label text="{{ 'I am %s' | L:'user name' }}" ></Label>
</StackLayout>
additionally please get the package.json
{
"nativescript": {
"id": "org.nativescript.myblankng",
"tns-android": {
"version": "6.3.1"
},
"tns-ios": {
"version": "6.3.0"
}
},
"description": "NativeScript Application",
"license": "SEE LICENSE IN <your-license-filename>",
"repository": "<fill-your-repository-here>",
"dependencies": {
"#angular/animations": "~8.2.0",
"#angular/common": "~8.2.0",
"#angular/compiler": "~8.2.0",
"#angular/core": "~8.2.0",
"#angular/forms": "~8.2.0",
"#angular/platform-browser": "~8.2.0",
"#angular/platform-browser-dynamic": "~8.2.0",
"#angular/router": "~8.2.0",
"#nativescript/theme": "~2.2.1",
"nativescript-angular": "~8.20.3",
"nativescript-localize": "^4.2.1",
"reflect-metadata": "~0.1.12",
"rxjs": "^6.4.0",
"tns-core-modules": "~6.3.0",
"zone.js": "~0.9.1"
},
"devDependencies": {
"#angular/compiler-cli": "~8.2.0",
"#ngtools/webpack": "~8.2.0",
"nativescript-dev-webpack": "~1.4.0",
"typescript": "~3.5.3"
},
"gitHead": "fa98f785df3fba482e5e2a0c76f4be1fa6dc7a14",
"readme": "NativeScript Application"
}
BUT THE RESULT IS disappointing : NO translation :(
What I'm doing wrong?
It seems the en.json is not funded (the locale of the simulator is en)
please see the image attached as well
add in app.module.js
import { NativeScriptLocalizeModule } from "nativescript-localize/angular";
....
imports: [
NativeScriptLocalizeModule
],
in actionbar
<ActionBar class="action-bar">
<Label>
<Span class="action-bar-title" text="{{ 'app.name' | L }}" >
</Span>
</Label>
</ActionBar>
in stack layout
<StackLayout>
<Label text="{{ 'hello.world' | L }}" ></Label>
</StackLayout>
try to put the i18n Folder one level up.
For example:
src>i18n
it worked for me.

unexpected token import when use localize-router in Angular 4 Universal app

I try to build Angular 4 app with server rendering side and language route path. All this base on Angular CLI in 1.5.0-rc1 version.
Everything work OK but I can't solve a problem with language in route.
I have two idea - one to make it like a parameter :lang in URL, but everywhere people advice me to use localize-router plugin. It look very good, but my npm run server can't start properly. In console I get an error:
/home/xxx/Projects/private/angular4-cli-seed/node_modules/localize-router/src/localize-router.config.js:1
(function (exports, require, module, __filename, __dirname) { import { Inject, OpaqueToken } from '#angular/core';
Here is my app-routing.module.ts:
import {NgModule, PLATFORM_ID, Inject, OpaqueToken} from '#angular/core';
import 'zone.js';
import 'reflect-metadata';
import { Routes, RouterModule } from '#angular/router';
import {AboutComponent} from './about/about.component';
import {HomeComponent} from './home/home.component';
import {LocalizeParser, LocalizeRouterModule, LocalizeRouterSettings, ManualParserLoader} from 'localize-router';
import {HttpClientModule, HttpClient} from '#angular/common/http';
import {TranslateService} from '#ngx-translate/core';
import { Observable } from 'rxjs/Observable';
import { Location } from '#angular/common';
const routes: Routes = [
{
path: '',
component: HomeComponent
},
{
path: 'about',
component: AboutComponent
}
];
export function localizeFactory(translate: TranslateService, location: Location, settings: LocalizeRouterSettings): LocalizeParser {
const browserLocalizeLoader = new ManualParserLoader(translate, location, settings, ['en', 'pl'], 'pl');
return browserLocalizeLoader;
}
#NgModule({
imports: [
RouterModule.forRoot(routes),
LocalizeRouterModule.forRoot(routes, {
parser: {
provide: LocalizeParser,
useFactory: (localizeFactory),
deps: [TranslateService, Location, LocalizeRouterSettings, HttpClient]
}
}),
],
exports: [RouterModule]
})
export class AppRoutingModule {
private static TranslateService: any;
}
Do you have any tips how can I solve it? I found some tips for Webpack (to use exclude list), but I want to use CLI because I don't know Webpack too well.
This problem is connected with library type - it's not a commonjs type, but ES6. More about this problem you can read here on GitHub.
To solve it you can contact the author of library what you want to use in Angular 4 Universal (with Angular CLI). They should recompile it in a proper way.
Another solution (more quick to realize) give me a #sjogren on GitHub. You can use babel.js to recompile library during a building process. To do this you should run:
npm install babel-cli --save
npm install babel-preset-env --save
npm install babel-preset-es2015 --save
and add this code in package.json:
"babel": {
"presets": [
"es2015"
]
},
Finally in package.json you should add to your scripts prestart script with code to recompile the library. In my example:
"scripts": {
"prestart": "node node_modules/babel-cli/bin/babel.js node_modules/localize-router --out-dir node_modules/localize-router --presets es2015"
"start": "......"
}
This worked fine for me, and I don't have an Unexpected Token Import error.

Cannot find module in Typescript

hy guys i have problem like this,
i have a app.typescript
and inside it i import
import { Component } from '#angular/core';
import {CORE_DIRECTIVES} from '#angular/common';
import { MODAL_DIRECTVES } from 'ng2-bootstrap';
#Component({
moduleId: module.id,
selector: 'my-app',
templateUrl: 'my-app.component.html',
styleUrls: ['my-app.component.css'],
directives: [MODAL_DIRECTVES]
})
export class my-appComponent {
title = 'Mp App';
}
but when i run it i got an error that say cant find module ng2-bootstrap
As per the documentation here. It appears you must import from a nested path:
import { MODAL_DIRECTIVES } from 'ng2-bootstrap/ng2-bootstrap';
or
import { MODAL_DIRECTIVES } from 'ng2-bootstrap/components/modal';

Resources