diff --git a/doc/api/buffer.md b/doc/api/buffer.md index 0d98a2f1c1b5c4..5de805b6e9831e 100644 --- a/doc/api/buffer.md +++ b/doc/api/buffer.md @@ -2609,7 +2609,7 @@ sensitive data. Use [`buf.fill(0)`][`buf.fill()`] to initialize a `SlowBuffer` t Example: ```js -const SlowBuffer = require('buffer').SlowBuffer; +const { SlowBuffer } = require('buffer'); const buf = new SlowBuffer(5); diff --git a/doc/api/child_process.md b/doc/api/child_process.md index 4800d5bc9302a9..3d73f0b859cab5 100644 --- a/doc/api/child_process.md +++ b/doc/api/child_process.md @@ -7,7 +7,7 @@ a manner that is similar, but not identical, to popen(3). This capability is primarily provided by the [`child_process.spawn()`][] function: ```js -const spawn = require('child_process').spawn; +const { spawn } = require('child_process'); const ls = spawn('ls', ['-lh', '/usr']); ls.stdout.on('data', (data) => { @@ -87,7 +87,7 @@ spaces it needs to be quoted. ```js // On Windows Only ... -const spawn = require('child_process').spawn; +const { spawn } = require('child_process'); const bat = spawn('cmd.exe', ['/c', 'my.bat']); bat.stdout.on('data', (data) => { @@ -105,7 +105,7 @@ bat.on('exit', (code) => { ```js // OR... -const exec = require('child_process').exec; +const { exec } = require('child_process'); exec('my.bat', (err, stdout, stderr) => { if (err) { console.error(err); @@ -168,7 +168,7 @@ containing shell metacharacters may be used to trigger arbitrary command execution. ```js -const exec = require('child_process').exec; +const { exec } = require('child_process'); exec('cat *.js bad_file | wc -l', (error, stdout, stderr) => { if (error) { console.error(`exec error: ${error}`); @@ -264,7 +264,7 @@ The same options as [`child_process.exec()`][] are supported. Since a shell is n spawned, behaviors such as I/O redirection and file globbing are not supported. ```js -const execFile = require('child_process').execFile; +const { execFile } = require('child_process'); const child = execFile('node', ['--version'], (error, stdout, stderr) => { if (error) { throw error; @@ -410,7 +410,7 @@ Example of running `ls -lh /usr`, capturing `stdout`, `stderr`, and the exit code: ```js -const spawn = require('child_process').spawn; +const { spawn } = require('child_process'); const ls = spawn('ls', ['-lh', '/usr']); ls.stdout.on('data', (data) => { @@ -430,7 +430,7 @@ ls.on('close', (code) => { Example: A very elaborate way to run `ps ax | grep ssh` ```js -const spawn = require('child_process').spawn; +const { spawn } = require('child_process'); const ps = spawn('ps', ['ax']); const grep = spawn('grep', ['ssh']); @@ -468,7 +468,7 @@ grep.on('close', (code) => { Example of checking for failed exec: ```js -const spawn = require('child_process').spawn; +const { spawn } = require('child_process'); const child = spawn('bad_command'); child.on('error', (err) => { @@ -515,7 +515,7 @@ Example of a long-running process, by detaching and also ignoring its parent `stdio` file descriptors, in order to ignore the parent's termination: ```js -const spawn = require('child_process').spawn; +const { spawn } = require('child_process'); const child = spawn(process.argv[0], ['child_program.js'], { detached: true, @@ -529,7 +529,7 @@ Alternatively one can redirect the child process' output into files: ```js const fs = require('fs'); -const spawn = require('child_process').spawn; +const { spawn } = require('child_process'); const out = fs.openSync('./out.log', 'a'); const err = fs.openSync('./out.log', 'a'); @@ -601,7 +601,7 @@ pipes between the parent and child. The value is one of the following: Example: ```js -const spawn = require('child_process').spawn; +const { spawn } = require('child_process'); // Child will use parent's stdios spawn('prg', [], { stdio: 'inherit' }); @@ -933,7 +933,7 @@ is given, the process will be sent the `'SIGTERM'` signal. See signal(7) for a list of available signals. ```js -const spawn = require('child_process').spawn; +const { spawn } = require('child_process'); const grep = spawn('grep', ['ssh']); grep.on('close', (code, signal) => { @@ -963,7 +963,7 @@ as in this example: ```js 'use strict'; -const spawn = require('child_process').spawn; +const { spawn } = require('child_process'); const child = spawn( 'sh', @@ -994,7 +994,7 @@ Returns the process identifier (PID) of the child process. Example: ```js -const spawn = require('child_process').spawn; +const { spawn } = require('child_process'); const grep = spawn('grep', ['ssh']); console.log(`Spawned child pid: ${grep.pid}`); diff --git a/doc/api/console.md b/doc/api/console.md index 606e13e601800c..3411d3ce0afba2 100644 --- a/doc/api/console.md +++ b/doc/api/console.md @@ -65,14 +65,14 @@ changes: The `Console` class can be used to create a simple logger with configurable output streams and can be accessed using either `require('console').Console` -or `console.Console`: +or `console.Console` (or their destructured counterparts): ```js -const Console = require('console').Console; +const { Console } = require('console'); ``` ```js -const Console = console.Console; +const { Console } = console; ``` ### new Console(stdout[, stderr]) diff --git a/doc/api/repl.md b/doc/api/repl.md index 99bd638e52d010..0e456b71ea13c9 100644 --- a/doc/api/repl.md +++ b/doc/api/repl.md @@ -172,7 +172,7 @@ translation of text from one language to another: ```js const repl = require('repl'); -const Translator = require('translator').Translator; +const { Translator } = require('translator'); const myTranslator = new Translator('en', 'fr'); diff --git a/doc/api/stream.md b/doc/api/stream.md index 0f8420e3dad71b..d1aeff747df191 100644 --- a/doc/api/stream.md +++ b/doc/api/stream.md @@ -1023,7 +1023,7 @@ section for more information. // Pull off a header delimited by \n\n // use unshift() if we get too much // Call the callback with (error, header, stream) -const StringDecoder = require('string_decoder').StringDecoder; +const { StringDecoder } = require('string_decoder'); function parseHeader(stream, callback) { stream.on('error', callback); stream.on('readable', onReadable); @@ -1087,8 +1087,8 @@ libraries. For example: ```js -const OldReader = require('./old-api-module.js').OldReader; -const Readable = require('stream').Readable; +const { OldReader } = require('./old-api-module.js'); +const { Readable } = require('stream'); const oreader = new OldReader(); const myReader = new Readable().wrap(oreader); @@ -1170,7 +1170,7 @@ of the four basic stream classes (`stream.Writable`, `stream.Readable`, parent class constructor: ```js -const Writable = require('stream').Writable; +const { Writable } = require('stream'); class MyWritable extends Writable { constructor(options) { @@ -1264,7 +1264,7 @@ objects and passing appropriate methods as constructor options. For example: ```js -const Writable = require('stream').Writable; +const { Writable } = require('stream'); const myWritable = new Writable({ write(chunk, encoding, callback) { @@ -1307,7 +1307,7 @@ constructor and implement the `writable._write()` method. The For example: ```js -const Writable = require('stream').Writable; +const { Writable } = require('stream'); class MyWritable extends Writable { constructor(options) { @@ -1321,7 +1321,7 @@ class MyWritable extends Writable { Or, when using pre-ES6 style constructors: ```js -const Writable = require('stream').Writable; +const { Writable } = require('stream'); const util = require('util'); function MyWritable(options) { @@ -1335,7 +1335,7 @@ util.inherits(MyWritable, Writable); Or, using the Simplified Constructor approach: ```js -const Writable = require('stream').Writable; +const { Writable } = require('stream'); const myWritable = new Writable({ write(chunk, encoding, callback) { @@ -1449,7 +1449,7 @@ on how the stream is being used. Using the callback ensures consistent and predictable handling of errors. ```js -const Writable = require('stream').Writable; +const { Writable } = require('stream'); const myWritable = new Writable({ write(chunk, encoding, callback) { @@ -1470,7 +1470,7 @@ is not of any real particular usefulness, the example illustrates each of the required elements of a custom [Writable][] stream instance: ```js -const Writable = require('stream').Writable; +const { Writable } = require('stream'); class MyWritable extends Writable { constructor(options) { @@ -1514,7 +1514,7 @@ constructor and implement the `readable._read()` method. For example: ```js -const Readable = require('stream').Readable; +const { Readable } = require('stream'); class MyReadable extends Readable { constructor(options) { @@ -1528,7 +1528,7 @@ class MyReadable extends Readable { Or, when using pre-ES6 style constructors: ```js -const Readable = require('stream').Readable; +const { Readable } = require('stream'); const util = require('util'); function MyReadable(options) { @@ -1542,7 +1542,7 @@ util.inherits(MyReadable, Readable); Or, using the Simplified Constructor approach: ```js -const Readable = require('stream').Readable; +const { Readable } = require('stream'); const myReadable = new Readable({ read(size) { @@ -1661,7 +1661,7 @@ consistent and predictable handling of errors. ```js -const Readable = require('stream').Readable; +const { Readable } = require('stream'); const myReadable = new Readable({ read(size) { @@ -1682,7 +1682,7 @@ The following is a basic example of a Readable stream that emits the numerals from 1 to 1,000,000 in ascending order, and then ends. ```js -const Readable = require('stream').Readable; +const { Readable } = require('stream'); class Counter extends Readable { constructor(opt) { @@ -1739,7 +1739,7 @@ constructor and implement *both* the `readable._read()` and For example: ```js -const Duplex = require('stream').Duplex; +const { Duplex } = require('stream'); class MyDuplex extends Duplex { constructor(options) { @@ -1752,7 +1752,7 @@ class MyDuplex extends Duplex { Or, when using pre-ES6 style constructors: ```js -const Duplex = require('stream').Duplex; +const { Duplex } = require('stream'); const util = require('util'); function MyDuplex(options) { @@ -1766,7 +1766,7 @@ util.inherits(MyDuplex, Duplex); Or, using the Simplified Constructor approach: ```js -const Duplex = require('stream').Duplex; +const { Duplex } = require('stream'); const myDuplex = new Duplex({ read(size) { @@ -1789,7 +1789,7 @@ incoming written data via the [Writable][] interface that is read back out via the [Readable][] interface. ```js -const Duplex = require('stream').Duplex; +const { Duplex } = require('stream'); const kSource = Symbol('source'); class MyDuplex extends Duplex { @@ -1830,7 +1830,7 @@ that accepts JavaScript numbers that are converted to hexadecimal strings on the Readable side. ```js -const Transform = require('stream').Transform; +const { Transform } = require('stream'); // All Transform streams are also Duplex Streams const myTransform = new Transform({ @@ -1895,7 +1895,7 @@ the output on the Readable side is not consumed. For example: ```js -const Transform = require('stream').Transform; +const { Transform } = require('stream'); class MyTransform extends Transform { constructor(options) { @@ -1908,7 +1908,7 @@ class MyTransform extends Transform { Or, when using pre-ES6 style constructors: ```js -const Transform = require('stream').Transform; +const { Transform } = require('stream'); const util = require('util'); function MyTransform(options) { @@ -1922,7 +1922,7 @@ util.inherits(MyTransform, Transform); Or, using the Simplified Constructor approach: ```js -const Transform = require('stream').Transform; +const { Transform } = require('stream'); const myTransform = new Transform({ transform(chunk, encoding, callback) { diff --git a/doc/api/string_decoder.md b/doc/api/string_decoder.md index 6b94b6bc3fb896..5757ba6e2b3440 100644 --- a/doc/api/string_decoder.md +++ b/doc/api/string_decoder.md @@ -7,13 +7,13 @@ strings in a manner that preserves encoded multi-byte UTF-8 and UTF-16 characters. It can be accessed using: ```js -const StringDecoder = require('string_decoder').StringDecoder; +const { StringDecoder } = require('string_decoder'); ``` The following example shows the basic use of the `StringDecoder` class. ```js -const StringDecoder = require('string_decoder').StringDecoder; +const { StringDecoder } = require('string_decoder'); const decoder = new StringDecoder('utf8'); const cent = Buffer.from([0xC2, 0xA2]); @@ -32,7 +32,7 @@ In the following example, the three UTF-8 encoded bytes of the European Euro symbol (`€`) are written over three separate operations: ```js -const StringDecoder = require('string_decoder').StringDecoder; +const { StringDecoder } = require('string_decoder'); const decoder = new StringDecoder('utf8'); decoder.write(Buffer.from([0xE2])); diff --git a/doc/api/url.md b/doc/api/url.md index a74ca26e08f919..837e41e8ff9608 100644 --- a/doc/api/url.md +++ b/doc/api/url.md @@ -55,7 +55,7 @@ properties of a WHATWG `URL` object. Parsing the URL string using the WHATWG API: ```js -const URL = require('url').URL; +const { URL } = require('url'); const myURL = new URL('https://user:pass@sub.host.com:8080/p/a/t/h?query=string#hash'); ``` @@ -607,7 +607,7 @@ Alias for [`urlSearchParams[@@iterator]()`][`urlSearchParams@@iterator()`]. Iterates over each name-value pair in the query and invokes the given function. ```js -const URL = require('url').URL; +const { URL } = require('url'); const myURL = new URL('https://example.org/?a=b&c=d'); myURL.searchParams.forEach((value, name, searchParams) => { console.log(name, value, myURL.searchParams === searchParams); @@ -1098,7 +1098,7 @@ using the [Punycode][] algorithm. Note, however, that a hostname *may* contain *both* Punycode encoded and percent-encoded characters. For example: ```js -const URL = require('url').URL; +const { URL } = require('url'); const myURL = new URL('https://%CF%80.com/foo'); console.log(myURL.href); // Prints https://xn--1xa.com/foo diff --git a/doc/guides/writing-and-running-benchmarks.md b/doc/guides/writing-and-running-benchmarks.md index f608dbd624c2e4..148082de4b6b3f 100644 --- a/doc/guides/writing-and-running-benchmarks.md +++ b/doc/guides/writing-and-running-benchmarks.md @@ -335,7 +335,7 @@ the code inside the `main` function if it's more than just declaration. ```js 'use strict'; const common = require('../common.js'); -const SlowBuffer = require('buffer').SlowBuffer; +const { SlowBuffer } = require('buffer'); const configs = { // Number of operations, specified here so they show up in the report.