Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 506
Ivar Reukers Avatar asked Dec 16 '16 13:12

Ivar Reukers


2 Answers

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'}
}
like image 142
Ankit Singh Avatar answered Sep 30 '22 13:09

Ankit Singh


Apologies but my corp proxy won't let me view the attached images... But it looks as though you haven't statically exposed node_modules through your webserver.

You haven't specifically mentioned your server technology, but with node/express put this in app.js:

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

Alternatively, use a build script to copy relevant js files to your express scripts (or similar) folder, and point your system.config mapping to the scripts folder instead.

Remember that the /// <reference.../> tag is only a hint to the Typescript transpiler and intellisense for type discovery. This is no longer necessary in Typescript 2, where @types/ are auto discovered.

like image 29
johnDisplayClass Avatar answered Sep 30 '22 13:09

johnDisplayClass