NPM-CODING-STYLENPM ' s "funny" coding styledescription
NPM ' s coding style is a bit unconventional. It isn't different for difference's sake, but rather a carefully crafted style that's designed to reduce visual clutter And make bugs more apparent.
If you want to contribute to NPM (which are very encouraged), you should make your code conform to NPM ' s style.
Note:this concerns NPM ' s code not the specific packages so can download from the NPM registry.
Line Length
Keep lines shorter than characters. It ' s better for lines to being too short than to being too long. Break up long lists, objects, and other statements onto multiple lines.
Indentation
Two-spaces. Tabs is better, but the They look like hell in the Web browsers (and on GitHub), and node uses 2 spaces, so that's that.
Configure your editor appropriately.
Curly Braces
Curly braces belong on the same line as the thing that necessitates them.
Bad:
function (){
Good:
function () {
If a block needs to wrap to the next line, use a curly brace. Don ' t use it if it doesn ' t.
Bad:
if (foo) { bar() }while (foo) bar()
Good:
if (foo) bar()while (foo) { bar()}
Semicolons
Don ' t use them except on four situations:
for (;;)Loops. They ' re actually required.
- Null loops like:
while (something) ; (if you ' d better has a good reason for doing.)
case ‘foo‘: doSomething(); break
- In front of a leading or at the start of the line
( [ . This prevents the expression from being interpreted as a function call or property access, respectively.
Some Examples of good semicolon usage:
;(x || y).doSomething();[a, b, c].forEach(doSomething)for (var i = 0; i < 10; i ++) { switch (state) { case ‘begin‘: start(); continue case ‘end‘: finish(); break default: throw new Error(‘unknown state‘) } end()}
Note that the starting lines - + with and also should are prefixed with a semicolon, but this is the much less common.
Comma First
If There is a list of things separated by commas, and it wraps across multiple lines, put the comma at the start of the NE XT line, directly below the token that starts the list. Put The final token in the list on a line by itself. For example:
var magicWords = [ ‘abracadabra‘ , ‘gesundheit‘ , ‘ventrilo‘ ] , spells = { ‘fireball‘ : function () { setOnFire() } , ‘water‘ : function () { putOut() } } , a = 1 , b = ‘abc‘ , etc , somethingElse
Quotes
Use the single quotes for strings except to avoid escaping.
Bad:
var notOk = "Just double quotes"
Good:
var ok = ‘String contains "double" quotes‘var alsoOk = "String contains ‘single‘ quotes or apostrophe"
Whitespace
Put a single space in front of-anything other than a function call. Also use a single space wherever it makes things more readable.
Don ' t leave trailing whitespace at the end of the lines. Don ' t indent empty lines. Don ' t use more spaces than is helpful.
Functions
Use named functions. They make stacks traces a lot easier to read.
Callbacks, Sync/async Style
Use the asynchronous/non-blocking versions of things as much as possible. It might make the synchronous FS APIs, the FS and HTTP and child process stuff all Uses the same callback-passing methodology.
The callback should is the last argument in the list. Its first argument is the Error or null.
Be very careful never to ever ever throw anything. It ' s worse than useless. Just Send the error message back as the first argument to the callback.
Errors
Always create a new Error object with your message. Don ' t just return a string message to the callback. Stack traces is handy.
Logging
Logging is done using the Npmlog utility.
Logs when they is no longer helpful. In particular, logging the same object over and over again are not helpful. Logs should report, what's happening so, it's easier to track down where a fault occurs.
Use appropriate log levels. See and npm-config search for "loglevel".
case, naming, etc.
Use of lowerCamelCase Multiword identifiers when they refer to objects, functions, methods, properties, or anything not specified In the This section.
Use UpperCamelCase the For class names (things, "D pass to" new ").
Use the for all-lower-hyphen-css-case multiword filenames and config keys.
Use named functions. They make stacks traces easier to follow.
Use CAPS_SNAKE_CASE for constants, things this should never change and is rarely used.
Use a single uppercase letter for function names where the function would normally is anonymous, but needs to call itself recursively. It makes it clear that it ' s a "throwaway" function.
null, undefined, false, 0
Boolean variables and functions should always be either true or false . Don ' t set it to 0 unless it's supposed to be a number.
When something are intentionally missing or removed, set it to null .
Don ' t set things to undefined . Reserve that value to mean ' not yet set to anything. '
Boolean objects is verboten.
See Also
- Npm-developers
- Npm-faq
- Npm
Code specification for node. js with ' Npm-coding-style '