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

There is always a problem with setting up an external project in Angular2.

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

Although README.md just says npm install ng2-sharebuttons --save , I know there is something else ( systemjs.config & typings )

First I tried to configure systemjs.config :

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

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

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

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

But now this results in a 404 for each ng2-sharebuttons/dist child

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

So, on my last hope, I added a link to see if this would work. Adding

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

But that didn’t change anything.

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

The structure and files of ng2-sharebuttons :

enter image description here

And finally, the error message that Chrome is currently producing:

enter image description here

The whole 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 , as well as XHR finished loading: GET .... in the same component / service / file, which seems rather strange.

+6
source share
2 answers

You need to do it

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

Sorry, but my corp proxy will not let me view attached images ... But it looks like you are not statically exposing node_modules through your web server.

You did not mention your server technology specifically, but with node / express put this in app.js:

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

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

Remember that the /// <reference.../> is just a hint for transpiler Typescript and intellisense for type detection. This is no longer required in Typescript 2, where @ types are automatically opened.

+1
source

Source: https://habr.com/ru/post/1013218/


All Articles