Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(learn): small update #6132

Merged
merged 10 commits into from
Nov 24, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ 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
```

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;
Expand All @@ -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);
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
canerakdas marked this conversation as resolved.
Show resolved Hide resolved
```
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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: Nodejs 20 and above have **experimental** support of .env file. You can read more about it [here](https://nodejs.org/dist/latest-v20.x/docs/api/cli.html#--env-fileconfig).
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand All @@ -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);
});
Expand Down Expand Up @@ -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@5`, then you can use it:
AugustinMauroy marked this conversation as resolved.
Show resolved Hide resolved

```js
const chalk = require('chalk');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,5 @@ Run the application using the `nodemon` command followed by the application's fi
```bash
nodemon app.js
```

> Note: nodejs 16 and above have an **exerimental** watch mode. You can use `node --watch app.js` to run the application in watch mode. You can read in the [api docs](https://nodejs.org/docs/latest-v18.x/api/cli.html#--watch).
2 changes: 1 addition & 1 deletion pages/en/learn/getting-started/introduction-to-nodejs.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down
4 changes: 3 additions & 1 deletion pages/en/learn/manipulating-files/nodejs-file-paths.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
8 changes: 4 additions & 4 deletions pages/en/learn/manipulating-files/nodejs-file-stats.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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');
Expand All @@ -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) {
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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');
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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');
Expand All @@ -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;
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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';

Expand All @@ -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();
};
Expand All @@ -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) {
Expand All @@ -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');
Expand All @@ -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 {
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down
10 changes: 5 additions & 5 deletions pages/en/learn/manipulating-files/writing-files-with-nodejs.md
Original file line number Diff line number Diff line change
Expand Up @@ -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!';

Expand All @@ -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!';

Expand All @@ -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 {
Expand Down Expand Up @@ -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!';

Expand All @@ -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 {
Expand Down
Loading