Using ES6 in nodeJS

Are you getting bored writing the ES5 code in nodeJS, then this is time to move to ES6. However node 6 has built in support for ES6, this post is meant for people using older versions of node.

  • Install babel-cli in your project
    npm install --save babel-cli
  • Install ES2015 presets for babel
    npm install --save babel-preset-es2015
  • If you want to compile your entire server code to distribution then you can use
    babel server/index.js -d output.js
    and then run
    node output.js
  • Else if you just want to run your code then use
    babel-node server/index.js
    This will automatically compile and run the es6 code for you.

Enjoy programming in ES6.

Advertisements

Contributing packages to npm

If you are a open source developer then you may feel to contribute to open source as well. This post helps you to get started with contributing to open source especially npm (Since I am a javascript lover).

Prerequisites:

  • Install node
  • Install npm

Creating the package:

  • Create a directory

$ mkdir <your project name>

  • Create a npm project

$ cd <your project name>
$ npm init

  • Give details as required.
  • Create your project files
    $ vi somefile.js
  • In your file start writing your functions
    yourFunction = function () { .... };
  • Dont forget to export your function at the end of the file.
    exports.default = yourFunction

Publishing:

  • Once your package is ready then you need to publish it to npm
  • To publish to npm you first need an account in npm
  • Then add your account to your machine

$ npm adduser

  • Give your username and password.
  • Now, ready to publish

$ npm publish

Yay!! your package is published and you are a open source contributor.

Javascript rocks!!!