10 Tips to Become a Better Node Developer in 2017 is by guest author Azat Mardan. SitePoint guest posts aim to bring you engaging content from prominent writers and speakers of the Web community.
I started working with Node full-time in 2012 when I joined Storify. Since then, I have never looked back or felt that I missed Python, Ruby, Java or PHP — languages with which I had worked during my previous decade of web development.
In this post I’ll outline ten tips to help you become a better Node developer in 2017. These tips come from me, who saw and learned them in the trenches, as well as people who have written the most popular Node and npm modules. Here’s what we’ll be covering:
- Avoid complexity — Organize your code into the smallest chunks possible until they look too small and then make them even smaller.
- Use asynchronous code — Avoid synchronous code like the plague.
- Avoid blocking require — Put ALL your require statements at the top of the file because they are synchronous and will block the execution.
- Know that require is cached — This could be a feature or a bug in your code.
- Always check for errors — Errors are not footballs. Never throw errors and never skip the error check.
- Use try…catch only in sync code — try…catch is useless for async code, plus V8 can’t optimize code in try…catch as well as plain code.
- Return callbacks or use if … else — Just to be sure, return a callback to prevent execution from continuing.
- Listen to the error events — Almost all Node classes/objects extend the event emitter (observer pattern) and emit the error event. Be sure to listen to that.
- Know your npm — Install modules with -S or -D instead of –save or –save-dev
- Use exact versions in package.json: npm stupidly adds a caret by default when you use -S, so get rid of them manually to lock the versions. Never trust semver in your apps, but do so in open-source modules.
- Bonus — Use different dependencies. Put things your project needs only in development in devDependencies and then use npm i –production. The more un-required dependencies you have, the greater the risk of vulnerability.
So let’s bisect and take a look at each one of them individually. Shall we?
This article and image was adapted from sitepoint.com
By Azat Mardan