Showing posts with label node js. Show all posts
Showing posts with label node js. Show all posts

Jan 28, 2014

[Solved] Error in npm install from a github url

Posted Jan 28, 2014
If you're like me who's used to executing "git clone [github url]" then you may encounter an unusual behavior when using "npm install [github url]" or specifying the git repository inside package.json.

Here's a sample error of using npm install [github path]:

E:\progs\workspace\tmp>npm install https://github.com/mannyvergel/oils-js.git
npm http GET https://github.com/mannyvergel/oils-js.git
npm http 200 https://github.com/mannyvergel/oils-js.git
npm ERR! not a package C:\Users\Manny\AppData\Local\Temp\npm-5216\1390883860006-
0.930888706818223\tmp.tgz
npm ERR! Error: ENOENT, open 'C:\Users\Manny\AppData\Local\Temp\npm-5216\1390883
860006-0.930888706818223\package\package.json'
npm ERR! If you need help, you may report this log at:
npm ERR!     <http://github.com/isaacs/npm/issues>
npm ERR! or email it to:
npm ERR!     <npm-@googlegroups.com>

npm ERR! System Windows_NT 6.2.9200
npm ERR! command "E:\\progs\\nodejs0.10.7\\\\node.exe" "E:\\progs\\nodejs0.10.7\
\node_modules\\npm\\bin\\npm-cli.js" "install" "https://github.com/mannyvergel/o
ils-js.git"
npm ERR! cwd E:\progs\workspace\tmp
npm ERR! node -v v0.10.7
npm ERR! npm -v 1.2.21
npm ERR! path C:\Users\Manny\AppData\Local\Temp\npm-5216\1390883860006-0.9308887
06818223\package\package.json
npm ERR! code ENOENT
npm ERR! errno 34
npm ERR!
npm ERR! Additional logging details can be found in:
npm ERR!     E:\progs\workspace\tmp\npm-debug.log
npm ERR! not ok code 0


The solution is very simple, just prefix your github url with "git+" e.g.
git+https://github.com/mannyvergel/oils-js.git

Use the format in your package.json or npm install, e.g.
npm install git+https://github.com/mannyvergel/oils-js.git



Jan 25, 2014

[Solved] NodeUnit does not finish when connected to Mongo DB

Posted Jan 25, 2014
When running NodeUnit tests and your tests are connected to Mongo DB, the tests do not end and it just hangs there. The fix is to close the connection on NodeUnit's tearDown function.

Example below is using Mongoose' disconnect function to close the connection after NodeUnit tests are done:
var mongoose = require('mongoose');
exports.tearDown = function(ok){
    mongoose.disconnect(function(err){
        if(err) {
            console.error(err);
            return;
        }
    });
    ok();
};



Mar 14, 2013

Convert Twitter's created_at to Javascript Date

Posted Mar 14, 2013
There are several solutions found on the web to parse Twitter's date, but some seems outdated, maybe Twitter changed its date format. But as of March 2013, the code below works:

function parseTwitterDate(aDate)
{   
  return new Date(Date.parse(aDate.replace(/( \+)/, ' UTC$1')));
  //sample: Wed Mar 13 09:06:07 +0000 2013 
}

Source 

Feb 7, 2013

Rounding a number to two decimal places in Javascript / Node JS

Posted Feb 7, 2013
Rounding numbers with decimals in Javascript is not straightforward. Javascript has a utility Math.round but it only rounds to the nearest integers. Some uses toFixed function e.g. (2.55555).toFixed(2) but it doesn't work consistently across all browsers, sometimes it just truncates the value which makes it troublesome for computations.

Creating a simple function for rounding numbers to two decimals in Javascript should be easy:

function roundTo2Decimals(numberToRound) {
  return Math.round(numberToRound * 100) / 100
}

//test
roundTo2Decimals(2.55555555);

If you want to use a library, numeraljs is a good one. It also formats numbers with commas. Very useful.


Dec 3, 2012

[Solved] JQuery File Upload is Not Working with Express 3

Posted Dec 3, 2012
I tried to incorporate the Node js implementation of JQuery File Upload with Express 3. It uses Formidable middleware for parsing multi-part data which is not compatible with Express 3's body parser. Fortunately, there's a quick fix:

//just replace express.bodyParser() with:
app.use(express.json());
app.use(express.urlencoded());

From Myk's explanation in StackOverflow, Express uses express.bodyParser() as a convenience method to wrap 3 parsers (express.json, express.urlencoded and express.multipart) into one.

Alternative Solution:

Another approach is to redefine the multipart parser of Express (co elmigranto):

var express = require('express');
var bodyParser = express.bodyParser;

bodyParser.parse('multipart/form-data') = function(req, options, next) {
 //custom parser or:
  next();
}

//don't forget to use the variable bodyParser in your configuration