0% found this document useful (0 votes)
40 views5 pages

Coding Guidelines NodeJS

NodeJS projects should be initialized with npm init to create a package.json. A .npmrc file should be used to ensure dependencies are saved exactly as installed. Environment variables should be used for configuration to avoid hardcoding values. Requires should be at the top of files and organized into sections. A coding style guide like lowerCamelCase and early returns should be followed for readability.

Uploaded by

Gaurav Sethi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
40 views5 pages

Coding Guidelines NodeJS

NodeJS projects should be initialized with npm init to create a package.json. A .npmrc file should be used to ensure dependencies are saved exactly as installed. Environment variables should be used for configuration to avoid hardcoding values. Requires should be at the top of files and organized into sections. A coding style guide like lowerCamelCase and early returns should be followed for readability.

Uploaded by

Gaurav Sethi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 5

NodeJS Coding Guidelines

1. Start all projects with npm init –

Recommend creating a new project using npm init, like so:

$ mkdir my-project
$ cd my-project
$ npm init

This will create a new package.json for you which allows you to add a


bunch of metadata to help others working on the project have the same
setup as you.

2. Setup .npmrc –

If you’ve used npm before, you may have come across the - -save flag
which updates the package.json with the dependency. When other
developers clone the project, they can be sure to have the right
dependencies because of this. Unfortunately, remembering to add the
flag can be a problem.

In addition, NPM adds a leading caret ^ to all versions. Consequently,


when someone runs npm install, they may get different versions of the
modules than what you have. While updating modules is always a good
practice, having a team of developers all running against slightly different
versions of dependencies can lead to differences in behavior or availability
of APIs.

Therefore, it’s a good idea to have everyone on the same version. To


make this easier for everyone, the .npmrc file has some useful properties
that can make sure npm install always updates the package.json and
enforces the version of installed dependency to be an exact match.
Simply run the following lines in your terminal:

$ npm config set save=true


$ npm config set save-exact=true

Now when you run npm install, you can be sure the dependency is saved
and will be locked down to the version you installed.

3. Use environment variables –

Configuration management is always a big topic in any language. How do


you decouple your code from the databases, services, etc. that it has to use
during development, QA, and production?
The recommended way in Node.js is to use environment variables and to
look up the values from process.env in your code.

4. ‘Requires’ at top –

Always put requires at top of file to clearly illustrate a file’s dependencies.


Besides giving an overview for others at a quick glance of dependencies and
possible memory impact. You should always load all your dependencies
upfront and configure them upfront. That way, you will know from the
startup if there is a problem, not three to four hours after your app has
gone live in production!

 Organize your node requires in the following order:

core modules
npm modules
others

5. Use a style guide –

Pick an existing set of guidelines for coding in Node.js and adhere to it to


maintain code readability across the project chain.
Formatting

1. 4 spaces for indentation - Use 4 spaces for indenting your code or a Tab
2. Newlines - Use UNIX-style newlines (\n), and a newline character as the
last character of a file.
Windows-style newlines (\r\n) are forbidden inside any repository.
3. No trailing white space – Just like you brush your teeth after every meal,
you clean up any trailing white space in your .js files before committing.
Otherwise the rotten smell of careless neglect will eventually drive away
contributors and/or co-workers.
4. 80 characters per line - Limit your lines to 80 characters.
5. Use single quotes - Use single quotes unless you are writing JSON. This
helps you separate your objects’ strings from normal strings.
6. Opening braces go on the same line - Your opening braces go on the
same line as the statement.
if (true) {
console.log(‘winning’);
}

Naming Conventions

1. Use lowerCamelCase for variables, properties and function names -


Variables, properties and function names should use lowerCamelCase. They
should also be descriptive. Single character variables and uncommon
abbreviations should generally be avoided.
2. Use UpperCamelCase for class names - Class names should be
capitalised using UpperCamelCase.
3. Use UPPERCASE for Constants - Constants should be declared as regular
variables or static class properties, using all uppercase letters.

Functions

1. Write small functions - Keep your functions short. A good function fits on
a slide that the people in the last row of a big room can comfortably read.
2. Return early from functions - To avoid deep nesting of if-statements,
always return a function’s value as early as possible.

Right:

function isPercentage(val) {
if (val < 0) {
return false;
}
if (val > 100) {
return false;
}
return true;
}

Wrong:

function isPercentage(val) {
if (val >= 0) {
if (val < 100) {
return true;
} else {
return false;
}
} else {
return false;
}
}
3. Method chaining - One method per line should be used if you want to
chain methods. You should also indent these methods so it’s easier to tell
they are part of the same chain.

Logging
Please clean up logs when they are no longer helpful. In particular, logging
the same object over and over again is not helpful. Logs should report what
is happening so that it’s easier to track down where a fault occurs. Use
appropriate log levels.

You might also like