Angular 1 and 2 hybrid app - "require is not defined" - angularjs

I'm trying to upgrade a giant client-side app from Angular 1.x to Angular 2, and I've hit a really annoying roadblock. I've recreated the issue with a dummy, light-weight project (files pasted below), but let me just explain the problem first.
Basically, when my tsconfig.json specifies module as commonjs, I get the following error in my chrome dev console:
app.module.ts:1Uncaught ReferenceError: require is not defined
When I switch the module to system, I get the following error:
Uncaught TypeError: Invalid System.register call. Anonymous System.register calls can only be made by modules loaded by SystemJS.import and not via script tags.
And when I switch module to umd, everything works fine. But given that angular themselves suggest using commonjs, I'm concerned that umd is not the right answer. However, if I'm wrong about that and umd is perfectly fine, I'd love to hear a good explanation as to why.
Here's my code to reproduce my issue:
tsconfig.json:
{
"compilerOptions": {
"target": "es5",
"module": "system",
"moduleResolution": "node",
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"removeComments": false,
"noImplicitAny": false
}
,
"exclude": [
"node_modules"
]
}
typings.json:
{
"globalDependencies": {
"angular": "registry:dt/angular#1.5.0+20160922195358",
"core-js": "registry:dt/core-js#0.0.0+20160725163759",
"jasmine": "registry:dt/jasmine#2.2.0+20160621224255",
"jquery": "registry:dt/jquery#1.10.0+20160929162922",
"node": "registry:dt/node#6.0.0+20160909174046"
}
}
systemjs.config.js :
(function (global) {
System.config({
paths: {
// paths serve as alias
'npm:': 'node_modules/'
},
// map tells the System loader where to look for things
map: {
// our app is within the app folder
app: 'app',
// angular bundles
'#angular/core': 'npm:#angular/core/bundles/core.umd.js',
'#angular/common': 'npm:#angular/common/bundles/common.umd.js',
'#angular/compiler': 'npm:#angular/compiler/bundles/compiler.umd.js',
'#angular/platform-browser': 'npm:#angular/platform-browser/bundles/platform-browser.umd.js',
'#angular/platform-browser-dynamic': 'npm:#angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js',
'#angular/ ': 'npm:#angular/http/bundles/http.umd.js',
'#angular/router': 'npm:#angular/router/bundles/router.umd.js',
'#angular/forms': 'npm:#angular/forms/bundles/forms.umd.js',
// other libraries
'rxjs': 'npm:rxjs',
'angular-in-memory-web-api': 'npm:angular-in-memory-web-api',
},
// packages tells the System loader how to load when no filename and/or no extension
packages: {
app: {
main: './main.js',
defaultExtension: 'js'
},
rxjs: {
defaultExtension: 'js'
},
'angular-in-memory-web-api': {
main: './index.js',
defaultExtension: 'js'
}
}
});
})(this);
package.json :
{
"name": "mattsApp",
"version": "0.0.1",
"dependencies": {
"angular": "^1.5.8",
"#angular/common": "~2.0.2",
"#angular/compiler": "~2.0.2",
"#angular/core": "~2.0.2",
"#angular/forms": "~2.0.2",
"#angular/http": "~2.0.2",
"#angular/platform-browser": "~2.0.2",
"#angular/platform-browser-dynamic": "~2.0.2",
"#angular/router": "~3.0.2",
"#angular/upgrade": "~2.0.2",
"angular-in-memory-web-api": "~0.1.5",
"bootstrap": "^3.3.7",
"core-js": "^2.4.1",
"reflect-metadata": "^0.1.8",
"rxjs": "5.0.0-beta.12",
"systemjs": "0.19.39",
"zone.js": "^0.6.25"
},
"devDependencies": {
"concurrently": "^3.0.0",
"lite-server": "^2.2.2",
"typescript": "^2.0.3",
"typings":"^1.4.0"
},
"scripts": {
"start": "tsc && concurrently \"tsc -w\" \"lite-server\" ",
"lite": "lite-server",
"postinstall": "typings install",
"tsc": "tsc",
"tsc:w": "tsc -w",
"typings": "typings"
}
}
app.js :
angular.module('app', []);
app.module.ts :
import { NgModule } from '#angular/core';
import { BrowserModule } from '#angular/platform-browser';
import { upgradeAdapter } from './upgrade_adapter';
#NgModule({
imports: [ BrowserModule ]
})
export class AppModule { }
upgradeAdapter.bootstrap(document.body, ['app'], {strictDi: true});
appCtrl.ts :
angular.module('app')
.controller('appCtrl', ['$scope', function($scope) {
$scope.hello = "howdy worldy";
}]);
upgrade_adapter.ts :
import { UpgradeAdapter } from '#angular/upgrade';
import {AppModule} from "./app.module";
export const upgradeAdapter = new UpgradeAdapter(AppModule);
What am I missing?

you need instantiate the UpgradeAdapter with a non null parametter. In this way you need pass a forwardRef instance, something like this:
const upgradeAdapter = new UpgradeAdapter(forwardRef(() => AppModule));
finally you need import the forwardRef
import {NgModule, forwardRef} from '#angular/core';

First, thanks Andres. I need to read up a bit about the forwardRef to understand what that does. But it turns out that the answer in my particular case was something different.
I didn't post my index.html here (simple oversight), but the problem was that I wasn't loading my modules with the System.import('app'). Embarrassing error, I have to admit. So the answer is to add that line.
I should note that this led to a different error which I solved, but I'll point it out here in case others have a similar issue. Since this is a hybrid angular 1 and 2 app, I have typescript files that are sometimes used by angular 1 controllers / directives / etc, and also by angular 2 components. I had changed those typescript files to use export so I could import them into my angular 2 components. This led me to also change my /// in the angular 1 files to use import. Unfortunately, this gave me an "undefinedModule" error. The solution (in my case) is not to use export on typescript files unless they are ONLY used with the angular 2 components. Meaning, in some angular 2 components, I'm actually using the /// and not the import.
Just thought other people might find that useful.

Related

Failed to load resource: the server responded with a status of 404 (Not Found) http://localhost/app/main.js

I have setup for Angular JS 2.x project in Visual Studio 2015 environment. I have followed the steps in https://angular.io/docs/ts/latest/cookbook/visual-studio-2015.html
It works fine if I set Start up file as Index.html.
But, As I am using asp.net MVC project I wanted to use \Views\Shared_Layout.cshtml as master page and \Views\Home\Index.cshtml as original Angular JS page.
So _Layout.cshtml will have #RenderBody(); to render Index.cshtml file which will have angular 2.x selector element <my-app>Loading...</my-app>.
While running the application I am getting following error which is related to referring the app/main.js file.
Failed to load resource: the server responded with a status of 404 (Not Found) http://localhost/app/main.js
I have not changed any path reference. Everything is as per instructions given above link except adding the following element in _Layout.cshtml. which helps me to find the referenced files from the root directory.
<base href="./">
Here is the list of files I am using for my project.
_Layout.cshtml
<!DOCTYPE html>
<html lang="en">
<head>
<!-- base href is must to specify location of app folder -->
<base href="./">
<title>Angular JS 2 </title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Polyfill(s) for older browsers -->
<script src="~/node_modules/core-js/client/shim.min.js"></script>
<script src="~/node_modules/zone.js/dist/zone.js"></script>
<script src="~/node_modules/systemjs/dist/system.src.js"></script>
<script src="~/systemjs.config.js"></script>
<script>
System.import('app').catch(function (err) {
console.error(err);
});
</script>
</head>
<body>
#RenderBody();
</body>
</html>
index.cshtml
#{
Layout = "~/Views/Shared/_Layout.cshtml";
}
<my-app>Loading...</my-app>
package.json
{
"name": "angular-quickstart",
"version": "1.0.0",
"description": "QuickStart package.json from the documentation, supplemented with testing support",
"scripts": {
"start": "tsc && concurrently \"tsc -w\" \"lite-server\" ",
"e2e": "tsc && concurrently \"http-server -s\" \"protractor protractor.config.js\" --kill-others --success first",
"lint": "tslint ./app/**/*.ts -t verbose",
"lite": "lite-server",
"pree2e": "webdriver-manager update",
"test": "tsc && concurrently \"tsc -w\" \"karma start karma.conf.js\"",
"test-once": "tsc && karma start karma.conf.js --single-run",
"tsc": "tsc",
"tsc:w": "tsc -w"
},
"keywords": [],
"author": "",
"license": "MIT",
"dependencies": {
"#angular/common": "~2.4.0",
"#angular/compiler": "~2.4.0",
"#angular/core": "~2.4.0",
"#angular/forms": "~2.4.0",
"#angular/http": "~2.4.0",
"#angular/platform-browser": "~2.4.0",
"#angular/platform-browser-dynamic": "~2.4.0",
"#angular/router": "~3.4.0",
"angular-in-memory-web-api": "~0.2.4",
"systemjs": "0.19.40",
"core-js": "^2.4.1",
"rxjs": "5.0.1",
"zone.js": "^0.7.4"
},
"devDependencies": {
"concurrently": "^3.1.0",
"lite-server": "^2.2.2",
"typescript": "~2.0.10",
"canonical-path": "0.0.2",
"http-server": "^0.9.0",
"tslint": "^3.15.1",
"lodash": "^4.16.4",
"jasmine-core": "~2.4.1",
"karma": "^1.3.0",
"karma-chrome-launcher": "^2.0.0",
"karma-cli": "^1.0.1",
"karma-jasmine": "^1.0.2",
"karma-jasmine-html-reporter": "^0.2.2",
"protractor": "~4.0.14",
"rimraf": "^2.5.4",
"#types/node": "^6.0.46",
"#types/jasmine": "^2.5.36"
},
"repository": {}
}
systemjs.config.js
/**
* System configuration for Angular samples
* Adjust as necessary for your application needs.
*/
(function (global) {
System.config({
paths: {
// paths serve as alias
'npm:': 'node_modules/'
},
// map tells the System loader where to look for things
map: {
// our app is within the app folder
app: 'app',
// angular bundles
'#angular/core': 'npm:#angular/core/bundles/core.umd.js',
'#angular/common': 'npm:#angular/common/bundles/common.umd.js',
'#angular/compiler': 'npm:#angular/compiler/bundles/compiler.umd.js',
'#angular/platform-browser': 'npm:#angular/platform-browser/bundles/platform-browser.umd.js',
'#angular/platform-browser-dynamic': 'npm:#angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js',
'#angular/http': 'npm:#angular/http/bundles/http.umd.js',
'#angular/router': 'npm:#angular/router/bundles/router.umd.js',
'#angular/forms': 'npm:#angular/forms/bundles/forms.umd.js',
// other libraries
'rxjs': 'npm:rxjs',
'angular-in-memory-web-api': 'npm:angular-in-memory-web-api/bundles/in-memory-web-api.umd.js'
},
// packages tells the System loader how to load when no filename and/or no extension
packages: {
app: {
main: 'main.js',
defaultExtension: 'js'
},
rxjs: {
defaultExtension: 'js'
}
}
});
})(this);
tsconfig.json
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"moduleResolution": "node",
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"lib": [ "es2015", "dom" ],
"noImplicitAny": true,
"suppressImplicitAnyIndexErrors": true
}
}
main.ts
import { platformBrowserDynamic } from '#angular/platform-browser-dynamic';
import { AppModule } from './app.module';
platformBrowserDynamic().bootstrapModule(AppModule);
app.module.ts
import { NgModule } from '#angular/core';
import { BrowserModule } from '#angular/platform-browser';
import { AppComponent } from './app.component';
#NgModule({
imports: [ BrowserModule ],
declarations: [ AppComponent ],
bootstrap: [ AppComponent ]
})
export class AppModule { }
app.component.ts
import { Component } from '#angular/core';
#Component({
moduleId:module.id,
selector: 'my-app',
template: `<h1>{{name}}</h1>`,
})
export class AppComponent {
name = "Ultimate";
}

Angular 2 Tour of Heroes, Step 7 error

I looked through the forums, but couldn't find anything relevant.
I just started the tutorial 3 days ago, got to Step 7, and when I add the InMemoryWebApiModule to the mix (as detailed in the steps), I get this error:
All of my config files are directly from what was provided in the tutorial, but I'll provide them here anyway.
Any ideas as to what's going on?
Here is my package.json:
{
"name": "angular-quickstart",
"version": "1.0.0",
"scripts": {
"start": "tsc && concurrently \"tsc -w\" \"lite-server\" ",
"lite": "lite-server",
"tsc": "tsc",
"tsc:w": "tsc -w"
},
"licenses": [
{
"type": "MIT",
"url": "https://github.com/angular/angular.io/blob/master/LICENSE"
}
],
"dependencies": {
"#angular/common": "~2.1.1",
"#angular/compiler": "~2.1.1",
"#angular/core": "~2.1.1",
"#angular/forms": "~2.1.1",
"#angular/http": "~2.1.1",
"#angular/platform-browser": "~2.1.1",
"#angular/platform-browser-dynamic": "~2.1.1",
"#angular/router": "~3.1.1",
"#angular/upgrade": "~2.1.1",
"angular-in-memory-web-api": "~0.1.13",
"bootstrap": "^3.3.7",
"core-js": "^2.4.1",
"reflect-metadata": "^0.1.8",
"rxjs": "5.0.0-beta.12",
"systemjs": "0.19.39",
"zone.js": "^0.6.25"
},
"devDependencies": {
"#types/core-js": "^0.9.34",
"#types/node": "^6.0.45",
"concurrently": "^3.0.0",
"lite-server": "^2.2.2",
"typescript": "^2.0.3"
}
}
Here is app.module.ts:
import { NgModule } from '#angular/core';
import { BrowserModule } from '#angular/platform-browser';
import { FormsModule } from '#angular/forms';
import { HttpModule } from '#angular/http';
// Imports for loading & configuring the in-memory web api
import { InMemoryWebApiModule } from 'angular-in-memory-web-api';
import { InMemoryDataService } from './in-memory-data.service';
import { AppRoutingModule } from './app-routing.module'
import { AppComponent } from './app.component';
import { HeroDetailComponent } from './hero-detail.component';
import { HeroesComponent } from './heroes.component';
import { HeroService } from './hero.service';
import { DashboardComponent } from './dashboard.component';
#NgModule({
imports: [
AppRoutingModule,
BrowserModule,
FormsModule,
HttpModule,
InMemoryWebApiModule.forRoot(InMemoryDataService)
],
declarations: [
AppComponent,
HeroDetailComponent,
HeroesComponent,
DashboardComponent
],
providers: [ HeroService ],
bootstrap: [ AppComponent ]
})
export class AppModule { }
Here is systemjs.config.js:
/**
* System configuration for Angular samples
* Adjust as necessary for your application needs.
*/
(function (global) {
System.config({
paths: {
// paths serve as alias
'npm:': 'node_modules/'
},
// map tells the System loader where to look for things
map: {
// our app is within the app folder
app: 'app',
// angular bundles
'#angular/core': 'npm:#angular/core/bundles/core.umd.js',
'#angular/common': 'npm:#angular/common/bundles/common.umd.js',
'#angular/compiler': 'npm:#angular/compiler/bundles/compiler.umd.js',
'#angular/platform-browser': 'npm:#angular/platform-browser/bundles/platform-browser.umd.js',
'#angular/platform-browser-dynamic': 'npm:#angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js',
'#angular/http': 'npm:#angular/http/bundles/http.umd.js',
'#angular/router': 'npm:#angular/router/bundles/router.umd.js',
'#angular/forms': 'npm:#angular/forms/bundles/forms.umd.js',
// other libraries
'rxjs': 'npm:rxjs',
'angular-in-memory-web-api': 'npm:angular-in-memory-web-api',
},
// packages tells the System loader how to load when no filename and/or no extension
packages: {
app: {
main: './main.js',
defaultExtension: 'js'
},
rxjs: {
defaultExtension: 'js'
},
'angular-in-memory-web-api': {
main: './index.js',
defaultExtension: 'js'
}
}
});
})(this);
Take a look at the CHANGELOG. You need to use the umd bundle, and remove it from the packages
In systemjs.config.js you should change the mapping to:
'angular-in-memory-web-api': 'npm:angular-in-memory-web-api/bundles/in-memory-web-api.umd.js'
then delete from packages:
'angular-in-memory-web-api': {
main: './index.js',
defaultExtension: 'js'
}

Angular2 Tutorial (Tour of Heroes): Cannot find module './app-routing.module'

I am going through the NG2 tutorial on here, but I've hit a stumbling block.
When I try and import my AppRoutingModule in my app.module.ts file I get a 'Cannot find module './app-routing.module' error. I have seen this post on here, amongst other solutions, but they all seem to relate to systemjs and not webpack.
Here is my package.json:
{
"name": "heroes",
"version": "0.0.0",
"license": "MIT",
"angular-cli": {},
"scripts": {
"start": "ng serve",
"lint": "tslint \"src/**/*.ts\"",
"test": "ng test",
"pree2e": "webdriver-manager update",
"e2e": "protractor"
},
"private": true,
"dependencies": {
"#angular/common": "2.0.0",
"#angular/compiler": "2.0.0",
"#angular/core": "2.0.0",
"#angular/forms": "2.0.0",
"#angular/http": "2.0.0",
"#angular/platform-browser": "2.0.0",
"#angular/platform-browser-dynamic": "2.0.0",
"#angular/router": "3.0.0",
"angular2-in-memory-web-api": "0.0.21",
"core-js": "^2.4.1",
"rxjs": "5.0.0-beta.12",
"ts-helpers": "^1.1.1",
"zone.js": "^0.6.23"
},
"devDependencies": {
"#types/jasmine": "^2.2.30",
"angular-cli": "1.0.0-beta.15",
"codelyzer": "~0.0.26",
"jasmine-core": "2.4.1",
"jasmine-spec-reporter": "2.5.0",
"karma": "1.2.0",
"karma-chrome-launcher": "^2.0.0",
"karma-cli": "^1.0.1",
"karma-jasmine": "^1.0.2",
"karma-remap-istanbul": "^0.2.1",
"protractor": "4.0.5",
"ts-node": "1.2.1",
"tslint": "3.13.0",
"typescript": "2.0.2"
}
}
Here is my app.module.ts
import { BrowserModule } from '#angular/platform-browser';
import { NgModule } from '#angular/core';
import { FormsModule } from '#angular/forms';
import { HttpModule } from '#angular/http';
import { RouterModule } from '#angular/router';
// Imports for loading & configuring the in-memory web api
import { InMemoryWebApiModule } from 'angular2-in-memory-web-api';
import { AppRoutingModule } from './app-routing.module'; //ERROR
...
#NgModule({
declarations: [
AppComponent,
HeroDetailComponent,
HeroesComponent,
DashboardComponent,
],
imports: [
BrowserModule,
FormsModule,
HttpModule,
AppRoutingModule,
InMemoryWebApiModule.forRoot(InMemoryDataService), //ERROR: Cannot find name 'InMemoryDataServiceInMemoryDataService
RouterModule.forRoot([
{
path: '', redirectTo: '/dashboard', pathMatch: 'full'
},
{
path: 'dashboard', component: DashboardComponent
},
{
path: 'detail/:id', component: HeroDetailComponent
},
{
path: 'heroes', component: HeroesComponent
}
])
],
providers: [HeroService],
bootstrap: [AppComponent]
})
export class AppModule { }
Here is my specs (I am using angular CLI):
angular-cli: 1.0.0-beta.15
node: 4.2.6
os: linux x64 (ubuntu)
Not sure what else I can try? I guess I need to import the module somehow?
I did try and run:
npm install angular-route
But this led to:
├──UNMET PEER DEPENDENCY
#angular/common#2.0.0
├──UNMET PEER DEPENDENCY
#angular/core#2.0.0
├──UNMET PEER DEPENDENCY
#angular/platform-browser#2.0.0
└── angular-route#1.5.8
Anyone have any suggestions?
Checkout the live example with all the files
import { NgModule } from '#angular/core';
import { RouterModule, Routes } from '#angular/router';
import { DashboardComponent } from './dashboard.component';
import { HeroesComponent } from './heroes.component';
import { HeroDetailComponent } from './hero-detail.component';
const routes: Routes = [
{ path: '', redirectTo: '/dashboard', pathMatch: 'full' },
{ path: 'dashboard', component: DashboardComponent },
{ path: 'detail/:id', component: HeroDetailComponent },
{ path: 'heroes', component: HeroesComponent }
];
#NgModule({
imports: [ RouterModule.forRoot(routes) ],
exports: [ RouterModule ]
})
export class AppRoutingModule {}
/*
Copyright 2016 Google Inc. All Rights Reserved.
Use of this source code is governed by an MIT-style license that
can be found in the LICENSE file at http://angular.io/license
*/
According to comments, it seems that you might be missing the app-routing file altogether. Perhaps you missed the piece describing that, or sometimes the documentation might be outdated. Please check the plunker showing a working, live example with the needed files.
Right before making a new angular project, the CLI prompts you to either include routing or not, like this:
Would you like to add Angular routing? Yes
Make sure you type in Y when it does so, or else your project won't be made with routing files.

angularjs 2.0: Can't inject anything through component constructor()

I am creating a sample application in angularjs 2.0. While developement I came across with a serious problem - I can't inject anything to the component through the constructor function.
This is the plnkr url : https://plnkr.co/edit/g1UcGmYPLtY8GiXuNHzK?p=preview
I used the following code for importing ItemService from app.item.service.ts
import { ItemService } from './app.item.service';
Then I specified provider as
#Component({
selector: 'list-todo',
template: `
<ul>
<li *ngFor="let item of items">{{item.text}}</li>
</ul>
`,
providers : [ItemService]
})
After that, given code for TodoComponent as
export class TodoComponent implements OnInit {
items:Array<Object> = [];
constructor(private itemService: ItemService){ //here is the problem
}
ngOnInit(){
this.getItems();
}
getItems(){
this.items = this.itemService.getItems();
}
}
When I try to inject ItemService through constructor , I am getting an error : " Error: Can't resolve all parameters for TodoComponent: (?)."
I am not able to inject even angularjs providers like Injector.
However, this method works for me :
const injector = ReflectiveInjector.resolveAndCreate([ItemService]);
this.itemService = injector.get(ItemService);
I am using the following versions of libraries for developement as mentioned in my package.json
"dependencies": {
"#angular/common": "2.0.0",
"#angular/compiler": "2.0.0",
"#angular/core": "2.0.0",
"#angular/forms": "2.0.0",
"#angular/http": "2.0.0",
"#angular/platform-browser": "2.0.0",
"#angular/platform-browser-dynamic": "2.0.0",
"#angular/router": "3.0.0",
"#angular/upgrade": "2.0.0",
"core-js": "^2.4.1",
"reflect-metadata": "^0.1.3",
"rxjs": "5.0.0-beta.12",
"systemjs": "0.19.27",
"zone.js": "^0.6.23",
"angular2-in-memory-web-api": "0.0.20",
"bootstrap": "^3.3.6"
},
"devDependencies": {
"concurrently": "^2.2.0",
"lite-server": "^2.2.2",
"typescript": "^2.0.2",
"typings":"^1.3.2"
}
Angular version: 2.0.0 final
Browser: all
attaching the error log I got from console :
(index):16 Error: Error: Can't resolve all parameters for TodoComponent: (?).
at CompileMetadataResolver.getDependenciesMetadata (http://localhost:3000/node_modules/#angular/compiler/bundles/compiler.umd.js:14404:21)
at CompileMetadataResolver.getTypeMetadata (http://localhost:3000/node_modules/#angular/compiler/bundles/compiler.umd.js:14301:28)
at CompileMetadataResolver.getDirectiveMetadata (http://localhost:3000/node_modules/#angular/compiler/bundles/compiler.umd.js:14074:30)
at eval (http://localhost:3000/node_modules/#angular/compiler/bundles/compiler.umd.js:14167:51)
at Array.forEach (native)
at CompileMetadataResolver.getNgModuleMetadata (http://localhost:3000/node_modules/#angular/compiler/bundles/compiler.umd.js:14161:51)
at RuntimeCompiler._compileComponents (http://localhost:3000/node_modules/#angular/compiler/bundles/compiler.umd.js:16803:49)
at RuntimeCompiler._compileModuleAndComponents (http://localhost:3000/node_modules/#angular/compiler/bundles/compiler.umd.js:16741:39)
at RuntimeCompiler.compileModuleAsync (http://localhost:3000/node_modules/#angular/compiler/bundles/compiler.umd.js:16732:23)
at PlatformRef_._bootstrapModuleWithZone (http://localhost:3000/node_modules/#angular/core/bundles/core.umd.js:6954:29)
Evaluating http://localhost:3000/dist/main.js
Error loading http://localhost:3000/dist/main.js(anonymous function) # (index):16ZoneDelegate.invoke # zone.js:203Zone.run # zone.js:96(anonymous function) # zone.js:462ZoneDelegate.invokeTask # zone.js:236Zone.runTask # zone.js:136drainMicroTaskQueue # zone.js:368ZoneTask.invoke # zone.js:308
I see that your tsconfig.json isn't correct.
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"moduleResolution": "node",
"sourceMap": false,
"emitDecoratorMetadata": true, <== it should be true
"experimentalDecorators": true,
"removeComments": false,
"noImplicitAny": false,
"outDir": "dist"
}
}
This is the magic sauce for DI. emitDecoratorMetadata: true. This option will preserve type information in your object's metadata.
See also
How does the TypeScript Angular DI magic work?
Injecting services in services in Angular 2
You code is fine!
Here is your updated plunker: https://plnkr.co/edit/6SR8Ibljuy0ElEBU87ox?p=preview
Did some changes to your system.js.config!
// ADDED THESE TWO OPTIONS BELOW
//use typescript for compilation
transpiler: 'typescript',
//typescript compiler options
typescriptOptions: {
emitDecoratorMetadata: true
},
and this..
app: {
main: './main.ts', // CHANGES HERE
defaultExtension: 'ts' // CHANGES HERE
},

when I use gulp I get a weird js output?

I'm using gulp with reactjs and browserify
Here's my gulp file
var gulp = require('gulp'),
browserify = require('browserify'),
source = require('vinyl-source-stream'),
reactify = require('reactify'),
rename = require('gulp-rename')
gulp.task('js-build', function() {
// converts jsx to js
var b = browserify({
entries: ['./lib/main.jsx'],
transform: [reactify],
extensions: ['.js','.jsx'],
debug: false,
cache: {},
packageCache: {},
fullPaths: false
});
function build(file) {
return b
.plugin('minifyify', {
map: false
})
.bundle()
.pipe(source('main.js'))
.pipe(gulp.dest('./js/'));
}
});
gulp.task('default', ['js-build']);
When I run gulp to convert my jsx to one js file, my js file output is really long and complicated and has functions that are beyond what I specified in my jsx files? Like there is a function called cleanUpNextTick() for example that I never declared.
Is this normal?
Here's my package.json
"dependencies": {
"browserify": "^11.0.1",
"expect": "^1.9.0",
"gulp": "^3.9.0",
"gulp-concat": "^2.6.0",
"gulp-react": "^3.0.1",
"gulp-rename": "^1.2.2",
"gulp-watch": "^4.3.4",
"jest-cli": "^0.5.0",
"jquery": "^2.1.4",
"marked": "^0.3.5",
"minifyify": "^7.0.5",
"react": "^0.13.3",
"react-tools": "^0.13.3",
"reactify": "^1.1.1",
"vinyl-source-stream": "^1.1.0"
},
"jest": {
"moduleFileExtensions": [
"jsx",
"js"
],
"scriptPreprocessor": "preprocessor.js",
"unmockedModulePathPatterns": [
"node_modules/react"
]
},
"devDependencies": {
"jest-cli": "^0.4.19"
}
Yes it's normal, your code is been transpiled into Javascript ES5 by reactify, which uses ReactTools.transform under the hood.
The new features of the language are being replaced with ES5-compatible code, and new functions can be added to the final code during the transformation.
Also, browserify bundles all the dependencies you referenced to with require (or ES6's import), so you'll see also third-party code in your final bundle.
Yes its definitely normal. The bundle file compiles ALL javascript in your project, including your node module. This means youll be able to see functions from anything you require, cleanUpNextTick if probably from one of the libraries youre requiring, like react's CSSTransitionGroup for example

Resources