JavaScript (Client/Server), nodeJS

SyntaxError: Use of const in strict mode

I’m working with node.js, and in one of my js files I’m using const in "strict mode". When trying to run it, I’m getting an error:

SyntaxError: Use of const in strict mode.

If this is happening in nodejs, it is due to the older version of nodejs. Update node by using,

1) Clear NPM’s cache:

sudo npm cache clean -f

2) Install a little helper called ‘n’

sudo npm install -g n

3) Install latest stable NodeJS version

sudo n stable

Update nodejs instructions taken from, http://stackoverflow.com/a/19584407/698072

 

Read More...

nodeJS

Using underscore.js with node.js

Underscore.js:

Underscore is a JavaScript library that provides a whole mess of useful functional programming helpers without extending any built-in objects. It’s the answer to the question: “If I sit down in front of a blank HTML page, and want to start being productive immediately, what do I need?” … and the tie to go along with jQuery‘s tux andBackbone‘s suspenders.

Underscore provides over 100 functions that support both your favorite workaday functional helpers: mapfilterinvoke — as well as more specialized goodies: function binding, javascript templating, creating quick indexes, deep equality testing, and so on.

Source: http://underscorejs.org/

To install underscore.js in a node.js project use: npm install underscore

After installing it you can use underscore now on your node.js project as : var _ = require(‘underscore’);

Things you can do with _ :

1. Merge two json objects:

var obj1 = {foo:”bar”};

var obj2 = {name:”john”};

var merged = _.extend(obj1,obj2);

 

 

Read More...

nodeJS

Error: /lib64/libc.so.6: version ‘GLIBC_2.14’ not found

Sometimes on your node.js project you have seen problems like “Error: /lib64/libc.so.6: version ‘GLIBC_2.14’ not found” by some npm modules.

Error: /lib64/libc.so.6: version ‘GLIBC_2.14’ not found

This error comes when the available version of glibc is not compatible by the node module.

So, if you still want to work with that module, you need to rebuild that module on your project.

To do this simply go to Github, Clone a repo for node module where its installed in project, may be in node_modules directory.

Inside the node_modules directory clone that repo:

Example- git clone https://github.com/node-xmpp/node-expat.git

Now go to the cloned module, cd node-expat

Then install all node module dependencies.
npm install

After installing it to a clean build for that module:
node-gyp rebuild

Voila! Use the module in your project now.

Read More...

PhantomJS

PhantomJs: Convert a String to an Image/PDF.

To convert a string or HTML to PNG or PDF using phantomjs:

Create a File say texttoimage.js

Add this code:

var page = require(‘webpage’).create();
page.viewportSize = { width: 200, height : 200 };
page.content =  “This Text will be converted to Image”;
page.setContent(page.content,page);
window.setTimeout(function () {
page.render(‘newimage.png’);
phantom.exit();
}, 1000);

Save and Run the Script from console: phantomjs texttoimage.js

A image named newimage.png will be generated on same directory where your script is saved.

Here is the output:

PhantomJS : Converted Text to Image

Read More...

JavaScript (Client/Server)

JavaScript – Executing Multiple window.onload events on a webpage

For executing multiple window.onload add this script to the head head section:

<script language=”javascript” >
function addLoadEvent(func) {
var oldonload = window.onload;
if (typeof window.onload != ‘function’) {
window.onload = func
} else {
window.onload = function() {
if (oldonload) {
oldonload()
}
func()
}
}
}
</script>

Add Function to window.onload event use :

addLoadEvent(FunctionToRunOnPageLoad);

“or”

addLoadEvent(function() {
/* code to run on page load *
});

Read More...