0

I wish I could create my custom Windows command for example log that would execute a .js file for example app.js present in my project folder whenever called.

I also want to pass parameters to the command that I could access as variables in my app.js file.

In other words:
When I type log "Hello World" in Windows command prompt window, app.js file is executed and a parameter called "Hello World" is passed to it.

My app.js file looks like this:

// This is app.js file

// This file should be run when I type “log "Hello World" "abcd"” in cmd and we must get those parameters saying "Hello World" and "abcd" to be used as variables in this code

console.log("Hello ", ...cliParameters)
Mofi
  • 46,139
  • 17
  • 80
  • 143

1 Answers1

0

1. Creation of a directory for the batch files

In a command prompt window run first the command line:

set path

There are output the two environment variables PATH and PATHEXT which are used by the Windows command processor cmd.exe to find executables or scripts like batch files entered by the user with just their file names without file extension and without full path. For more details please look at What is the reason for "X is not recognized as an internal or external command, operable program or batch file"?

What you need first is to create a directory into which you save the batch files to run he Node.js scripts with the arguments. I suggest to use the following command line for creating such a directory:

md "%APPDATA%\MyBatFiles"

2. Adding this folder path to USER environment variable PATH

Click on Windows Start button and type on keyboard environment. Windows offers now in language of Windows:

  1. Edit environment variables for your account
  2. Edit the system environment variables

Click on the first item and the dialog window with title Environment Variables opens. If there is no environment variable with name Path in the upper list, click on button New below the upper list, otherwise select Path in the upper list and click on button Edit below the upper list. Enter Path as variable name if that is necessary at all (new variable). Then enter as variable value %APPDATA%\MyBatFiles (new variable) or append the folder path %APPDATA%\MyBatFiles at end of the variable value respectively at bottom of the folder path list. Finally close both dialog windows with a click on button OK.

3. Verify folder path in LOCAL environment variable PATH

Close the already opened command prompt window and open a new one. Run in the newly opened command prompt window:

set appdata
set path

There should be displayed now the environment variable PATH ending with the also displayed folder path of the environment variable APPDATA on which \MyBatFiles is appended, for example:

C:\Users\Ayush Samantaray\AppData\Roaming\MyBatFiles

4. Creating a batch file to run a Node.js script with all optional arguments

Let us assume that the Node.js script file app.js is stored in the directory %USERPROFILE%\Projects.

Let us assume next that Nodejs is installed into the directory %LOCALAPPDATA%\nvs\node\17.8.0\x64.

Then run in the command prompt window:

echo @"%%LOCALAPPDATA%%\nvs\node\17.8.0\x64\node.exe" "%%USERPROFILE%%\Projects\app.js" %%*>"%APPDATA%\MyBatFiles\log.cmd"

This command line creates in the directory %APPDATA%\MyBatFiles a batch file with name log.cmd containing the command line:

@"%LOCALAPPDATA%\nvs\node\17.8.0\x64\node.exe" "%USERPROFILE%\Projects\app.js" %*

Run in the command prompt window call /? for a description of %* at end of this command line which references all argument strings passed to the batch file.

4. How does it work?

Run in the command prompt window:

log "Hello World"

The Windows command processor cmd.exe searches now for a file with name log with a file extension as in semicolon separated list of environment variable PATHEXT in the current directory and next all directories of local environment variable PATH and finds log.cmd in directory %APPDATA%\MyBatFiles.

This batch file is processed now by cmd.exe with passing the argument string "Hello World" to the batch file.

@ at beginning of the single command line results in suppressing the output of the command line in the command prompt window after replacing all environment variable references like %LOCALAPPDATA% or %USERPROFILE% by the appropriate values.

So there is executed node.exe with its fully qualified file name to process the Node.js script file app.js specified also with fully qualified file name and with all the arguments passed to the batch file passed further to node.exe which passes these argument strings further to the Node.js script file.

5. Some more information

I don't have Node.js installed and have not read its documentation. So I don't know what is the default installation folder of Node.js and where Node.js is installed on your computer. I don't know the real file name of the executable of Node.js and I don't know which options it supports or requires to run a Node.js script file.

There can be used also a different path than %APPDATA%\MyBatFiles for your batch files. That is just an example.

If node.exe is the real file name of Node.js executable and its installation directory is already in either system or user environment variable Path, there can be used also just node.exe instead of "%LOCALAPPDATA%\nvs\node\17.8.0\x64\node.exe" in the batch files created by you. cmd.exe has to search in this case additionally also for node.exe after having found log.cmd to run this executable.

Mofi
  • 46,139
  • 17
  • 80
  • 143