Tag Archives: nodejs

OSX Global NPM Module command not found

In case you ended up in a situation where you just installed a global NPM module, but it still throws command not found, here’s what you have to do:

Find out where the global NPM modules are installed by running:

npm prefix -g

Double check that your $PATH does not already contain that value:

echo $PATH

If the value is not included, you must update your etc/paths with the NPM location:

sudo vi /etc/paths

Add the value returned by npm prefix -g preceded by /bin

e.g. /Users/catalinmunteanu/.npm-global/bin

Save the file and exit.

Open a new terminal tab/window and retry the command.

Cheers!

Node n permission denied without sudo

Each time I do a fresh install of the Node Version Management tj/n I end up getting the permission denied when running npm install.

If you also ran into this issue, well, there’s a quick fix.

The issue is caused by the Node Version Management for which the owner is the root.

The two following steps will help you continue in peace 😀

which n

Returns the install location of the Node Version Management package. e.g. /Users/username/n

sudo chown -R $(whoami) <PATH_WHICH_N>

Sets the current user as owner.

You can now install the NPM packages without the power of sudo.

NPM module command not found

Until you properly config your environment you’ll probably run into the command not found error when trying to run globally installed NPM modules.

Solution for Linux and OSX:

Step 1 – check if the path is correct

You need to have /users/<ME>/.npm-global/bin in your $PATH

echo $PATH

If the printed string doesn’t contain the above path, continue to Step 2

Step 2 – add npm global install path

We need to adjust the $PATH in order to contain the path to the NPM global module folder:

export PATH=$PATH:~/.npm-global/bin

Step 3 – reload .bashrc

The .bashrc file is usually only reloaded on startup. Run the bellow command to reload it again after you made the changes from Step 2.

source ~/.bash_profile

That’s all. Your global NPM modules should now work in your terminal.

Socket.IO AngularJS Wrapper

I’ve been playing around with Socket.IO and AngularJS. To make things easier I created this service which can be used for transferring data between the client (which could be a web page) and a NodeJS server.

The app is served by NodeJS so the Socket.IO server is listening on same location.

'use strict';

angular
.module('APP')
.factory('$WS', function($rootScope) {
    var self = this;

    self.ready = false;

    self.socket = io.connect(location.host, {
        'connect timeout':       100,
        'reconnection delay':    100
    });

    self.socket.on('init', function() {
        console.log('init');
    });

    self.socket.on('disconnect', function() {
        console.log('socket disconnected');
    });

    self.socket.on('connect', function() {
        console.log('socket connected');
    });

    return {
        ready: function(callback) {
            if(self.ready) {
                callback();
                return;
            }

            self.socket.on('connect', function() {
                $rootScope.$apply(function() {
                    self.ready = true;
                    callback.apply(self.socket);
                });
            });
        },
        on: function(eventName, callback) {
            self.socket.on(eventName, function() {
                var args = arguments;

                $rootScope.$apply(function() {
                    callback.apply(self.socket, args);
                });
            });
        },
        emit: function(eventName, data, callback) {
            self.socket.emit(eventName, data, function() {
                var args = arguments;
                $rootScope.$apply(function() {
                    if(callback) {
                        callback.apply(self.socket, args);
                    }
                });
            });
        }
    };
});

Usage:

angular
.module('APP')
.controller('TestController', ['$scope', '$WS', function($scope, $WS) {

    $scope.messages = [];

    // Connect websocket
    $WS.ready(function() {
    
        // Receive message
        $WS.on('chat/message', function(data) {
            
            // Validate input data
            if(data.message_id) {
                console.log(data);
                $scope.messages.push(data);
            }
        });
    });

    // You can add another ready state listener for the same connection
    // Let's consider we have a method which sends messages
    $scope.send_message = function(msg_title, msg_value) {
        
        $WS.ready(function() {
            // Emit message
            $WS.emit('chat/message', {
                title:    msg_title,
                message:  mgs_value
            });
        });
    };

}]);

 

This is just basic usage.  Will be back with NodeJS & Socket.IO server example.

NodeJS AES256 Encryption/Decryption

In one of my projects I needed to encrypt some data. I decided to use AES256 symmetrical encryption algorithm.

So this is what I’ve come to (aes_encryption.js):

var crypto	= require('crypto'),
    cipher_seed = 'some_random_characters';
 
var encrypt = function(text) {
	var cipher	= crypto.createCipher('aes-256-cbc', cipher_seed),
	    crypted	= cipher.update(text, 'utf8', 'hex');

	crypted += cipher.final('hex');

	return crypted;
};
 
var decrypt = function(text) {
	var decipher  = crypto.createDecipher('aes-256-cbc', cipher_seed),
	    decrypted = decipher.update(text, 'hex', 'utf8');

	decrypted += decipher.final('utf8');

	return decrypted;
};
 
module.exports.encrypt = encrypt;
module.exports.decrypt = decrypt;

Usage:

var AES = require('path_to_aes_encryption.js');

var data = {
    name:    'Catalin',
    surname: 'Munteanu',
    address: 'Romania'
};

// Encryption
var encrypted_data = AES.encrypt(JSON.stringify(data));

// Decryption
var decrypted_data = JSON.parse(AES.decrypt(encrypted_data));

Don’t forget that if the cipher seed is changed or lost all the encrypted data is also lost.

Easy, right?

 

 

Finalize – JavaScript Asynchronous Requests Queue

Most web apps send lots of asynchronous http requests. Sometimes you need to know when some selected requests are finished so that you can pass to the next step.

The following function can be used for creating a queue with asynchronous requests using JavaScript and be notified when the queue is empty again (all selected requests are finished):

function Finalize(_callback) {
	var count = 0,
	    exec  = false;

	if((typeof _callback !== 'undefined') && (_callback !== null)) {
		this.exec = function() {
			exec = true;
			if(count === 0) {
				_callback();
			}
		}
		this.queue = function() {
			count++;
			return function() {
				count--;
				if(exec && (count === 0)) {
					_callback();
				}
			}
		}
	} else {
		this.exec  = function() {};
		this.queue = function() {};
	}
}

Usage:

var request_set = new Finalize(function() {
    // Code inside this function will be executed at the end
});

// To add an item to the queue
var first_item = request_set.queue();

// Let's add another item
var second_item = request_set.queue();

// The code inside the final callback will only be called after
// all the items in queue are completed.
// Let's consider we have two async requests:
jQuery.get('http://example.com', function(data) {
    // We now have the response from the first request
    // Remove one item from queue by calling the item's name
    first_item();
});

// The items can also be removed by passing them directly as callback
jQuery.get('http://example.com', second_item);

// You can even skip declaring items and create them on the fly as the callback function
jQuery.get('http://example.com', request_set.queue());

// After you've added all items to the queue we need to execute it
request_set.exec();

The function can also be used in NodeJS.