Skip to content

Commit

Permalink
chore(learn): small update (#6132)
Browse files Browse the repository at this point in the history
* 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 <brian.muenzenmeyer@gmail.com>
Signed-off-by: Augustin Mauroy <augustin.mauroy@outlook.fr>

* Update pages/en/learn/command-line/output-to-the-command-line-using-nodejs.md

Co-authored-by: Brian Muenzenmeyer <brian.muenzenmeyer@gmail.com>
Signed-off-by: Augustin Mauroy <augustin.mauroy@outlook.fr>

* 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 <brian.muenzenmeyer@gmail.com>

---------

Signed-off-by: Augustin Mauroy <augustin.mauroy@outlook.fr>
Signed-off-by: Brian Muenzenmeyer <brian.muenzenmeyer@gmail.com>
Co-authored-by: Brian Muenzenmeyer <brian.muenzenmeyer@gmail.com>
  • Loading branch information
AugustinMauroy and bmuenzenmeyer authored Nov 24, 2023
1 parent db7f2e6 commit cf5f25f
Show file tree
Hide file tree
Showing 19 changed files with 57 additions and 60 deletions.
2 changes: 1 addition & 1 deletion pages/en/about/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion pages/en/get-involved/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
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
```
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: Node.js 20 introduced **experimental** [support for .env files](https://nodejs.org/dist/latest-v20.x/docs/api/cli.html#--env-fileconfig).
2 changes: 1 addition & 1 deletion pages/en/learn/command-line/how-to-use-the-nodejs-repl.md
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`, then you can use it:

```js
const chalk = require('chalk');
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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:
Expand All @@ -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).
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
2 changes: 1 addition & 1 deletion pages/en/learn/getting-started/nodejs-with-webassembly.md
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
Loading

0 comments on commit cf5f25f

Please sign in to comment.