Appearance
The First Node Program
In all the previous chapters, the JavaScript code we wrote was executed in the browser. Now, starting from this chapter, the JavaScript code will no longer run in the browser environment, but in the Node environment. Therefore, the JavaScript code will run directly on your computer via the command line. We need to first choose a text editor to write the JavaScript code and save it to a directory on your local hard drive to execute it.
So, the question arises: which text editor is the best?
I still recommend using IntelliJ IDEA
to write Node.js code. Since Node.js code runs in a Node.js environment rather than a browser environment, there's no need to create HTML files; just create JavaScript files for development.
Input the following code:
javascript
'use strict';
console.log('Hello, world.');
The first line should always be 'use strict';
because we always run JavaScript code in strict mode to avoid various potential pitfalls.
Next, choose a directory, such as C:\Workspace
, and save the file as hello.js
. You can then open the command line window, change the current directory to the directory containing hello.js
, and enter the following command to run this program:
C:\Workspace> node hello.js
Hello, world.
You can also save it with a different name, like first.js
, but it must end with .js
. Additionally, the filename can only be a combination of English letters, numbers, and underscores.
If the file hello.js
does not exist in the current directory, running node hello.js
will result in an error:
C:\Workspace> node hello.js
node:internal/modules/cjs/loader:1227
throw err;
^
Error: Cannot find module 'C:\Workspace\hello.js'
at Module._resolveFilename
...
This error means that the file hello.js
could not be found because it does not exist. At this point, you should check if the file is present in the current directory.
Command Line Mode and Node Interactive Mode
Please distinguish between command line mode and Node interactive mode.
A prompt like C:\>
indicates the command line mode provided by Windows:
┌────────────────────────────────────────────────────────┐
│Command Prompt - □ x │
├────────────────────────────────────────────────────────┤
│Microsoft Windows [Version 10.0.0] │
│(c) 2015 Microsoft Corporation. All rights reserved. │
│ │
│C:\> node hello.js │
│Hello, world. │
│ │
│C:\> │
│ │
└────────────────────────────────────────────────────────┘
In command line mode, you can execute node
to enter the Node interactive environment or execute node hello.js
to run a .js
file.
A prompt like >
indicates that you are in the Node interactive environment:
┌────────────────────────────────────────────────────────┐
│Command Prompt - □ x │
├────────────────────────────────────────────────────────┤
│Microsoft Windows [Version 10.0.0] │
│(c) 2015 Microsoft Corporation. All rights reserved. │
│ │
│C:\> node │
│Welcome to Node.js v22.x.x. │
│> │
│ │
└────────────────────────────────────────────────────────┘
In the Node interactive environment, you can enter JavaScript code and execute it immediately.
Moreover, there are differences between running a .js
file in command line mode and running JavaScript code directly in the Node interactive environment. The Node interactive environment will automatically print the result of each line of JavaScript code, while running a JavaScript file directly will not display any output.
For example, in the Node interactive environment, entering:
> 100 + 200 + 300;
will yield:
600
However, if you create a file called calc.js
with the following content:
javascript
100 + 200 + 300;
and execute it in command line mode:
C:\Workspace> node calc.js
you will find that there is no output.
This is normal. To output results, you must use console.log()
to print them. Modify calc.js
as follows:
javascript
console.log(100 + 200 + 300);
Now when you execute it, you will see the result:
C:\Workspace> node calc.js
600
Summary
Write JavaScript programs using a text editor, save them with the .js
extension, and you can run these programs directly with node
.
What is the difference between the Node interactive mode and running a .js
file directly?
Entering node
to access interactive mode starts the Node interpreter, which waits for you to input source code line by line and executes each line as you enter it.
Directly running node hello.js
starts the Node interpreter and executes all the source code in hello.js
at once, without giving you the opportunity to input code interactively.
While writing JavaScript code, you can write in a text editor and have a Node interactive command window open simultaneously, pasting some code into the command line for verification, making it much more efficient! The prerequisite is having a 27" large display!