Skip to content

Instantly share code, notes, and snippets.

@canavci2016
Last active September 8, 2022 14:39
Show Gist options
  • Save canavci2016/69c90fbc0660e3384a4b4e7f1f4d5e6a to your computer and use it in GitHub Desktop.
Save canavci2016/69c90fbc0660e3384a4b4e7f1f4d5e6a to your computer and use it in GitHub Desktop.
nodejs style guide

Not: it is quoted from google javascript guide document

https://google.github.io/styleguide/jsguide.html

File name

File names must be all lowercase and may include underscores (_) or dashes (-), but no additional punctuation. Follow the convention that your project uses. Filenames’ extension must be .js. example: user-relations.js

Variable Names

Classes, enums, functions, constants, and other symbols are exported using the exports object. Exported symbols may be defined directly on the exports object, or else declared locally and exported separately. Symbols are only exported if they are meant to be used outside the module. Non-exported module-local symbols are not declared @private nor do their names end with an underscore. There is no prescribed ordering for exported and module-local symbols.

const /** !Array<number> */ exportedArray = [1, 2, 3];

const /** !Array<number> */ moduleLocalArray = [4, 5, 6];

/** @return {number} */
function moduleLocalFunction() {
  return moduleLocalArray.length;
}

/** @return {number} */
function exportedFunction() {
  return moduleLocalFunction() * 2;
}

exports = {exportedArray, exportedFunction};

Import paths

ES module files must use the import statement to import other ES module files.

import './sideeffects.js';
import * as goog from '../closure/goog/goog.js';
import * as parent from '../parent.js';
import {name} from './sibling.js';

Naming module imports

Module import names (import * as name) are lowerCamelCase names that are derived from the imported file name.

import * as fileOne from '../file-one.js';
import * as fileTwo from '../file_two.js';
import * as fileThree from '../filethree.js';
import * as libString from './lib/string.js';
import * as math from './math/math.js';
import * as vectorMath from './vector/math.js';

Naming default imports

Default import names are derived from the imported file name and follow the rules in 6.2 Rules by identifier type.

import MyClass from '../my-class.js';  // for class
import myFunction from '../my_function.js';  // for function 
import SOME_CONSTANT from '../someconstant.js'; // constants

Named vs default exports

// Do not use default exports:
export default class Foo { ... } // BAD!

// Use named exports:
export class Foo { ... }
// Alternate style named exports:
class Foo { ... }

export {Foo};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment