1

On the angular2 quick start page: https://angular.io/guide/quickstart

with Angular2 rc6 , file systemjs.config.js has changed . I'm trying to change the folder /app/ accord to angular quick start by /src/app accord my app, so i'm not sure what i must to change in this file. I've tried some paths but it's not working, anyone can explain more about this file and how I use the paths. in this configuration i dont care about production or /dist , because i'm just testing angular. I was working before with webpack starters or angular-cli but new versions angular broke those packages, so i want to stay simple.

src/app/systemjs.config.js:

/**
 * System configuration for Angular 2 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',
      // angular testing umd bundles
      '@angular/core/testing': 'npm:@angular/core/bundles/core-testing.umd.js',
      '@angular/common/testing': 'npm:@angular/common/bundles/common-testing.umd.js',
      '@angular/compiler/testing': 'npm:@angular/compiler/bundles/compiler-testing.umd.js',
      '@angular/platform-browser/testing': 'npm:@angular/platform-browser/bundles/platform-browser-testing.umd.js',
      '@angular/platform-browser-dynamic/testing': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic-testing.umd.js',
      '@angular/http/testing': 'npm:@angular/http/bundles/http-testing.umd.js',
      '@angular/router/testing': 'npm:@angular/router/bundles/router-testing.umd.js',
      '@angular/forms/testing': 'npm:@angular/forms/bundles/forms-testing.umd.js',
      // other libraries
      'rxjs':                       'npm:rxjs',
      'angular2-in-memory-web-api': 'npm:angular2-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'
      },
      'angular2-in-memory-web-api': {
        main: './index.js',
        defaultExtension: 'js'
      }
    }
  });
})(this);

src/app/app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule, ApplicationRef } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
import { HttpModule }     from '@angular/http';

import { AppComponent } from './app.component';
import { Configuration } from './app.constants';
import { routing }        from './app.routing';
import { AccountListComponent } from './accounts/account-list/account-list.component';
import { AccountDetailComponent } from './accounts/account-detail/account-detail.component';
import { DashboardComponent } from './dashboard/';


@NgModule({
  declarations: [
    AppComponent,
    AccountListComponent,
    AccountDetailComponent,
    DashboardComponent
  ],
  imports: [
    BrowserModule,
    CommonModule,
    FormsModule,
    HttpModule,
    routing
  ],
  providers: [Configuration],
  entryComponents: [AppComponent],
  bootstrap: [AppComponent]
})
export class AppModule {

}

src/app.component.ts

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

@Component({
  selector: 'app-root',
  templateUrl: 'app.component.html',
  styleUrls: ['app.component.css']
})
export class AppComponent {
  title = 'app works!';
}

/src/main.ts

import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { enableProdMode } from '@angular/core';
import { AppModule, environment } from './index';

if (environment.production) {
  enableProdMode();
}

platformBrowserDynamic().bootstrapModule(AppModule);

./index.html

<!doctype html>
<html>
<head>
  <meta charset="utf-8">
  <title>Ng2</title>
  <base href="/">

  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" type="image/x-icon" href="favicon.ico">
  <link rel="stylesheet" type="text/css" href="vendor/bootstrap/dist/css/bootstrap.min.css">
  <base href="/">
</head>
<body>
  <app-root>Loading...</app-root>
</body>
</html>
2
  • I recommend use webpack and not systemjs, because support barrel concept stackoverflow.com/questions/37682372/… , i've found the guide of angular with webpack is working very well stackoverflow.com/questions/37682372/… , another starters and angular-cli don't support yet rc6 version Commented Sep 4, 2016 at 7:21
  • That is easily the worst reason to use one over the other that I have ever heard. They are fundamentally different tools with different approaches and they each have strengths and weaknesses. Barrels, on the other hand are a bad coding convention that somehow got infused into the minds of Angular 2 users. Probably because of the framework's lack of a proper service provider abstraction. 8 months later and it still doesn't have one. Commented Apr 17, 2017 at 7:39

2 Answers 2

2

I find the new systemjs.config from angular to be a bit more messy. This is what i use and it works great:

(function(global) {
    var map = {
            'app': 'src/app',
            '@angular': 'node_modules/@angular',
            'angular2-in-memory-web-api': 'node_modules/angular2-in-memory-web-api',
            'rxjs': 'node_modules/rxjs'
        },
        packages = {
            'app': { main: 'main.js',  defaultExtension: 'js' },
            'angular2-in-memory-web-api': {main: './index.js',defaultExtension:'js'}
            'rxjs': { defaultExtension: 'js' }
        },
        ngPackageNames = [
            'common',
            'compiler',
            'core',
            'http',
            'platform-browser',
            'platform-browser-dynamic',
            'router',
            'forms'
        ],

        tests = [
            'core/testing',
            'common/testing',
            'compiler/testing',
            'platform-browser/testing',
            'platform-browser-dynamic/testing',
            'http/testing',
            'router/testing',
            'forms/testing'
        ];

    function packUmd(pkgName) {packages['@angular/'+pkgName] = { main: '/bundles/' + pkgName + '.umd.min.js', defaultExtension: 'js' }}

    ngPackageNames.forEach(packUmd);

    tests.forEach(function (test) {
        return packages['@angular/'+ test] = { main: '/bundles/' + test.replace(/\//, '-') + '.umd.min.js', defaultExtension: 'js' }
    })

    var config = {
        map: map,
        packages: packages
    };

    System.config(config);

})(this);

I think this looks much cleaner and understandable. The only thing you need to change if your app folder changes is: 'app': 'path-to-app/app'

Sign up to request clarification or add additional context in comments.

4 Comments

thanks now it's working, the only problem i have it's angular don't find my .js that are in subfolders , like localhost:3000/src/app/dashboard.js 404 (Not Found) , but there is in localhost:3000/src/app/dashboard/dashboard.component.js, i don't understand how i can configure systemjs.config.js to do that
That can't be right. I have the same configuration and everything works fine. Are you sure that something else isn't causing the error?
thanks for your help, i'm sure i'm missing something; i've updated my question with the source files. if you can realize the error. webpack make one file in one folder all the .ts compiled files (.js), how does work systemjs.config ? load every js, but where?
I'm happy to help. Sorry it still isn't working for you. You can achieve a similar build using systemjs-builder. Here is the link:github.com/systemjs/builder
0

To load modules in NodeJS, install SystemJS with:

npm install systemjs

If transpiling ES modules, install the transpiler plugin following the instructions from the transpiler project page.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.