4

I'm new to angular2, npm and systemjs and I can't figure out how to load and use downloaded node_modules. Let's say I want to load first jQuery and then bootstrap. I modified my systemjs but there are some errors...

Errors:

(index):21 Error: Error: Bootstrap's JavaScript requires jQuery(…)(anonymous function) @ (index):21ZoneDelegate.invoke @ zone.js:323Zone.run @ zone.js:216(anonymous function) @ zone.js:571ZoneDelegate.invokeTask @ zone.js:356Zone.runTask @ zone.js:256drainMicroTaskQueue @ zone.js:474ZoneTask.invoke @ zone.js:426 (index):20 Error: ReferenceError: module is not defined(…)

index.html

<!doctype html>
<html>
  <head>
    <base href="/"/>
    <title>Angular 2 QuickStart</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="styles.css">
    <link rel="stylesheet" href="node_modules/bootstrap/dist/css/bootstrap.min.css">
    <!-- 1. Load libraries -->
     <!-- 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/reflect-metadata/Reflect.js"></script>
    <script src="node_modules/systemjs/dist/system.src.js"></script>
    <!-- 2. Configure SystemJS -->
    <script src="systemjs.config.js"></script>
    <script>
      System.import('app').catch(function(err){ console.error(err); });
      System.import('jQuery').catch(function(err){ console.error(err); });
      System.import('bootstrap').catch(function(err){ console.error(err); });
    </script>
    <!--<script src="node_modules/bootstrap/dist/js/bootstrap.min.js"></script>-->
  </head>
  <!-- 3. Display the application -->
  <body>
    <app>Loading...</app>
  </body>
</html>

systemjs.config.js

(function(global) {
  // map tells the System loader where to look for things
  var map = {
    'app':                        'app', // 'dist',
    '@angular':                   'node_modules/@angular',
    'rxjs':                       'node_modules/rxjs',
    'jQuery':                     'node_modules/jQuery/lib/node-jquery.js',
    'bootstrap':                  'node_modules/bootstrap/dist/js/bootstrap.min.js'
  };
  // packages tells the System loader how to load when no filename and/or no extension
  var packages = {
    'app':                        { main: 'main.js',  defaultExtension: 'js' },
    'rxjs':                       { defaultExtension: 'js' }
  };
  var ngPackageNames = [
    'common',
    'compiler',
    'core',
    'forms',
    'http',
    'platform-browser',
    'platform-browser-dynamic',
    'router',
    'router-deprecated',
    'upgrade'
  ];
  // Individual files (~300 requests):
  function packIndex(pkgName) {
    packages['@angular/'+pkgName] = { main: 'index.js', defaultExtension: 'js' };
  }
  // Bundled (~40 requests):
  function packUmd(pkgName) {
    packages['@angular/'+pkgName] = { main: '/bundles/' + pkgName + '.umd.js', defaultExtension: 'js' };
  }
  // Most environments should use UMD; some (Karma) need the individual index files
  var setPackageConfig = System.packageWithIndex ? packIndex : packUmd;
  // Add package entries for angular packages
  ngPackageNames.forEach(setPackageConfig);
  var config = {
    map: map,
    packages: packages
  };
  System.config(config);
})(this)

;

5
  • Did you find any solution for this @phip1611 Commented Aug 26, 2016 at 5:47
  • i would love to take the challenge @JagadeeshGovindaraj :-) give me sometime! Commented Aug 26, 2016 at 6:03
  • take your time @MrJSingh. i am wating. Commented Aug 26, 2016 at 6:12
  • @JagadeeshGovindaraj i have post an answer with working solution if you want i can provide the source code as well :-) Commented Aug 26, 2016 at 6:36
  • Thanks guys. I also had the problem that I didn't installed jQuery but jQuery Node. Due to I'm new to all of this I had no clue there is a difference. Commented Aug 26, 2016 at 12:58

1 Answer 1

11
+50

Here is how you can load jquery and bootstrap with systemjs #

installation

npm install --save-dev bootstrap jquery

Adding main files in systemjs.config.js

'bootstrap':                  'node_modules/bootstrap/dist/js/bootstrap.min.js',
'jquery':                     'node_modules/jquery/dist/jquery.js'

Using it in your project in main.ts

import "jquery";
import  "bootstrap";

That's it you have jquery and bootstrap in your build

Source code

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

5 Comments

any example i did not get what you try to say.where can i put import statement ,i am new to angular 2.
you can put in main.ts to include it in your whole project
And now you placed bounty on this question and I would appreciate if you can reward me 😉
TMRW I WILL REWARD TO YOU

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.