From cf5f25fc7e8dd368883893b87af112c35d42ead9 Mon Sep 17 00:00:00 2001 From: Augustin Mauroy Date: Fri, 24 Nov 2023 14:42:20 +0100 Subject: [PATCH] chore(learn): small update (#6132) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * chore(learn): small update * update grammar * add référence to Learn section * update about to have same code as learn * update from feedback * rewrite: "Restart the application automatically" * Update pages/en/learn/command-line/how-to-read-environment-variables-from-nodejs.md Co-authored-by: Brian Muenzenmeyer Signed-off-by: Augustin Mauroy * Update pages/en/learn/command-line/output-to-the-command-line-using-nodejs.md Co-authored-by: Brian Muenzenmeyer Signed-off-by: Augustin Mauroy * Update run-nodejs-scripts-from-the-command-line.md * Update pages/en/learn/command-line/run-nodejs-scripts-from-the-command-line.md Signed-off-by: Brian Muenzenmeyer --------- Signed-off-by: Augustin Mauroy Signed-off-by: Brian Muenzenmeyer Co-authored-by: Brian Muenzenmeyer --- pages/en/about/index.md | 2 +- pages/en/get-involved/index.md | 3 ++- ...-asynchronous-programming-and-callbacks.md | 2 +- .../overview-of-blocking-vs-non-blocking.md | 12 ++++----- .../the-nodejs-event-emitter.md | 2 +- .../understanding-processnexttick.md | 2 +- ...t-input-from-the-command-line-in-nodejs.md | 2 +- ...-read-environment-variables-from-nodejs.md | 2 ++ .../how-to-use-the-nodejs-repl.md | 2 +- ...output-to-the-command-line-using-nodejs.md | 4 ++- ...un-nodejs-scripts-from-the-command-line.md | 26 +++++-------------- .../getting-started/introduction-to-nodejs.md | 2 +- .../nodejs-with-webassembly.md | 2 +- .../manipulating-files/nodejs-file-paths.md | 4 ++- .../manipulating-files/nodejs-file-stats.md | 8 +++--- .../reading-files-with-nodejs.md | 6 ++--- ...working-with-file-descriptors-in-nodejs.md | 10 +++---- .../working-with-folders-in-nodejs.md | 16 +++++++----- .../writing-files-with-nodejs.md | 10 +++---- 19 files changed, 57 insertions(+), 60 deletions(-) diff --git a/pages/en/about/index.md b/pages/en/about/index.md index 178fb8c39d02..edba38b1991a 100644 --- a/pages/en/about/index.md +++ b/pages/en/about/index.md @@ -11,7 +11,7 @@ connections can be handled concurrently. Upon each connection, the callback is fired, but if there is no work to be done, Node.js will sleep. ```js -const http = require('http'); +const http = require('node:http'); const hostname = '127.0.0.1'; const port = 3000; diff --git a/pages/en/get-involved/index.md b/pages/en/get-involved/index.md index 73f61c87aab1..641d0ee16e03 100644 --- a/pages/en/get-involved/index.md +++ b/pages/en/get-involved/index.md @@ -18,7 +18,8 @@ layout: contribute.hbs ## Learning -- [Official API reference documentation](https://nodejs.org/api/) details the Node.js API. +- [Official Learn section](https://nodejs.org/en/learn/) of the Node.js website. +- [Official API reference documentation](https://nodejs.org/api/). - [NodeSchool.io](https://nodeschool.io/) will teach you Node.js concepts via interactive command-line games. - [Stack Overflow Node.js tag](https://stackoverflow.com/questions/tagged/node.js) collects new information every day. - [The DEV Community Node.js tag](https://dev.to/t/node) is a place to share Node.js projects, articles and tutorials as well as start discussions and ask for feedback on Node.js-related topics. Developers of all skill-levels are welcome to take part. diff --git a/pages/en/learn/asynchronous-work/javascript-asynchronous-programming-and-callbacks.md b/pages/en/learn/asynchronous-work/javascript-asynchronous-programming-and-callbacks.md index bd39d3355467..b504d2ed5532 100644 --- a/pages/en/learn/asynchronous-work/javascript-asynchronous-programming-and-callbacks.md +++ b/pages/en/learn/asynchronous-work/javascript-asynchronous-programming-and-callbacks.md @@ -93,7 +93,7 @@ How do you handle errors with callbacks? One very common strategy is to use what If there is no error, the object is `null`. If there is an error, it contains some description of the error and other information. ```js -const fs = require('fs'); +const fs = require('node:fs'); fs.readFile('/file.json', (err, data) => { if (err) { diff --git a/pages/en/learn/asynchronous-work/overview-of-blocking-vs-non-blocking.md b/pages/en/learn/asynchronous-work/overview-of-blocking-vs-non-blocking.md index e07cdbef3493..5936abf7030c 100644 --- a/pages/en/learn/asynchronous-work/overview-of-blocking-vs-non-blocking.md +++ b/pages/en/learn/asynchronous-work/overview-of-blocking-vs-non-blocking.md @@ -40,7 +40,7 @@ execute **asynchronously**. Using the File System module as an example, this is a **synchronous** file read: ```js -const fs = require('fs'); +const fs = require('node:fs'); const data = fs.readFileSync('/file.md'); // blocks here until file is read ``` @@ -48,7 +48,7 @@ const data = fs.readFileSync('/file.md'); // blocks here until file is read And here is an equivalent **asynchronous** example: ```js -const fs = require('fs'); +const fs = require('node:fs'); fs.readFile('/file.md', (err, data) => { if (err) throw err; @@ -65,7 +65,7 @@ shown. Let's expand our example a little bit: ```js -const fs = require('fs'); +const fs = require('node:fs'); const data = fs.readFileSync('/file.md'); // blocks here until file is read console.log(data); @@ -75,7 +75,7 @@ moreWork(); // will run after console.log And here is a similar, but not equivalent asynchronous example: ```js -const fs = require('fs'); +const fs = require('node:fs'); fs.readFile('/file.md', (err, data) => { if (err) throw err; @@ -114,7 +114,7 @@ There are some patterns that should be avoided when dealing with I/O. Let's look at an example: ```js -const fs = require('fs'); +const fs = require('node:fs'); fs.readFile('/file.md', (err, data) => { if (err) throw err; @@ -129,7 +129,7 @@ better way to write this, which is completely **non-blocking** and guaranteed to execute in the correct order is: ```js -const fs = require('fs'); +const fs = require('node:fs'); fs.readFile('/file.md', (readFileErr, data) => { if (readFileErr) throw readFileErr; diff --git a/pages/en/learn/asynchronous-work/the-nodejs-event-emitter.md b/pages/en/learn/asynchronous-work/the-nodejs-event-emitter.md index 8f4998f3074f..c8346e587721 100644 --- a/pages/en/learn/asynchronous-work/the-nodejs-event-emitter.md +++ b/pages/en/learn/asynchronous-work/the-nodejs-event-emitter.md @@ -15,7 +15,7 @@ This module, in particular, offers the `EventEmitter` class, which we'll use to You initialize that using ```js -const EventEmitter = require('events'); +const EventEmitter = require('node:events'); const eventEmitter = new EventEmitter(); ``` diff --git a/pages/en/learn/asynchronous-work/understanding-processnexttick.md b/pages/en/learn/asynchronous-work/understanding-processnexttick.md index 3424bd6ca5ca..f551fc76fcfe 100644 --- a/pages/en/learn/asynchronous-work/understanding-processnexttick.md +++ b/pages/en/learn/asynchronous-work/understanding-processnexttick.md @@ -47,6 +47,6 @@ process.nextTick(() => { ```bash Hello => number 1 Running at next tick => number 2 -Running before the timeout => number 3 The timeout running last => number 4 +Running before the timeout => number 3 ``` diff --git a/pages/en/learn/command-line/accept-input-from-the-command-line-in-nodejs.md b/pages/en/learn/command-line/accept-input-from-the-command-line-in-nodejs.md index 69dd019f90cc..26cc7fc183b8 100644 --- a/pages/en/learn/command-line/accept-input-from-the-command-line-in-nodejs.md +++ b/pages/en/learn/command-line/accept-input-from-the-command-line-in-nodejs.md @@ -11,7 +11,7 @@ How to make a Node.js CLI program interactive? Node.js since version 7 provides the [`readline` module](https://nodejs.org/api/readline.html) to perform exactly this: get input from a readable stream such as the `process.stdin` stream, which during the execution of a Node.js program is the terminal input, one line at a time. ```js -const readline = require('readline').createInterface({ +const readline = require('node:readline').createInterface({ input: process.stdin, output: process.stdout, }); diff --git a/pages/en/learn/command-line/how-to-read-environment-variables-from-nodejs.md b/pages/en/learn/command-line/how-to-read-environment-variables-from-nodejs.md index 3033029c9d76..5693d7341e6f 100644 --- a/pages/en/learn/command-line/how-to-read-environment-variables-from-nodejs.md +++ b/pages/en/learn/command-line/how-to-read-environment-variables-from-nodejs.md @@ -47,3 +47,5 @@ process.env.NODE_ENV; // "development" ``` > You can also run your js file with `node -r dotenv/config index.js` command if you don't want to import the package in your code. + +> Note: Node.js 20 introduced **experimental** [support for .env files](https://nodejs.org/dist/latest-v20.x/docs/api/cli.html#--env-fileconfig). diff --git a/pages/en/learn/command-line/how-to-use-the-nodejs-repl.md b/pages/en/learn/command-line/how-to-use-the-nodejs-repl.md index 022534e5d18d..c89d5bf1e496 100644 --- a/pages/en/learn/command-line/how-to-use-the-nodejs-repl.md +++ b/pages/en/learn/command-line/how-to-use-the-nodejs-repl.md @@ -113,7 +113,7 @@ If you type `.break` at the end of a line, the multiline mode will stop and the We can import the REPL in a JavaScript file using `repl`. ```js -const repl = require('repl'); +const repl = require('node:repl'); ``` Using the repl variable we can perform various operations. diff --git a/pages/en/learn/command-line/output-to-the-command-line-using-nodejs.md b/pages/en/learn/command-line/output-to-the-command-line-using-nodejs.md index 833790f355e8..345afd003c4b 100644 --- a/pages/en/learn/command-line/output-to-the-command-line-using-nodejs.md +++ b/pages/en/learn/command-line/output-to-the-command-line-using-nodejs.md @@ -80,6 +80,7 @@ You can just count apples and oranges: ```js const oranges = ['orange', 'orange']; const apples = ['just one apple']; + oranges.forEach(fruit => { console.count(fruit); }); @@ -97,6 +98,7 @@ We will use the apples and orange example to demonstrate this. ```js const oranges = ['orange', 'orange']; const apples = ['just one apple']; + oranges.forEach(fruit => { console.count(fruit); }); @@ -178,7 +180,7 @@ You can try that in the Node.js REPL, and it will print `hi!` in yellow. However, this is the low-level way to do this. The simplest way to go about coloring the console output is by using a library. [Chalk](https://github.com/chalk/chalk) is such a library, and in addition to coloring it also helps with other styling facilities, like making text bold, italic or underlined. -You install it with `npm install chalk@4`, then you can use it: +You install it with `npm install chalk`, then you can use it: ```js const chalk = require('chalk'); diff --git a/pages/en/learn/command-line/run-nodejs-scripts-from-the-command-line.md b/pages/en/learn/command-line/run-nodejs-scripts-from-the-command-line.md index 2785722c48ea..883f68f13df8 100644 --- a/pages/en/learn/command-line/run-nodejs-scripts-from-the-command-line.md +++ b/pages/en/learn/command-line/run-nodejs-scripts-from-the-command-line.md @@ -1,7 +1,7 @@ --- title: Run Node.js scripts from the command line layout: learn.hbs -authors: flaviocopes, MylesBorins, fhemberger, LaRuaNa, ahmadawais, akazyti +authors: flaviocopes, MylesBorins, fhemberger, LaRuaNa, ahmadawais, akazyti, AugustinMauroy --- # Run Node.js scripts from the command line @@ -25,7 +25,7 @@ Above, we are explicitly giving the absolute path of interpreter. Not all operat ```js #!/usr/bin/env node -// your code +// your javascript code ``` To use a shebang, your file should have executable permission. You can give `app.js` the executable permission by running: @@ -48,24 +48,12 @@ node -e "console.log(123)" ## Restart the application automatically -The `node` command has to be re-executed in bash whenever there is a change in the application. To restart the application automatically, use the `nodemon` module. - -Install the nodemon module globally to system path: - -```bash -npm i -g nodemon -``` - -You can also install nodemon as a development dependency: +As of nodejs V16, there is a built-in option to automatically restart the application when a file changes. This is useful for development purposes. +To use this feature, you need to pass the `--watch' flag to nodejs. ```bash -npm i --save-dev nodemon +node --watch app.js ``` -This local installation of nodemon can be run by calling it from within npm script such as npm start or using npx nodemon. - -Run the application using the `nodemon` command followed by the application's file name: - -```bash -nodemon app.js -``` +So when you change the file, the application will restart automatically. +Read the [`--watch` flag documentation](https://nodejs.org/docs/latest/api/cli.html#--watch). diff --git a/pages/en/learn/getting-started/introduction-to-nodejs.md b/pages/en/learn/getting-started/introduction-to-nodejs.md index 0042aa4c346f..11995db5b8b0 100644 --- a/pages/en/learn/getting-started/introduction-to-nodejs.md +++ b/pages/en/learn/getting-started/introduction-to-nodejs.md @@ -25,7 +25,7 @@ In Node.js the new ECMAScript standards can be used without problems, as you don The most common example Hello World of Node.js is a web server: ```js -const http = require('http'); +const http = require('node:http'); const hostname = '127.0.0.1'; const port = 3000; diff --git a/pages/en/learn/getting-started/nodejs-with-webassembly.md b/pages/en/learn/getting-started/nodejs-with-webassembly.md index a18683a4c9a6..c042d515cd19 100644 --- a/pages/en/learn/getting-started/nodejs-with-webassembly.md +++ b/pages/en/learn/getting-started/nodejs-with-webassembly.md @@ -47,7 +47,7 @@ Once you have a WebAssembly module, you can use the Node.js `WebAssembly` object ```js // Assume add.wasm file exists that contains a single function adding 2 provided arguments -const fs = require('fs'); +const fs = require('node:fs'); const wasmBuffer = fs.readFileSync('/path/to/add.wasm'); WebAssembly.instantiate(wasmBuffer).then(wasmModule => { diff --git a/pages/en/learn/manipulating-files/nodejs-file-paths.md b/pages/en/learn/manipulating-files/nodejs-file-paths.md index 06d521f1c298..8ad9b49e3e1e 100644 --- a/pages/en/learn/manipulating-files/nodejs-file-paths.md +++ b/pages/en/learn/manipulating-files/nodejs-file-paths.md @@ -10,7 +10,7 @@ Every file in the system has a path. On Linux and macOS, a path might look like: You need to pay attention when using paths in your applications, as this difference must be taken into account. -You include this module in your files using `const path = require('path');` and you can start using its methods. +You include this module in your files using `const path = require('node:path');` and you can start using its methods. ## Getting information out of a path @@ -23,6 +23,8 @@ Given a path, you can extract information out of it using those methods: ### Example ```js +const path = require('node:path'); + const notes = '/users/joe/notes.txt'; path.dirname(notes); // /users/joe diff --git a/pages/en/learn/manipulating-files/nodejs-file-stats.md b/pages/en/learn/manipulating-files/nodejs-file-stats.md index 73e89f225840..13f3cc0a0104 100644 --- a/pages/en/learn/manipulating-files/nodejs-file-stats.md +++ b/pages/en/learn/manipulating-files/nodejs-file-stats.md @@ -11,7 +11,7 @@ Every file comes with a set of details that we can inspect using Node.js. In par You call it passing a file path, and once Node.js gets the file details it will call the callback function you pass, with 2 parameters: an error message, and the file stats: ```js -const fs = require('fs'); +const fs = require('node:fs'); fs.stat('/Users/joe/test.txt', (err, stats) => { if (err) { @@ -24,7 +24,7 @@ fs.stat('/Users/joe/test.txt', (err, stats) => { Node.js also provides a sync method, which blocks the thread until the file stats are ready: ```js -const fs = require('fs'); +const fs = require('node:fs'); try { const stats = fs.statSync('/Users/joe/test.txt'); @@ -44,7 +44,7 @@ The file information is included in the stats variable. What kind of information There are other advanced methods, but the bulk of what you'll use in your day-to-day programming is this. ```js -const fs = require('fs'); +const fs = require('node:fs'); fs.stat('/Users/joe/test.txt', (err, stats) => { if (err) { @@ -62,7 +62,7 @@ fs.stat('/Users/joe/test.txt', (err, stats) => { You can also use promise-based `fsPromises.stat()` method offered by the `fs/promises` module if you like: ```js -const fs = require('fs/promises'); +const fs = require('node:fs/promises'); async function example() { try { diff --git a/pages/en/learn/manipulating-files/reading-files-with-nodejs.md b/pages/en/learn/manipulating-files/reading-files-with-nodejs.md index e087302d87e7..55b17d98761d 100644 --- a/pages/en/learn/manipulating-files/reading-files-with-nodejs.md +++ b/pages/en/learn/manipulating-files/reading-files-with-nodejs.md @@ -9,7 +9,7 @@ authors: flaviocopes, MylesBorins, fhemberger, LaRuaNa, ahmadawais, clean99 The simplest way to read a file in Node.js is to use the `fs.readFile()` method, passing it the file path, encoding and a callback function that will be called with the file data (and the error): ```js -const fs = require('fs'); +const fs = require('node:fs'); fs.readFile('/Users/joe/test.txt', 'utf8', (err, data) => { if (err) { @@ -23,7 +23,7 @@ fs.readFile('/Users/joe/test.txt', 'utf8', (err, data) => { Alternatively, you can use the synchronous version `fs.readFileSync()`: ```js -const fs = require('fs'); +const fs = require('node:fs'); try { const data = fs.readFileSync('/Users/joe/test.txt', 'utf8'); @@ -36,7 +36,7 @@ try { You can also use the promise-based `fsPromises.readFile()` method offered by the `fs/promises` module: ```js -const fs = require('fs/promises'); +const fs = require('node:fs/promises'); async function example() { try { diff --git a/pages/en/learn/manipulating-files/working-with-file-descriptors-in-nodejs.md b/pages/en/learn/manipulating-files/working-with-file-descriptors-in-nodejs.md index 34b083748390..27cf097ca312 100644 --- a/pages/en/learn/manipulating-files/working-with-file-descriptors-in-nodejs.md +++ b/pages/en/learn/manipulating-files/working-with-file-descriptors-in-nodejs.md @@ -11,7 +11,7 @@ Before you're able to interact with a file that sits in your filesystem, you mus A file descriptor is a reference to an open file, a number (fd) returned by opening the file using the `open()` method offered by the `fs` module. This number (`fd`) uniquely identifies an open file in operating system: ```js -const fs = require('fs'); +const fs = require('node:fs'); fs.open('/Users/joe/test.txt', 'r', (err, fd) => { // fd is our file descriptor @@ -34,7 +34,7 @@ That flag means we open the file for reading. You can also open the file by using the `fs.openSync` method, which returns the file descriptor, instead of providing it in a callback: ```js -const fs = require('fs'); +const fs = require('node:fs'); try { const fd = fs.openSync('/Users/joe/test.txt', 'r'); @@ -50,7 +50,7 @@ You can also open the file by using the promise-based `fsPromises.open` method o The `fs/promises` module is available starting only from Node.js v14. Before v14, after v10, you can use `require('fs').promises` instead. Before v10, after v8, you can use `util.promisify` to convert `fs` methods into promise-based methods. ```js -const fs = require('fs/promises'); +const fs = require('node:fs/promises'); // Or const fs = require('fs').promises before v14. async function example() { let filehandle; @@ -68,8 +68,8 @@ example(); Here is an example of `util.promisify`: ```js -const fs = require('fs'); -const util = require('util'); +const fs = require('node:fs'); +const util = require('node:util'); async function example() { const open = util.promisify(fs.open); diff --git a/pages/en/learn/manipulating-files/working-with-folders-in-nodejs.md b/pages/en/learn/manipulating-files/working-with-folders-in-nodejs.md index c35dabf701e3..7c923786fd87 100644 --- a/pages/en/learn/manipulating-files/working-with-folders-in-nodejs.md +++ b/pages/en/learn/manipulating-files/working-with-folders-in-nodejs.md @@ -17,7 +17,7 @@ Use `fs.access()` (and its promise-based `fsPromises.access()` counterpart) to c Use `fs.mkdir()` or `fs.mkdirSync()` or `fsPromises.mkdir()` to create a new folder. ```js -const fs = require('fs'); +const fs = require('node:fs'); const folderName = '/Users/joe/test'; @@ -37,7 +37,7 @@ Use `fs.readdir()` or `fs.readdirSync()` or `fsPromises.readdir()` to read the c This piece of code reads the content of a folder, both files and subfolders, and returns their relative path: ```js -const fs = require('fs'); +const fs = require('node:fs'); const folderPath = '/Users/joe'; @@ -55,6 +55,8 @@ fs.readdirSync(folderPath).map(fileName => { You can also filter the results to only return the files, and exclude the folders: ```js +const fs = require('node:fs'); + const isFile = fileName => { return fs.lstatSync(fileName).isFile(); }; @@ -71,7 +73,7 @@ fs.readdirSync(folderPath) Use `fs.rename()` or `fs.renameSync()` or `fsPromises.rename()` to rename folder. The first parameter is the current path, the second the new path: ```js -const fs = require('fs'); +const fs = require('node:fs'); fs.rename('/Users/joe', '/Users/roger', err => { if (err) { @@ -84,7 +86,7 @@ fs.rename('/Users/joe', '/Users/roger', err => { `fs.renameSync()` is the synchronous version: ```js -const fs = require('fs'); +const fs = require('node:fs'); try { fs.renameSync('/Users/joe', '/Users/roger'); @@ -96,7 +98,7 @@ try { `fsPromises.rename()` is the promise-based version: ```js -const fs = require('fs/promises'); +const fs = require('node:fs/promises'); async function example() { try { @@ -113,7 +115,7 @@ example(); Use `fs.rmdir()` or `fs.rmdirSync()` or `fsPromises.rmdir()` to remove a folder. ```js -const fs = require('fs'); +const fs = require('node:fs'); fs.rmdir(dir, err => { if (err) { @@ -129,7 +131,7 @@ To remove a folder that has contents use `fs.rm()` with the option `{ recursive: `{ recursive: true, force: true }` makes it so that exceptions will be ignored if the folder does not exist. ```js -const fs = require('fs'); +const fs = require('node:fs'); fs.rm(dir, { recursive: true, force: true }, err => { if (err) { diff --git a/pages/en/learn/manipulating-files/writing-files-with-nodejs.md b/pages/en/learn/manipulating-files/writing-files-with-nodejs.md index 34521853adfc..429ca17e2051 100644 --- a/pages/en/learn/manipulating-files/writing-files-with-nodejs.md +++ b/pages/en/learn/manipulating-files/writing-files-with-nodejs.md @@ -11,7 +11,7 @@ authors: flaviocopes, MylesBorins, fhemberger, LaRuaNa, ahmadawais, clean99, ovf The easiest way to write to files in Node.js is to use the `fs.writeFile()` API. ```js -const fs = require('fs'); +const fs = require('node:fs'); const content = 'Some content!'; @@ -28,7 +28,7 @@ fs.writeFile('/Users/joe/test.txt', content, err => { Alternatively, you can use the synchronous version `fs.writeFileSync()`: ```js -const fs = require('fs'); +const fs = require('node:fs'); const content = 'Some content!'; @@ -43,7 +43,7 @@ try { You can also use the promise-based `fsPromises.writeFile()` method offered by the `fs/promises` module: ```js -const fs = require('fs/promises'); +const fs = require('node:fs/promises'); async function example() { try { @@ -84,7 +84,7 @@ Appending to files is handy when you don't want to overwrite a file with new con A handy method to append content to the end of a file is `fs.appendFile()` (and its `fs.appendFileSync()` counterpart): ```js -const fs = require('fs'); +const fs = require('node:fs'); const content = 'Some content!'; @@ -101,7 +101,7 @@ fs.appendFile('file.log', content, err => { Here is a `fsPromises.appendFile()` example: ```js -const fs = require('fs/promises'); +const fs = require('node:fs/promises'); async function example() { try {