Home >Web Front-end >JS Tutorial >How to run script with nodejs

How to run script with nodejs

青灯夜游
青灯夜游Original
2021-10-28 18:38:3714131browse

Operation method: 1. Open the cmd command window, execute the "node" command to enter the node editing mode, enter the js script code, and press Enter to run. 2. Write the js script code into the js file and execute "node js file path" in the cmd command window.

How to run script with nodejs

The operating environment of this tutorial: windows7 system, nodejs version 12.19.0, Dell G3 computer.

How to run a script using nodejs:

1. Use node to execute a separate js code script

First enter the node command on the console to enter the node code execution and editing mode. As shown below, an arrow and input cursor will be displayed

How to run script with nodejs

This Just enter a separate js code at the cursor, as shown below, I randomly executed some javascript code

[It should be noted that I heard that the way to enter the node mode to execute js code is only one line Execute js code line by line, but cannot execute a js file】

How to run script with nodejs

2. Use node to execute the entire js script file

If If you want to use node to directly execute an entire nodejs file, you cannot enter the editing mode of node. Instead, you should enter directly in the command box: node js file name

is as follows:

node hello
或者
node hello.js

For the js file following node, the .js suffix can be added or not. (Note: If the js file is not in the directory where the current command box is located, you need to locate the command box in the corresponding directory first, or add the specific path of the js file name after node)

How to run script with nodejs

[Recommended learning: "nodejs tutorial"]

The above is the detailed content of How to run script with nodejs. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn