REPL代表Read-Evaluate-Print-Loop,它是快速探索Node功能的好方法
這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:

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

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:
- An introduction to the npm package manager
- Introduction to Node.js
- HTTP requests using Axios
- Where to host a Node.js app
- Interact with the Google Analytics API using Node.js
- The npx Node Package Runner
- The package.json guide
- Where does npm install the packages?
- How to update Node.js
- How to use or execute a package installed using npm
- The package-lock.json file
- Semantic Versioning using npm
- Should you commit the node_modules folder to Git?
- Update all the Node dependencies to their latest version
- Parsing JSON with Node.js
- Find the installed version of an npm package
- Node.js Streams
- Install an older version of an npm package
- Get the current folder in Node
- How to log an object in Node
- Expose functionality from a Node file using exports
- Differences between Node and the Browser
- Make an HTTP POST request using Node
- Get HTTP request body data using Node
- Node Buffers
- A brief history of Node.js
- How to install Node.js
- How much JavaScript do you need to know to use Node?
- How to use the Node.js REPL
- Node, accept arguments from the command line
- Output to the command line using Node
- Accept input from the command line in Node
- Uninstalling npm packages with `npm uninstall`
- npm global or local packages
- npm dependencies and devDependencies
- The Node.js Event Loop
- Understanding process.nextTick()
- Understanding setImmediate()
- The Node Event emitter
- Build an HTTP Server
- Making HTTP requests with Node
- The Node fs module
- HTTP requests in Node using Axios
- Reading files with Node
- Node File Paths
- Writing files with Node
- Node file stats
- Working with file descriptors in Node
- Working with folders in Node
- The Node path module
- The Node http module
- Using WebSockets with Node.js
- The basics of working with MySQL and Node
- Error handling in Node.js
- The Pug Guide
- How to read environment variables from Node.js
- How to exit from a Node.js program
- The Node os module
- The Node events module
- Node, the difference between development and production
- How to check if a file exists in Node.js
- How to create an empty file in Node.js
- How to remove a file with Node.js
- How to get the last updated date of a file using Node.js
- How to determine if a date is today in JavaScript
- How to write a JSON object to file in Node.js
- Why should you use Node.js in your next project?
- Run a web server from any folder
- How to use MongoDB with Node.js
- Use the Chrome DevTools to debug a Node.js app
- What is pnpm?
- The Node.js Runtime v8 options list
- How to fix the "Missing write access" error when using npm
- How to enable ES Modules in Node.js
- How to spawn a child process with Node.js
- How to get both parsed body and raw body in Express
- How to handle file uploads in Node.js
- What are peer dependencies in a Node module?
- How to write a CSV file with Node.js
- How to read a CSV file with Node.js
- The Node Core Modules
- Incrementing multiple folders numbers at once using Node.js
- How to print a canvas to a data URL
- How to create and save an image with Node.js and Canvas
- How to download an image using Node.js
- How to mass rename files in Node.js
- How to get the names of all the files in a folder in Node
- How to use promises and await with Node.js callback-based functions
- How to test an npm package locally
- How to check the current Node.js version at runtime
- How to use Sequelize to interact with PostgreSQL
- Serve an HTML page using Node.js
- How to solve the `util.pump is not a function` error in Node.js