Site icon TechVidvan

Node.js Command Line Options

Node JS command line

One of its notable features is the ability to accept command line options, which provide developers with a range of configuration and customization possibilities. In this article, we will explore the various command line options available in Node.js and learn how to use them effectively.

Command Line Options in Node.js:

Node.js provides a set of built-in command line options that you can use when running your JavaScript files. These options enable you to control various aspects of the Node.js runtime environment and tweak its behaviour according to your specific needs.

Let’s look at some commonly used command line options:

1. –help: This option displays the available command line options and their descriptions.

2. –version: It prints the version of Node.js currently installed on your system.

3. –inspect: This option enables the built-in Node.js debugger, allowing you to connect to it using a debugger client like Chrome DevTools.

4. –inspect-brk: Similar to –inspect, this option starts the debugger and pauses the execution of your script at the beginning so you can debug from the very start.

5. –max-old-space-size: Use this option to increase the maximum memory limit available to your Node.js process. For example, –max-old-space-size=4096 sets the maximum heap size to 4GB.

6. –trace-deprecation: It logs a stack trace whenever a deprecated API is used, helping you identify and update outdated code.

7. –trace-warnings: This option logs stack traces for certain runtime warnings, such as deprecations, promise rejections, and more.

8. –experimental modules: Enables experimental support for ECMAScript modules (ESM). Note that this feature is still experimental and subject to change.

Using Command Line Options in Node.js:

To use these command line options, you need to open a terminal or command prompt and navigate to the directory where your JavaScript file is located. Then, you can run your script with the desired options.

Here’s an example of using the –inspect option to enable the Node.js debugger:

node --inspect myscript.js

In this example, ‘myscript.js’ is the name of your JavaScript file. Once the script is running, you can open the Chrome browser and navigate to ‘chrome://inspect’ to connect to the Node.js debugger.

Similarly, you can use other command line options by appending them to the ‘node’ command when running your script.

It’s worth noting that you can also pass additional arguments to your script after the command line options.

For example:

node --max-old-space-size=4096 myscript.js arg1 arg2

In this case, ‘arg1’ and ‘arg2’ are arguments that will be accessible within your script via the ‘process.argv’ array.

Command Line Arguments:

Node.js allows you to pass command line arguments to your script. These arguments can be accessed using the ‘process.argv’ array. The first element of the array (‘process.argv[0]’) contains the path to the Node.js executable, and the second element (‘process.argv[1]’) contains the path to the executed file. Any additional arguments start from ‘process.argv[2]’ onwards.

For example:

// app.js
console.log(process.argv);

Running the following command:

node app.js arg1 arg2 arg3

Will produce the following output:

[ '/usr/local/bin/node', '/path/to/app.js', 'arg1', 'arg2', 'arg3' ]

Global Variables:

Node.js provides a set of global variables that can be used in your scripts. These variables are accessible without requiring any additional imports.

Some commonly used global variables include:

‘__dirname’: The directory name of the current module.

‘__filename’: The file name of the current module.

‘require’: A function to include and use external modules.

// app.js
console.log(__dirname);
console.log(__filename);
console.log(require);

Environment Variables:

Node.js allows you to access environment variables using the ‘process.env’ object. These variables are set outside of your application and can be useful for storing configuration values or sensitive information. To access an environment variable, use ‘process.env.VARIABLE_NAME’.

For example:

// app.js
console.log(process.env.HOME);
console.log(process.env.NODE_ENV);

Custom Command Line Options:

In addition to the built-in options, you can define custom command line options for your Node.js applications using libraries like ‘yargs’ or ‘commander’. These libraries provide a convenient way to define and parse command line options with minimal effort.

Here’s an example using the ‘yargs’ library:

// app.js
const argv = require('yargs')
  .option('name', {
    alias: 'n',
    description: 'Your name',
    type: 'string',
    demandOption: true
  })
  .option('age', {
    alias: 'a',
    description: 'Your age',
    type: 'number',
    demandOption: true
  })
  .argv;
 
console.log(`Hello ${argv.name}! You are ${argv.age} years old.`);

Running the following command:

node app.js --name John --age 25

Will produce the following output:

Hello John! You are 25 years old.

Conclusion

Node.js command line options provide developers with a range of configuration and debugging capabilities. Whether you need to enable the debugger, increase memory limits, or log warnings and deprecations, these options allow you to fine-tune the Node.js runtime environment according to your requirements.

Harnessing the power of command line options in Node.js can greatly enhance your development workflow and help you build robust and efficient applications. So, go ahead, experiment with different options, and leverage the flexibility they provide to take your Node.js projects to the next level.

Exit mobile version