2

A simple question: I do not understand if a standard exists for variable names to be used left of require()'s, and if it does, what does it require: Uppercase or lowercase (camelcase) variable names?

To be clearer:

myImage = require('./image');

or

MyImage = require('./image');

?

I ask since required modules always return objects, so they can be used as Classes or Objects...

sdgluck
  • 24,894
  • 8
  • 75
  • 90
MarcoS
  • 17,323
  • 24
  • 96
  • 174
  • 1
    NodeJS is server sided *javascript*, so following standard JS practices (camelCase) would be the "norm" for things like variable naming conventions. – Sterling Archer Sep 29 '15 at 15:10

1 Answers1

5

There is no requirement for the name of any variable in JavaScript except the rules defined in this answer. Some JavaScript frameworks have naming conventions, but these do not borrow from any native JavaScript behaviour.

However, usually you will see JS code with classes having capitalised pascal-case names (MyClass) and everything else is camel-case (anObject). I suggest that you abide this general standard in your own code so that it is easier for others to reason with.

This means that when you require you should employ the above naming convention according to what it is that you are requireing:

// CommonJS
let MyClass = require('./MyClass')
let utilMethod  = require('./MyClass/utilMethod')

// ES6
import MyClass, { utilMethod } from 'MyClass';
sdgluck
  • 24,894
  • 8
  • 75
  • 90
  • I'm speaking about *Node.js*... And asking for *conventions*... I know javascript syntax does not require any specific case... – MarcoS Sep 29 '15 at 15:11
  • @MarcoS no language "requires" specific case, but seeing as NodeJS is still JS, it is best to adhere to standard JavaScript naming conventions. I could easily use snake_case in JS, or camelCase in python and it will still work, but it's not language convention. – Sterling Archer Sep 29 '15 at 15:12
  • `pascal-case` names for classes and `camel-case` for objects you are speaking of in your answer: these are the conventions I'm speaking about... My question could be rephrased as "Should I consider the return value from a require as a Class, or as an Object, or it depends by the module?" – MarcoS Sep 29 '15 at 15:18
  • Depends on the module. could be a class, a function, an object, array, string, or even undefined. – Kevin B Sep 29 '15 at 15:19
  • @MarcoS Yes, it depends on the module. – sdgluck Sep 29 '15 at 15:19