Node.jsREPLの使用方法

REPLはRead-Evaluate-Print-Loopの略で、ノードの機能をすばやく調べるのに最適な方法です。

ザ・nodeコマンドは、Node.jsスクリプトを実行するために使用するコマンドです。

node script.js

ファイル名を省略すると、REPLモードで使用します。

node

今すぐターミナルで試してみると、次のようになります。

❯ node
>

コマンドはアイドルモードのままで、何かが入力されるのを待ちます。

ヒント:ターミナルを開く方法がわからない場合は、「ターミナルを開く方法」。

REPLは、より正確には、JavaScriptコードの入力を待っています。

簡単に始めて入力してください

> console.log('test')
test
undefined
>

The first value, test, is the output we told the console to print, then we get undefined which is the return value of running console.log().

We can now enter a new line of JavaScript.

Use the tab to autocomplete

The cool thing about the REPL is that it’s interactive.

As you write your code, if you press the tab key the REPL will try to autocomplete what you wrote to match a variable you already defined or a predefined one.

Exploring JavaScript objects

Try entering the name of a JavaScript class, like Number, add a dot and press tab.

The REPL will print all the properties and methods you can access on that class:

Pressing tab reveals object properties

Explore global objects

You can inspect the globals you have access to by typing global. and pressing tab:

Globals

The _ special variable

If after some code you type _, that is going to print the result of the last operation.

Dot commands

The REPL has some special commands, all starting with a dot .. They are

  • .help: shows the dot commands help
  • .editor: enables editor more, to write multiline JavaScript code with ease. Once you are in this mode, enter ctrl-D to run the code you wrote.
  • .break: when inputting a multi-line expression, entering the .break command will abort further input. Same as pressing ctrl-C.
  • .clear: resets the REPL context to an empty object and clears any multi-line expression currently being input.
  • .load: loads a JavaScript file, relative to the current working directory
  • .save: saves all you entered in the REPL session to a file (specify the filename)
  • .exit: exists the repl (same as pressing ctrl-C two times)

The REPL knows when you are typing a multi-line statement without the need to invoke .editor.

For example if you start typing an iteration like this:

[1, 2, 3].forEach(num => {

and you press enter, the REPL will go to a new line that starts with 3 dots, indicating you can now continue to work on that block.

... console.log(num)
... })

If you type .break at the end of a line, the multiline mode will stop and the statement will not be executed.

Download my free Node.js Handbook


More node tutorials: