exports
.
geometricSum
=
function
(
a
,
x
,
n
) {
if
(
x
===
1
)
return
a
*
n
;
return
a
*
(
1
-
Math
.
pow
(
x
,
n
))
/
(
1
-
x
);
};
exports
.
arithmeticSum
=
function
(
n
) {
return
(
n
+
1
)
*
n
/
2
;
};
exports
.
quadraticFormula
=
function
(
a
,
b
,
c
) {
const
D
=
Math
.
sqrt
(
b
*
b
-
4
*
a
*
c
);
return
[(
-
b
+
D
)
/
(
2
*
a
), (
-
b
-
D
)
/
(
2
*
a
)];
};
The exports shorthand only works for exporting objects; if you
want to export a function or some other value, you must use
module.exports
. Furthermore, you can’t meaningfully mix the
two: use one or the other.
Core Modules, File Modules, and npm Modules
Modules fall into three categories, core modules, file modules, and npm modules. Core
modules are reserved module names that are provided by Node itself, such as
fs
and
os
(which we’ll discuss later in this chapter). File modules we’ve already seen: we cre‐
ate a file that assigns to
module.exports
, and then require that file. npm modules are
just file modules that are located in a special directory called node_modules. When
you use the
require
function, Node determines the type of module (listed in
) from the string you pass in.
Table 20-1. Module types
Type
String passed to require
Examples
Core
Doesn’t start with
/
,
./
,
or
../
require
(
'fs'
)
require
(
'os'
)
require
(
'http'
)
require
(
'child_process'
)
File
Starts with
/
,
./
, or
../
require
(
'./debug.js'
)
require
(
'/full/path/to/module.js'
)
require
(
'../a.js'
)
require
(
'../../a.js'
)
npm
Not a core module and doesn’t
start with
/
,
./
, or
../
require
(
'debug'
)
require
(
'express'
)
require
(
'chalk'
)
require
(
'koa'
)
require
(
'q'
)
284 | Chapter 20: Node