Node.js Redux – Updating my Node.js Skills

It has been a while since I originally posted about node.js. We’re getting into API platforms at work so it is time for me to update my node.js skills and see what is new in the past year. In this post I’ll talk about Node.js on OS-X El Capitan using the various IDEs and training sites that I think are useful. I’ll also use this post as a place to keep track of tips and tricks I’ve picked up.

UPDATED: 10/22/2016: Updated a few bits on installing, updating node/npm, and IDE’s.

Getting Everything Installed

Just ignore everything on the internet and use homebrew. On El Capitan you’ll need to fix the newly introduced security glitches, check my OS-X page if you have trouble with those. Once you have node.js installed then you can use npm to bring in all the add-ons you like. As I write this (see the update date above)  I am looking at node.js 6.5.0 and npm 3.10.3.

Steps:

  1. brew update
  2. brew doctor
  3. brew install node
  4. To really be sure things are working install a package like gulp:
    npm install -g gulp (you might need to use sudo when installing globally on OS-X)
  5. To get everything update to the latest:
    npm update
    npm upgrade -g

With all that you should be good to go – try “node –version” (two dashes) to make sure. On OSX global modules are installed under /users/your username/npm.

Node.js Development Tools

Last time I looked at Cloud9  and it is still is a nice tool for a node IDE however there are several others to look at. Sublime Text 2 is out and Brackets from Adobe is pretty nice. Sublime Text 2 costs $70, although you can evaluate forever (not recommended). PHPStorm from Jetbrains is decent as well, but you need to pay a bit more. It is great to have so many decent choices. I’m going to use a combo of PHPStorm and Brackets I guess because I use PHPStorm all the time, but I’m curious about Brackets and its features.

PHPStorm’s support for node is kinda minimal, but it does syntax highlighting and it does have an internal

Open up the settings and enable Node support for your project.

Open up the settings and enable Node support for your project.

console that you can use to start and stop node. If you are already using it for other stuff then it makes sense to use it for node. There’s a node plugin that brings some support for common features (code help, syntax highlighting, and debugging). Once you install the plugin you will need to enable it in the project settings – see the screenshot.

Node.js has lots of tools to help out building. Mocha.org is a unit test execution tool and Istanbul provides coverage reports.

Node.js Training and On-line Resources

These are some of the sites I’ve been looking thru. I do have a Lynda account and the node courses are ok, but not great. In general the on-line stuff isn’t too great. Its basic and then you are on your own so be sure to have some solid ideas about what you want to do so that you can get going right away:

  • Nodeschool.io: Tutorials and workshops are pretty good
  • NodeBeginner: Getting a little dated, but still good.
  • Lynda.com: good stuff, but you need an account
  • HowToNode: This is a great site with lots of interesting articles once you have the basics.

Interesting Node.js Packages and Frameworks

There’s been a lot going on. In my last look I really liked Express and it is definitely still the most mature framework. Sails looks interesting. It adds on top of Express and brings some very cool features for APIs and data persistence. Koa also looks interesting and is positioned as a “next generation” Express framework.

To find new packages or explore them go to the NPM Packages site.

When using npm to install packages into your project, you should “npm init” your project to create a package.json file. Then don’t forget the “–save” parameter to have the package automatically added to your package.json file:

npm install –save express

Updates the dependencies list:

{
  "name": "wp-api",
  "version": "1.0.0",
  "description": "Creating a node API for wordpress",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "Beren Erchamion",
  "license": "LGPL",
  "dependencies": {
    "express": "^4.13.3"
  }
}

NPM does more than just manage packages. Like Gradle or Maven, NPM can also be used to build your project, execute unit tests, and more. Inside your project the package.json file is the main configuration file and is equivalent to .gradle files with Gradle or the pom.xml file with Maven.

Other Useful facts/tips when using npm:

  • On OS-X npm installs global packages in ~/npm/lib.
  • If you think something should work after a package is installed, check the package’s readme.
  • Use npm –prune to remove things you don’t need in your project anymore.

Unit Testing, Coverage, SonarQube

Good programmers are testers too. I always want to know my code works so unit testing is key for that. Node.js has really good testing tools (IMO because its been flaky for so long coders really needed them). There are a bunch of testing frameworks out there, but I like Mocha. Its simple and it can produce output in a number of formats, including XUnit. Its easy to run too. You can add a line to the scripts section in the packages.json file for your project like this:

"test": "mocha -r tests/testhelper.js -R xunit tests/**/*.test.js"

Now you can execute all your tests with “npm test” on the command line. To get coverage information you can use Istanbul. It can also be run by adding a line to your packages.json file:

"coverage": "/Users/username/npm/lib/node_modules/istanbul/lib/cli.js 
     cover /Users/username/npm/bin/_mocha -- -r tests/testhelper.js -R xunit tests/**/*.test.js"

That’s a bit more complicated. Basically what’s going on is we are using the Istanbul command line client to run mocha (note the underscore) client that will not fork processes, and pass everything after the double dash to the test process. Complicated! Then you can run the coverage with “npm coverage” from the command line.

Express Notes and Tips:

  • Install Express with:
    sudo npm install -g express
  • Install the generator functions with:
    sudo npm install -g express-generator@4
  • Create an app with:
    express myApp
  • Start the app with:
    npm start or node app

Node.js and Object Oriented Programming

I’ve always been a big OOP guy – C++, Java, and yes PHP. Javascript has always been a bit twitchy with OOP, perhaps because it is the only “classless” OO language that I’ve worked with. While there are a number of OOP Javascript guides, there’s not too much on how to use OOP with Node.js. Not sure why. Here’s some good articles I’ve been reading through:

Async, Generators, and Fibers

This is a very interesting topic. In my slightly inexperienced view, the async model works very well for small and specific jobs, but starts to show problems with larger projects. Generator and Fibers are meant to ease this issue and bring a blend of synch and async to node projects. I’m still reading about these, but in general my use of node will be smaller things initially.

Working with Amazon DynamoDB

Eventually you will need a database for a Node.js project. There are a ton of them, but I’ve found DynamoDB to be pretty good. You can either use it online via an Amazon Web Service (AWS) account, or download it and use it yourself. Running it locally is pretty simple. You do need to have Java installed (check my Java tips if you need help with that) and then its a simple matter of starting it from the command line:

java -Djava.library.path=./DynamoDBLocal_lib -jar DynamoDBLocal.jar

 

1 Response

Leave a Reply