Search code examples
angulartypescriptsystemjsnode-modules

External javascript library returns 404 on '{module}/dist/'


Always having some trouble configuring an external project in Angular2.

I want to include ng2-sharebuttons in my project. (which does not seem to have a global .d.ts file?)

Although the README.md just says npm install ng2-sharebuttons --save, I know there are more things to this (systemjs.config& typings)

First I tried to configure systemjs.config having:

map{
     ......
     'ng2-sharebuttons' : 'node_modules/ng2-sharebuttons'
}

This at least fixed the 404 on localhost/ng2-sharebuttons but created a new 404 on localhost/node_modules/ng2-sharebuttons.

So, I added a main file, changing systemjs.config to

map{
     ......
     'ng2-sharebuttons' : 'node_modules/ng2-sharebuttons/dist/index.js'
}

But now this results in a 404 on every child of the ng2-sharebuttons/dist

I tried doing typings install ng2-sharebuttons --save --global and typings install @types/ng2-sharebuttons --save --global both with and without the --global flag.

So on my last hope I added a reference to see if that would work. Adding

/// <reference path="../../node_modules/ng2-sharebuttons/dist/index.d.ts" />

But this didn't change anything.

What did I forget? Why is this so hard? (Or am I just making it hard?)

Structure & files of ng2-sharebuttons:

enter image description here

And lastly, the error message currently being produced by Chrome:

enter image description here

Entire 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',
      // other libraries
      'rxjs': 'npm:rxjs',
      'angular-in-memory-web-api': 'npm:angular-in-memory-web-api/bundles/in-memory-web-api.umd.js',
      'angularfire2' : 'npm:angularfire2/bundles/angularfire2.umd.js',
      'firebase' : 'npm:firebase',
      'ng2-bs3-modal': 'npm:ng2-bs3-modal',
      'ng2-sharebuttons' : 'npm:ng2-sharebuttons/dist/index.js',
      'ng2-social-share' : 'npm:ng2-social-share/bundles/ng2-social-share.js'
    },

    // 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'
      },
      firebase: {
        main: './firebase-browser.js',
        defaultExtension: 'js'
      }
    }
  });
})(this);

It throws an error GET .... 404 but also a XHR finished loading: GET .... on the same component/service/file which seems quite weird.


Solution

  • You need to do this

    map:{
       'ng2-sharebuttons' : 'npm:ng2-sharebuttons/dist'
    },
    
    packages: {
       app: {main: './main.js', defaultExtension: 'js'},
       rxjs: {defaultExtension: 'js'},
    
       'ng2-sharebuttons': {main: 'index.js', defaultExtension: 'js'}
    }