angular2 can't import ng2-bootstrap

mckit picture mckit · Dec 21, 2015 · Viewed 21k times · Source

I can't import ng2-bootstrap

layout.jade

html
 head
title Angular 2 QuickStart
// 1. Load libraries

script(src="/node_modules/angular2/bundles/angular2-polyfills.js")
script(src="/node_modules/systemjs/dist/system.src.js")
//script(src="/node_modules/ng2-bootstrap/ng2-bootstrap.js")
script(src="/node_modules/rxjs/bundles/Rx.js")
script(src="/node_modules/angular2/bundles/angular2.dev.js")

// 2. Configure SystemJS
script.
  System.config({
      packages: {
         app: {
            //format: 'register',
            //defaultExtension: 'js',
            defaultJSExtensions: true
        }
      },
          paths: {
          'ng2-bootstrap/*': 'node_modules/ng2-bootstrap/*.js'
          }
  });
  System.import('app/boot')
  .then(null, console.error.bind(console));
// 3. Display the application
body
    my-app Loading...

express

 app.use('/node_modules', express.static(__dirname + '/node_modules'));

app.component.ts

 //import "ng2-bootstrap/ng2-bootstrap";

   import {Component} from 'angular2/core';
   import {Alert} from 'ng2-bootstrap/ng2-bootstrap';
   import {firstComponent} from './component/first.component';

   @Component({
     //directives: [Alert],
      directives: [firstComponent,Alert],
      selector: 'my-app',
      template: `
        <h1>My First Angular 2 App</h1>
        <h2> helllo</h2>
        <first></first>
        <alert type="info">ng2-bootstrap hello!</alert>
        `
    })
    export class AppComponent { }

error console

GET http://localhost:3000/node_modules/ng2-bootstrap/components/accordion/accordion 404 (Not Found)
Error: XHR error (404 Not Found) loading http://localhost:3000/node_modules/ng2-

and also more errors the same as that re: cannot import component?

Answer

Wobbley picture Wobbley · Dec 31, 2015

I had the same issue myself and found this link helpful: https://github.com/valor-software/ng2-bootstrap/issues/50#issuecomment-165448962

A snippet of the most relevant code:

System.JS config:

System.config({
   packages: {
     "app": {
        "defaultExtension": "js"
     },
     "node_modules/ng2-bootstrap": {
        "defaultExtension": "js"
     }
  },
  paths: {
    "ng2-bootstrap":   "node_modules/ng2-bootstrap"
  }
});

You can then simply import it like so:

import { Progressbar } from "ng2-bootstrap";
@Component({
    selector: "my-app",
    directives: [
        Progressbar
    ],
    template: "<progressbar></progressbar>" 
})

Hope this helps you, like it did to me.