Using a browserified JS library with Angular 2

alexandernst :

I want to use libquassel (https://github.com/magne4000/node-libquassel) in my Angular 2 project. The library is browserified, so in theory it should work, but I'm not really sure how to import it in my project.

I tried adding to my typings.d.ts

declare module 'libquassel';

and then importing the library with

import * as Quassel from 'libquassel';

but I get

EXCEPTION: net.Socket is not a function

when I try to run my code, which I believe is another library that browserify embedded in the client/libquassel.js file.

How can I use this library?

Edit: I'll answer all questions here:

  • My setup is a plain angular-cli project. No fancy stuff, just ng new proj1 and then npm install libquassel --save.
  • My index.html doesn't have anything else that ng new hasn't placed in there.
  • I tried importing the library with import * as Quassel from 'libquassel' and var Quassel = require('quassel') (and permutations of those), without any luck (errors varying from unknown function 'require' to can't find module lib|quassel).
  • Steps to repro my project:

    ng new test
    cd test
    npm install libquassel --save
    ng g s quassel
    

Then add QuasselService to the providers array in app.module.ts. This would be a good demo of my problem, which is how to import libquassel in my QuasselService.

SergGr :

Update (fix it, this time for real)

There is a very good reason why it doesn't work in your code: because I lied to you. So here is my real cheat in case you didn't "diffed" it yourself yet.

I still need to require libquassel 2 times but the first time is done not by the call to require which is useless but by adding it to angular-cli.json to the scripts section:

  "scripts": [
    "../node_modules/libquassel/client/libquassel.js"
  ],

This is important because "client/libquassel.js" declares its own require but do not explicitly exports it so if you do

require('libquassel/client/libquassel.js');

libquassel's custom require is left stuck inside anonymous namespace and you can't access it. Adding it to the scripts section on the other hand, lets libquassel.js to pollute the global namespace (i.e. window) with its require and thus make it work.

So the actual steps to make it work are:

  1. Add client/libquassel.js to the scripts section of angular-cli.json
  2. Use additional require to actually load Quassel module
let Quassel = window['require']('quassel'); // note that usingg require('quassel') leads to compilation error
let quasselObj = new Quassel(...);

Here is my attempt. It looks like you need to require "quassel" 2 times: first time to load the JS from "../node_modules/libquassel/client/libquassel.js" and second time to let the overridden require from that JS actually resolve "quassel". Here is a trick that seems to work for me in 'main.ts':

require('../node_modules/libquassel/client/libquassel.js');
let Quassel = window['require']('quassel'); // note that using require('quassel') leads to compilation error
let quasselObj = new Quassel(...);

I needed one more trick to not fail with a compilation error: particularly I had to use window['require'] instead of just require for the second call as it is a call for the inner require as defined inside client/libquassel.js and Node/Angular-cli alone can't handle it.

Note that after that setup my application still failed with a runtime error in some AJAX because browsified libquassel.js seem to require a proxy set up on the server-side at URL like /api/vm/net/connect and this is probably what server-side of the net-browsify should do but I didn't try to set it up.

Answer to comments

It looks like you use old version of Angular-CLI and if you upgrade everything should work fine.

Here is what ng --version tells me on my original machine

angular-cli: 1.0.0-beta.28.3
node: 6.10.0
os: win32 x64
@angular/common: 2.4.10
@angular/compiler: 2.4.10
@angular/core: 2.4.10
@angular/forms: 2.4.10
@angular/http: 2.4.10
@angular/platform-browser: 2.4.10
@angular/platform-browser-dynamic: 2.4.10
@angular/router: 3.4.10
@angular/compiler-cli: 2.4.10

When I tried to copy my original project to a different machine I also got a compilation error about require but when I updated Angular-CLI to

@angular/cli: 1.0.0
node: 6.10.0
os: darwin x64
@angular/common: 2.4.10
@angular/compiler: 2.4.10
@angular/core: 2.4.10
@angular/forms: 2.4.10
@angular/http: 2.4.10
@angular/platform-browser: 2.4.10
@angular/platform-browser-dynamic: 2.4.10
@angular/router: 3.4.10
@angular/cli: 1.0.0
@angular/compiler-cli: 2.4.10

it compiled and worked the same. What I did is just followed the instruction

The package "angular-cli" has been deprecated and renamed to "@angular/cli".

Please take the following steps to avoid issues:
"npm uninstall --save-dev angular-cli"
"npm install --save-dev @angular/cli@latest"

and then updated angular-cli.json following the instructions by ng serve

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to use moment.js library in angular 2 typescript app?

how to use underscore.js library in angular 2

Is Angular JS a library or a framework?

Google API Endpoints using Javascript client library as an Angular 2 Service

Using flow.js in Angular 2 Typescript

Angular 2 Component Library

How to use Numeral.js library in angular2/ionic 2 typescript app?

import JS library in Angular

Angular using Google fonts library

Download PDF not working with Firefox using Angular 2 and Node.js

using third party library in angular

how to break cell in excel using FileSaver library in angular2

Get full access token from Auth0 using angular-oauth2-oidc library

@ViewChild is undefined in Angular component, using angular2-signaturepad library

Angular library using forRoot

Using GreenSock JS library with Webstorm

How to use Browserified npm package in Angular controller?

TypeScript type definition; export default for using external JavaScript library in Angular2

Integrate the ng2-ace library into a freshly created angular-cli (angular2) project using SystemJS

Use JS library xml2js with Angular 2

Using Angular 2 to call jQuery based Javascript ajax library

Angular 2 animations (using a different animation library e.g GreenSock Application Platform)

Cannot load phoenix js library with SystemJS in angular2

Importing external JS Library to Angular 2 CLI @NgModule

Consume angular 2 custom library

Using vanilla js code in Angular 2 (angular-cli)

How to call an external javascript library function within a Typescript class using Angular 2/4?

Use a pure JS library in Angular

How to use thirdparty js library in Angular 2+ without installing types?