Search code examples
angularwebpacksystemjsnode-modulesangular2-cli

node_modules in AngularJS 2 [ng cli] without System.config


How do I include node_modules without System.config with AngularJS 2 projects generated with ng?

Attempts (in src/index.html):

<link rel="stylesheet" href="./node_modules/bootstrap/dist/css/bootstrap.min.css">
<link rel="stylesheet" href="node_modules/bootstrap/dist/css/bootstrap.min.css">
<link rel="stylesheet" href="bootstrap/dist/css/bootstrap.min.css">
<link rel="stylesheet" href="dist/css/bootstrap.min.css">
<link rel="stylesheet" href="css/bootstrap.min.css">
<link rel="stylesheet" href="bootstrap.min.css">

Project setup:

npm install -g angular2-cli
ng init
npm install
npm install --save bootstrap@4.0.0-alpha.5
npm start

Note: [by default] there is neither systemjs.config.js nor any reference to System.config in the file-tree anymore…

EDIT: Would be great if all my included node_modules are also pushed into the one bundle (e.g.: from ng build -prod).


Solution

  • To include node_module assets such as css with your build, you need to list it in angular-cli.json like this:

    {
      "apps": [
        {
          "styles": [
            "../node_modules/bootstrap/dist/css/bootstrap.css"
          ]
        }
      ]
    }
    

    Also checkout Global Library Installation from the angular-cli readme.

    When you run ng build -prod the cli will bundle the listed files.