Over time, I learned a few useful facts while working with NodeJS that made my life a lot easier at the beginning stage. I tried to compile them into a list on this post. As a NodeJS developer, you may find at least some of these NodeJS tips helpful as well.
Using Strict Mode In Coding:
It won’t result in any errors. But in the case of using ‘use strict’, it will throw an error like the below:
Use Node shell:
If you are trying to test a NodeJS function on how it works, the NodeJS shell works a lot better for that. Just run ‘node,’ and it will bring you up the node shell. write code just like in a text editor, and it will work fine. However, when you are done exiting the node shell, it doesn’t have support for ‘exit’/’quit’ or similar commands, just like other CLI tools. You must write a valid NodeJS statement that tells you to exit the process. Here is the command you will need for that.
$process.exit(0)Code language: PHP (php)
On my first day of NodeJS/shell using, I had no idea how to exit the shell and had to google to figure out this way.
Using global variable As Cache:
If you are creating a comparatively small app where it will be an overhead to integrate a third-party caching mechanism, you can handle those right inside your NodeJS app, given that you are running it in a default single-threaded mode.
As being single-threaded, only one instance of Nodejs application is running(just like a desktop app) to process all requests, unlike other web programming languages PHP/ruby/python etc based apps. Declaring a global variable there persists and is accessible to all requests. So, you should always be careful about that too.
To achieve such a mechanism, create a global variable of necessary types(JSON/array) and inside your function, add necessary manipulation mechanism for that variable. it will persist across all requests.
Easy Logging Without any third-party library:
We usually use the ‘console.log’ method for printing temporary info while development and some good logging libraries for logging
in the deployment stage. If you are lazy enough and trying to avoid third-party logging libraries, there’s good news for you. You can still get a decent logging system on your NodeJS app with native support.
The ‘console’ object in NodeJS has several different levels for logging(error/info/warn etc), which you can check more here on the console object’s official documentation page. Use them appropriately as needed. Then, while running the NodeJS app, refer to a log file where you want everything written as below:
$node app.js > mylog.logCode language: PHP (php)
And you are set. All information will be logged in the log file, which can be checked later for useful information.
One more thing about the console object, we usually use the following format to print something to the terminal window:
It works for the single-valued item. For JSON/objects, we can use ‘console.log(abc)’ , but it won’t work with ‘+’ operator. Here we can use ‘,’ separator to print different values:
This may look obvious to some of you. But interestingly, I didn’t know or even try this for my first few days of NodeJS. So, other beginners might find this useful.
Avoid using “*” in production dependency:
In your package.json file, you can specify a version to use or keep ‘*’ to have the latest version always. It’s a good way to have all of your packages updated to their latest version, which might have several improvements, bug fixes, and new features included. But this makes sense as long as you are working only in development mode and the product isn’t in production yet. You can fix it if any compatibility occurs.
However, in a production environment, it’s very, very risky to have such a notation for dependencies. You never know which package gonna break when, and it could cause crashing your app too! So, use it in the earlier stage of development, but switch to a specific version-based dependency as soon as the product becomes in a major deployment stage.
We get useful support for asynchronous control flow/looping with underscoreJS/AsyncJS. However, it’s not that hard to have it natively as long as you understand the callback concepts well. So, in case you don’t want to use such libraries for a simple task, you can use the following mechanism:
This is not a completed post. Rather, it supposes to be an ongoing list of small tips for beginners. If you have something to add, write it in the comments! Happy coding 🙂