Node.js Coding Standards – Const Variables in SCREAMING_SNAKE_CASE or camelCase

clean codecoding-standardscoding-stylenode.js

TL/DR: When requiring another script in Node.js and defining it as a const should the variable name still be in camelCase like it was usual with var or should it instead be in SCREAMING_SNAKE_CASE as is more usual for a const?

const someVariable = require('...js');

vs

const SOME_VARIABLE = require('...js');

Longer version: In Node.js another script is usually imported using

var someScript = require('path/to/someScript.js');

With ES6 being available in Node.js such a var could instead be declared as a const (or in some cases as a let if it's ever redefined).

Now as we're working on defining a style guide for our development we came to the conclusion in other languages like Typescript (Angular 2) that whenever we declare a const we write the variable name in SCREAMING_SNAKE_CASE like

const SOME_CONSTANT_VARIABLE = '123';

Now we would conclude that this means that in Node.js we should define our script variables like

const SOME_SCRIPT = require('path/to/someScript.js');

As you're usually using those script variables a lot in Node.js code can become quite difficult to read. Also I couldn't see a lot of examples of other people doing it similarly and instead often use it like

const someScript = ...;

so still writing the variable name in camelCase.

My question is as a What is the preferred / best practice way of writing those variable names using ES6 const?

Best Answer

You are not really declaring a variable, but what other languages call an alias.

Example in Python:

import numpy as np

Example in C#:

using Project = PC.MyCompany.Project;

For your case, from the official node.js module API documentation:

const square = require('./square.js');
const mySquare = square(2);
console.log(`The area of my square is ${mySquare.area()}`);

Or

console.log('main starting');
const a = require('./a.js');
const b = require('./b.js');
console.log('in main, a.done=%j, b.done=%j', a.done, b.done);

Different languages use different conventions. The official node.js API documentation uses camelCase, you should use camelCase.

Related Topic