6

I'm using angular2-webpack-starter to build my project and I want to use bootstrap as well.

I install ng2-bootstrap as npm install ng2-bootstrap --save. Since the ng2-bootstrap only has some directives and it requires the "real" .css file of bootstrap to style it (but its author seems to assume we have had it), so I install bootstrap as npm install bootstrap --save.

Now, my question is how to import the "real" bootstrap .css file to my project, so that ng2-bootstrap can use it.

I tried several ways:

  1. copy the bootstrap.min.css file from node_modules/bootstrap/dist/css folder to my src/assets/css folder and add <link href="assets/css/bootstrap.min.css" rel="stylesheet"> to my index.html. This way works but my concern is that the bootstrap.min.css file won't be managed by npm any more. I have to manually update it in future.

  2. Another attempt is requiring it from my app.component.ts like

    styles: [ require('./app.style.css'), require('../../node_modules/bootstrap/dist/css/bootstrap.min.css') ],

but it can't be resolved.

  1. last try is adding import 'bootstrap'; to vendor.browser.ts just like import '@angular/core'; in it. But it failed either. It seems that the bootstrap is not a package like @angular2/core that I can easily import it.

So, my question comes down to how to import/load bootstrap in webpack, so that it can be used by ng2-bootstrap and other components in my project and it can also be upgraded by using npm.

4
  • Regarding number 2. Are you sure you can't import css directly from node_modules? I have no problem doing this. Commented Aug 3, 2016 at 13:39
  • @Helzgate I can't. How do you import it, is it like require('../../node_modules/bootstrap/dist/css/bootstrap.min.css')? Commented Aug 3, 2016 at 22:01
  • I don't know, it just works, i've been using it this way in many places for some time in my app (and yes i'm talking about from node_modules). Are you absolutely sure you have enough elipses? I would try adding some more ../ and trying that, or subtracting some. Pay attention to the error too, maybe it is loading the css but you are running into some other error. Commented Aug 4, 2016 at 16:07
  • Here's what I would do --> git clone --depth 1 github.com/AngularClass/angular2-webpack-starter.git <-- then I would get this new cloned demo working, then I would test out your css within this demo app and if you get it working (which I'm sure you will), hopefully you'll find the missing problem by comparing the webpack.config files to your project config files, or something similar. Commented Aug 4, 2016 at 16:16

5 Answers 5

5

you need to use css-loader download css-loader by using, i did some test in my angular 2 and it work you need some loaders

npm install css-loader style-loader url-loader file-loader  --save-dev

then in your webpack.config you can use loader like this

 loaders: [
    {test: /\.css$/, loader:   ['style-loader', 'css-loader']},
    {test: /\.woff(\?v=\d+\.\d+\.\d+)?$/, loader: "url?limit=10000&mimetype=application/font-woff" },
    {test: /\.ttf(\?v=\d+\.\d+\.\d+)?$/, loader: "url?limit=10000&mimetype=application/octet-stream" },
    {test: /\.eot(\?v=\d+\.\d+\.\d+)?$/, loader: "file" },
    {test: /\.svg(\?v=\d+\.\d+\.\d+)?$/, loader: "url?limit=10000&mimetype=image/svg+xml" },
    {test: /\.html$/, loader: 'raw',exclude: /node_modules/},
    {test   : /\.(ttf|eot|svg|woff(2)?)(\?[a-z0-9=&.]+)?$/,loader : 'file-loader'},
]

you can load your bootstrap file where you are going to use it with and you can use bootstrap in your class like

import "bootstrap/dist/css/bootstrap.css";
    @Component({
        selector: "sg-nav",
        template: `
                    <div class="container"></div>`,
    })
    export class NavComponent {
        public name: string = "MR.Js";
    }

here you can read how it works with css loader

working example with bootstrap

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

3 Comments

can you tell how to use this css loader with angular-cli.i a not able to refer to my bootstrap in node_modules
This solution is similar to my No. 1 solution. The .css is loaded but it's loaded from src folder rather than from node_modules/bootstrap. So, if I update version in package.json and npm install, the css file won't get updated.
i have implemented bootstrap in my angular2 project and edited my answer to show you how it work. try it!
0

import 'bootstrap'; would refer to Bootstrap's JS entry point, not its CSS. Try number 3 with

import 'bootstrap/assets/css/bootstrap.min.css';

instead.

2 Comments

It doesn't work. I get ERROR in ./~/bootstrap/dist/css/bootstrap.min.css. The css file is in node_modules/bootstrap/dist/css/bootstrap.min.css but it seems that we can't import a .css file in a .ts file. It can't be resolved.
but how to add jquery? When I import files to vendor I get Uncaught Error: Bootstrap's JavaScript requires jQuery but Css works just fine
0

maybe you can try like this

import 'bootstrap';
import '!style-loader!css-loader!bootstrap/assets/css/bootstrap.min.css';

the first 'import' will import the 'bootstrap' module(maybe refer to bootstrap.js);

the second one may import the css.

and in your webpack.config.js you can use loader

Comments

0

You could set it up via Webpack, but this requires installing and adding loaders to your webpack.config.js (css-loaders, style-loaders, url-loaders, file-loaders), then importing the bootstrap.min.css file and bootstrap files in your vendor.ts.

I think this is pretty verbose. Rather than the above, you could just simply install (eg using npm) and add the one line below your scripts (the minified webpack js files) to your index.html

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">

Comments

0

There might be a better way, but I'm using bower to handle css-related libs (e.g. bootstrap, font-awesome) in my ng2 project.

Set your .bowerrc to save files into: src/assets/lib (for example) like this:

{
    "directory": "src/assets/lib",
}

Then install bower with npm.

npm install bower --save-dev

And then you can install bootstrap with:

bower install bootstrap --save to add (and if you want to pin the version number) to your bower.json file.

The nice thing about this solution is that bower handles all dependencies for you (for example, it would automatically add jquery to your lib directory)

Then you can manually add your links in your index.html, as you already did: <link rel="stylesheet" href="./assets/lib/bootstrap/dist/css/bootstrap.min.css"/>

I added the src/assets/lib folder to my .gitignore ... and for installing/deploying, I run bower install.

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.