NodeJS library within Ionic

node version: v4.4.4
npm version: 3.9.2
ionic version (app): 2.0.0-beta.7
amqplib version: 0.4.1

I am currently trying to develop an app using Ionic 2 framework and I have decided to introduce messaging in my app by using RabbitMQ within this library. Feel free to inspect the code here for any further reference.

  1. First of all, I installed the library manually using npm install because the release version from npm is outdated.

  2. After that, I added the Typescript definitions for the library via typings install dt~amqplib --global --save.

  3. I created a new page for my app called Page2 where the library is imported…

     import * as amqp from 'amqplib/callback_api';

    … and used to connect to the server…

     setConnection() {
         amqp.connect(this.connectionUrl, (err: any, connection: amqp.Connection) => {
             this.connection = connection;
             this.connection.createChannel((err: any, channel: amqp.Channel) => {
        = channel;
       , 'topic', { durable: false });
  4. The problem comes when I try to run it (I have done it using both an emulator and a native device running Android). If I try to hit the Set connection button, I get the following error:

The error is linked to the line sock = require('net').connect(sockopts, onConnect); of connect.js file.


I was told in this post in StackOverflow that my problem was the fact of using a Node library in my app, because there is no Node in ionic 2 and I should run a node app separately. Is that true? Is it not possible to use a Node libray and a require statement in Ionic 2?

Node runs on the server side. Ionic is client side (you will build with Ionic an app that can communicate to a server application).

This is correct. Ionic just a UI library ontop of angular. It run in the browser, which cannot run node.js modules. Modules like express or amqp in the instance, cannot run in the browser. They’re meant to run server side on a node server.

Well, how do you include functionality and libraries like ampqlib in such a context? Using a feature like AMQP makes a lot of sense for a client …

You can’t. Libraries like ampqlib would need to be run on a server.

If you do have a serve setup with ampqlib, from there is just building out your own API that can interact with the library.

If you need some sort of realtime messaging in your app, you could always checkout, which has it’s own client side library.

There are a few things I do not understand here:

  1. Ionic is built on top of phonegap is built on top of node. As long as there is no native component to a node.js module I would expect things to work. I do not fully understand why a node.js module cannot be loaded, especially if it is a pure javascript library. Could you please explain what hinders the node.js module to be loaded?

  2. As far as I can see ampqlib is only a pure Javascript library. How do you add a Javascript (or Typescript) library to an ionic project? Can I e.g. wrap it into an Angular 2 module and use it from there? Are there any other possibilities how we can take the pure HTML5 code and provide it to an ionic app?

I was thinking of some functionality like browserify. Also in comparison NW.js supports node.js module loading and even native modules. So in theory it is possible …

This is false. While the CLI tooling is built on node, the actual runtime has ZERO node instances.
A node.js module may require access to the filesystem by var fs = require('fs');. Since there NO node instance in a cordova app, this will not run at all.

As for ampqlib, it’s the same thing. It’s expecting to run in a node runtime and have access to features. So you cannot wrap it in an Angular 2 wrapper, since it expects to run on the server

NW.js can allow for this because of how it is set up. It has two run times, one is a chrome webview for rendering, and a node runtime for executing server side node. This is the same thing with Electron.
Cordova on the other hand is not setup to run this way.

Thanks for this clarification. A shock for us, but the truth has to be told. My imagination was that I could use all node packages within an ionic app.

So at least in theory it would be possible to add this feature to ionic, too? Running a node instance within an ionic app?

No, this is not possible. This would require changes to be made to the browser.

When Ionic is used inside of an electron environment. It is absolutely possible to access node modules from the renderer process (where ionic Javascript would be executed). Yet this problem prevents it…