Command Line Flags by the Node Binary
Last Updated :
29 Jul, 2022
In this article, we will learn about the command line flags offered by the Node binary. The Node.js platform is almost entirely represented by the node binary executable. In order to execute a JavaScript program we use: node app.js, where app.js is the program we wish to run. However, before we can start running programs, we need to learn some basic command line flags offered by the Node binary and how to use them.
Printing command options: In order to view the command-line flags for any version of the node, we can use the --help flag. Write the below code in the terminal:
node --help
Beyond the Node command line flags, there are additional flags for modifying the JavaScript runtime engine: V8. To view, these flags run node --v8-options. Write the below code in the terminal:
node --v8-options
This was all about the command line, now we need to know how to use it in order to save the hassle while the program is being executed. As you might know, the most basic requirement for a program to execute is that there shouldn't be any syntax errors. Some popular IDEs like VS Code come with tools like Intellisense which informs the users in case of any syntax errors, but what if you don't want to use the extensions and actually use the command line to know if there are any syntax errors. The node binary aids this too.
Checking the Syntax: It is possible to parse the JavaScript Application without actually running it, just to check the syntax.
To check the Syntax we can use:
node --check app.js
node -c app.js
Note: Any one of the flags can be used from the above list. On successful parsing one of the two things happen:
- There is no syntax error and no output is generated
- There is an error and an error message is displayed in the terminal window.\
Example 1: This is Case 1 where there isn't any syntax error:
JavaScript
const func = () => {
console.log("This block of code is "
+ "the example of good/correct syntax");
}
Step to check the Syntax: Write the below code in the terminal to check the syntax:
node --check app.js
Output:
Example 2: This is Case 2 where there is a syntax error.
JavaScript
const func: () => {
console.log("This block of code is an example of incorrect syntax.");
}
Step to check the Syntax: Write the below code in the terminal to check the syntax:
node --check app.js
Output:
In this way, we can check whether or not our code has any syntax errors.
Dynamic Evaluation: Node can directly evaluate code from the shell, which becomes very useful for quickly checking a code snippet. There are two flags that can evaluate the code. They are as follows:
- -p or --print: Evaluates the expression and prints the result.
- -e or --eval: Evaluates the expression but doesn't print the result.
Here are a few examples of how the --print and --eval flag works:
node -p "2+2"
node -e "2+2"
As you can see the -p flag evaluates the expression 2+2 and prints 4 as the output.
Consider another example that evaluates the console.log function in the command line:
node -e "console.log(2)"
The --eval or -e flag doesn't generate any output but here, as the console.log prints 2, the output is explicitly generated.
Similarly, when console.log is evaluated using the --print flag, this is what we get as the output:
node -p "console.log(2)"
2 is generated as output because it is explicitly printed using the console.log function, also undefined is printed in the terminal because console.log itself returns undefined.
Another example is where we can use the -p or --print flag to print all the files with the .js extension in the current working directory.
For the below example, these are the files that we have in the current directory:
We can use the --print flag as follows:
node -p "fs.readdirSync('.').filter(cf=>/.js$/.text(cf))"
Pre-Loading CommonJs Modules: The command line flag -r or --require can be used to preload a CommonJS module before anything else loads.
Suppose that there are two files preload.js and app.js, and their content is as follows:
Now in order the preload a file over another, we can use the --require flag as follows:
node --require ./preload.js app.js
Preloading modules is useful when using consuming modules that instrument or configure the process in some way.
NOTE: There are two module systems that Node uses, CommonJS and ESM, but it's important to note here that the --require flag can only preload a CommonJS module, not an ESM module. ESM modules have a vaguely related flag, called --loader, a currently experimental flag that should not be confused with the --require preloader flag. For more info on CommonJS and ESM modules, refer to the node documentation here.
Stack Trace Limit: Stack traces are generated for any Error that occurs, so they're usually the first point of call when debugging a failure scenario. By default, a stack trace will contain the last ten stack frames (function call sites) at the point where the trace occurred. This is often fine because the part of the stack you are interested in is often the last 3 or 4 call frames. However, there are scenarios where seeing more call frames in a stack trace makes sense, like checking that the application flow through various functions is as expected.
The stack trace limit can be modified with the --stack-trace-limit flag. This flag is part of the JavaScript runtime engine, V8, and can be found in the output of the --v8-options flag.
Consider a file named will-throw.js, with the following contents:
JavaScript
function f(n = 99) {
if (n == 85)
throw Error();
f(n - 1)
}
f()
When we execute the following command in the command line:
node --stack-trace-limit=10 will-throw.js
There will be only ten stack frames in the error output because the limit is explicitly set to 10.
What if we set the limit to say 1000, something like this:
node --stack-trace-limit=1000 will-throw.js
In this case, we can display output to 1000 stack frames.
We see that setting the stack trace limit to a number higher than the number of call frames in the stack guarantees that the entire stack will be output (In our case greater than 85).
But wait, is it really feasible to change the default limit of the stack frames?
The answer is NO because in production scenarios the overhead involved with retaining long stacks will not only cause overhead but also consume a lot of memory space, ultimately degrading the performance of your application.
Similar Reads
How to Read Command Line Arguments in Node ?
Command-line arguments (CLI) consist of text strings utilized to provide extra information to a program during its execution via the command line interface of an operating system. Node facilitates the retrieval of these arguments through the global object, specifically the process object. ApproachTo
2 min read
How to Parse Command Line Arguments in Node ?
Command-line arguments, in the context of a command-line interface (CLI), are text strings that provide extra information to a program when it is executed.In Nodejs, these arguments are accessible through an array known as argv (arguments values), where the shell passes all received command-line arg
3 min read
CLI Commands in NPM
NPM, short for Node Package Manager, is the default package manager for NodeJS. It is a command-line utility that allows you to install, manage, and share packages or modules of JavaScript code. These packages can range from small utility libraries to large frameworks, and they can be easily integra
4 min read
Uninstall Node.JS Using Linux Command Line
To uninstall Node.js from your Linux system, you can use the command line, and the process will vary depending on how you originally installed it. Whether you used a package manager like apt, installed Node.js using nvm (Node Version Manager), or manually installed it from a binary file, you can eas
3 min read
Command Line Arguments in ElectronJS
ElectronJS is an Open Source Framework used for building Cross-Platform native desktop applications using web technologies such as HTML, CSS, and JavaScript which are capable of running on Windows, macOS, and Linux operating systems. It combines the Chromium engine and NodeJS into a Single Runtime.
6 min read
npm bin Command
The npm bin command is a lesser-known but incredibly useful command in the Node.js ecosystem. It provides information about the location where npm installs globally executable binaries or locally installed binaries for the current project. In this article, weâll explore the details of the npm bin co
4 min read
How to print command line arguments passed to the script in Node.js ?
Node.js is an open-source and cross-platform runtime environment built on Chrome's V8 engine that enables us to use JavaScript outside the browser. Node.js helps us to use build server-side applications using JavaScript. In Node.js if you want to print the command line arguments then we can access
2 min read
How to kill all instances of a Node.js process via command line ?
To kill all instances of a Node.js process via the command line, use the command killall node on the Mac or Unix-based systems. This terminates all running Node.js processes. Why we need to kill all the instances?Sometimes there may be some issue with the NodeJS like the server is listening to some
2 min read
What are Modules in Node.js ?
In Node.js Application, a Module can be considered as a block of code that provide a simple or complex functionality that can communicate with external application. Modules can be organized in a single file or a collection of multiple files/folders. Almost all programmers prefer modules because of t
5 min read
Sending Command Line Arguments to NPM Script
In the sector of NodeJS development, NPM (Node Package Manager) scripts are a effective tool for automating numerous tasks like strolling exams, building the mission, or starting a improvement server. However, there are times while you need to bypass arguments to these scripts to customize their beh
3 min read