Like any kind of apps, JavaScript apps also have to be written well.
Otherwise, we run into all kinds of issues later on.
In this article, we’ll look at some best practices we should follow when writing Node apps.
Stick with Lowercase
We should stick with lower case file names to make the files portable.
Since Node is cross-platform, we may run into issues if we have names with different cases.
To make our lives easier, we should just use lower case names everywhere.
Cluster Our App
To make our app run with all the cores available, we should cluster our app.
One instance of our app can only use one CPU core and 1.5GB of memory.
Therefore, lots of resources may be unused if we don’t cluster them.
To go beyond the limit, we should cluster our app.
To do this, we can use forky and throng to create our clusters.
Be Environmentally Aware
We should make our app configurable so that it runs in any environment.
This way, we read all the configuration into our file.
For example, we can write:
DATABASE_URL='postgres://localhost/foobar'
HTTP_TIMEOUT=10000
in our .env
file.
Then we can use some library like dotenv to read the file.
Avoid Garbage
Node will wait for the memory limit to be hit before it reclaims the unused memory.
Yo change this, we can set some settings when we run our app to reclaim the memory faster.
For example, we can run it by typing in:
node --optimize_for_size --max_old_space_size=920 --gc_interval=100 server.js
The optimize_for_size
option will reclaim the memory faster.
It’ll make Node reduce memory size but compromise on speed since the unused memory is reclaimed faster.
gc-interval
sets the garbage collection interval to 100ms.
max_old_space_size
limits the total size of the objects in the heap.
Hooks
We can add custom hooks by adding lifecycle scripts.
To do that, we add all the scripts
section of package.json
to add the scripts.
For example, we can write:
"scripts": {
"build": "bower install && grunt build",
"start": "grunt"
}
to add 2 scripts, build
and start
.
Then we can run npm run build
and npm start
to run the scripts.
To give us more control over the scripts, we can run an external script file:
"build": "scripts/build.sh"
Track Only the Important Bits
We should only track the code files we created.
The rest should be in .gitignore
so that they’re ignored.
node_modules
should be ignored so that we install them with npm install
instead of committing lots of package into our project repository.
This will reduce the size of our project a lot and we can do any operation like checkout, branching, etc. much faster.
If we checked in the node_modules
folder, we should remove the folder by running:
echo 'node_modules' >> .gitignore
git rm -r --cached node_modules
git commit -am 'ignore node_modules'
We put node_modules
into .gitignore
with the first line.
Then we remove node_modules
from tracking with the 2nd line.
And then we make a new commit with the change in the 3rd line.
We can ignore the npm-debug.log
file the same way:
echo 'npm-debug.log' >> .gitignore
git commit -am 'ignore npm-debug'
Conclusion
There are a few things we can do with our Node project to improve it.
We can make garbage collection more frequent and we should ignore automatically produced files and folders.