Modules

Node Imports

Import Node built-in modules or locally installed third-part module:

// These modules are built in to Node
const fs = require("fs");     // The built-in filesystem module
const http = require("http"); // The built-in HTTP module

// The Express HTTP server framework is a third-party module.
// It is not part of Node but has been installed locally.
const express = require("express");

Import your own code:

const stats = require('./stats.js');
const BitSet = require('./utils/bitset.js');

You can either import everything:

// Import the entire stats object, with all of its functions
const stats = require('./stats.js');

// We've got more functions than we need, but they're neatly
// organized into a convenient "stats" namespace.
let average = stats.mean(data);

Or import only a function:

// Alternatively, we can use idiomatic destructuring assignment to import
// exactly the functions we want directly into the local namespace:
const { stddev } = require('./stats.js');

// This is nice and succinct, though we lose a bit of context
// without the 'stats' prefix as a namespace  for the stddev() function.\
let sd = stddev(data);

Last updated