You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
1. Updated code blocks to fences, added shell syntax.
2. Added headers to sections
3. Added "Special Commands" section, from the Node API docs
Mention the existence of built in REPL module and standalone executable
Copy file name to clipboardExpand all lines: locale/en/knowledge/REPL/how-to-use-nodejs-repl.md
+96-29Lines changed: 96 additions & 29 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,53 +8,120 @@ difficulty: 1
8
8
layout: knowledge-post.hbs
9
9
---
10
10
11
+
# Learn to use the REPL
11
12
13
+
Node.js ships with a Read-Eval-Print Loop, also known as a REPL. It is the Node.js interactive shell; any valid JavaScript which can be written in a script can be passed to the REPL. It can be extremely useful for experimenting with node.js, debugging code, and figuring out some of JavaScript's more eccentric behaviors.
12
14
13
-
Node.js ships with a REPL, which is short for 'Read-Eval-Print Loop'. It is the Node.js shell; any valid JavaScript which can be written in a script can be passed to the REPL. It can be extremely useful for experimenting with node.js, debugging code, and figuring out some of JavaScript's more eccentric behaviors.
15
+
Node.js has a standalone REPL accessible from the command line, and a built in REPL module you can use to [create your own custom REPLs](https://nodejs.org/api/repl.html#repl_repl). We are going to learn about the basics of the standalone REPL.
14
16
15
-
Running it is simple - just run node without a filename.
17
+
## How to Start the REPL
16
18
17
-
docs@nodejitsu:~/$ node
19
+
Starting the REPL is simple - just run node on the command line without a filename.
18
20
19
-
It then drops you into a simple prompt ('>') where you can type any JavaScript command you wish. As in most shells, you can press the up and down arrow keys to scroll through your command history and modify previous commands. The REPL also `Tab` to make the REPL try to autocomplete the command.
21
+
```shell
22
+
$ node
23
+
```
24
+
25
+
It then drops you into a simple prompt ('>') where you can type any JavaScript command you wish. As in most shells, you can press the up and down arrow keys to scroll through your command history and modify previous commands.
26
+
27
+
```shell
28
+
$ node
29
+
> var x = "Hello, World!"
30
+
undefined
31
+
> x
32
+
"Hello, World!"
33
+
> .exit
34
+
```
35
+
36
+
You can also use the `Tab` key to autocomplete some commands. When multiple autocomplete options are available, hit `Tab` again to cycle through them.
37
+
38
+
## Special Commands and Exiting the REPL
39
+
40
+
The following special commands are supported by all REPL instances (from [Node.js REPL docs](https://nodejs.org/api/repl.html#repl_commands_and_special_keys):
41
+
42
+
*`.exit` - Close the I/O stream, causing the REPL to exit.
43
+
*`.break` - When in the process of inputting a multi-line expression, entering
44
+
the `.break` command (or pressing the `<ctrl>-C` key combination) will abort
45
+
further input or processing of that expression.
46
+
*`.clear` - Resets the REPL `context` to an empty object and clears any
47
+
multi-line expression currently being input.
48
+
*`.help` - Show this list of special commands.
49
+
*`.save` - Save the current REPL session to a file:
50
+
`> .save ./file/to/save.js`
51
+
*`.load` - Load a file into the current REPL session.
52
+
`> .load ./file/to/load.js`
53
+
*`.editor` - Enter editor mode (`<ctrl>-D` to finish, `<ctrl>-C` to cancel).
54
+
55
+
```shell
56
+
> .editor
57
+
# Entering editor mode (<ctrl>-D to finish, <ctrl>-C to cancel)
58
+
functionwelcome(name) {
59
+
return`Hello ${name}!`;
60
+
}
61
+
62
+
welcome('Node.js User');
63
+
64
+
# <ctrl>-D
65
+
'Hello Node.js User!'
66
+
>
67
+
```
68
+
69
+
The following key combinations in the REPL have these special effects:
70
+
71
+
*`<ctrl>-C` - When pressed once, has the same effect as the `.break` command.
72
+
When pressed twice on a blank line, has the same effect as the `.exit`
73
+
command.
74
+
*`<ctrl>-D` - Has the same effect as the `.exit` command.
75
+
*`<tab>` - When pressed on a blank line, displays global and local (scope)
76
+
variables. When pressed while entering other input, displays relevant
77
+
autocompletion options.
78
+
79
+
## Return Values
20
80
21
81
Whenever you type a command, it will print the return value of the command. If you want to reuse the previous return value, you can use the special `_` variable.
22
82
23
83
For example:
24
-
25
-
node
26
-
> 1+1
27
-
2
28
-
> _+1
29
-
3
84
+
```shell
85
+
$ node
86
+
> 1+1
87
+
2
88
+
> _+1
89
+
3
90
+
```
30
91
31
92
One thing worth noting where REPL return values are concerned:
32
93
33
-
> x = 10
34
-
10
35
-
> var y = 5
36
-
> x
37
-
10
38
-
> y
39
-
5
94
+
```shell
95
+
> x = 10
96
+
10
97
+
> var y = 5
98
+
> x
99
+
10
100
+
> y
101
+
5
102
+
```
40
103
41
104
When the `var` keyword is used, the value of the expression is stored, but *NOT* returned. When a bare identifier is used, the value is also returned, as well as stored.
42
105
106
+
## Accessing Modules
107
+
43
108
If you need to access any of the builtin modules, or any third party modules, they can be accessed with `require`, just like in the rest of Node.
44
109
45
110
For example:
46
111
47
-
node
48
-
> path = require('path')
49
-
{ resolve: [Function],
50
-
normalize: [Function],
51
-
join: [Function],
52
-
dirname: [Function],
53
-
basename: [Function],
54
-
extname: [Function],
55
-
exists: [Function],
56
-
existsSync: [Function] }
57
-
> path.basename("/a/b/c.txt")
58
-
'c.txt'
112
+
```shell
113
+
$ node
114
+
> path = require('path')
115
+
{ resolve: [Function],
116
+
normalize: [Function],
117
+
join: [Function],
118
+
dirname: [Function],
119
+
basename: [Function],
120
+
extname: [Function],
121
+
exists: [Function],
122
+
existsSync: [Function] }
123
+
> path.basename("/a/b/c.txt")
124
+
'c.txt'
125
+
```
59
126
60
127
Note once again that without the `var` keyword, the contents of the object are returned immediately and displayed to `stdout`.
0 commit comments