Package Manager
npm
yarn
pnpm
1
pnpm exec jest
Semver
Semantic Versioning
MAJOR
.MINOR
.PATCH
-PRE-RELEASE
MAJOR
- incompatible API changesMINOR
- add/change functionality in a backwards compatible mannerPATCH
- backwards compatible bug fixesPRE-RELEASE
- a series of dot separated identifiers ([0-9A-Za-z-]
)
Tilde Ranges
- Allows patch-level changes if a minor version is specified.
- Allows minor-level changes if not.
~1.2.3
: >=1.2.3
and <1.3.0
~1
: 1.x
Caret Ranges
Allows changes that do not modify the left-most non-zero element in the [major, minor, patch]
tuple.
^1.2.3
:=>=1.2.3
and<2.0.0
^0.2.3
:=>=0.2.3
and<0.3.0
^0.0.3
:= >=0.0.3
and<0.0.4
Spec
Major version zero (0.y.z) is for initial development. Anything MAY change at any time. The public API SHOULD NOT be considered stable.
Patch version MUST be reset to 0 when minor version is incremented.
Patch and minor versions MUST be reset to 0 when major version is incremented.
Precedence
When major, minor, and patch are equal, a pre-release version has lower precedence than a normal version:
Example: 1.0.0-alpha
< 1.0.0
.
ES6 import
node >= 13
save file as .mjs
Add { "type": "module" }
in the package.json
Process
cwd()
- the current working directory
child_process
The child_process
module provides the ability to spawn subprocesses in a manner that is similar, but not identical, to popen(3)
. This capability is primarily provided by the child_process.spawn()
function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
const { spawn } = require('child_process');
const ls = spawn('ls', ['-lh', '/usr']);
ls.stdout.on('data', (data) => {
console.log(`stdout: ${data}`);
});
ls.stderr.on('data', (data) => {
console.error(`stderr: ${data}`);
});
ls.on('close', (code) => {
console.log(`child process exited with code ${code}`);
});
Readline
1
2
3
4
5
6
7
8
9
10
11
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question('What do you think of Node.js? ', (answer) => {
console.log(`Thank you for your valuable feedback: ${answer}`);
rl.close();
});
Util
util.deprecate()
- wraps fn in such a way that it is marked as deprecated.
fs
fs-extra
fs-extra
adds file system methods that aren’t included in the native fs
module and adds promise support to the fs
methods. It also uses graceful-fs
to prevent EMFILE
errors. It should be a drop in replacement for fs
.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// ES6 version using asynchronous iterators, compatible with node v10.0+
const fs = require("fs");
const path = require("path");
async function* walk(dir) {
for await (const d of await fs.promises.opendir(dir)) {
const entry = path.join(dir, d.name);
if (d.isDirectory()) yield* walk(entry);
else if (d.isFile()) yield entry;
}
}
// Then, use it with a simple async for loop
async function main() {
for await (const p of walk('/tmp/'))
console.log(p)
}