I have a web server written in Node.js and I would like to launch with a specific folder. I'm not sure how to access arguments in JavaScript. I'm running node like this:
$ node server.js folder
here server.js
is my server code. Node.js help says this is possible:
$ node -h
Usage: node [options] script.js [arguments]
How would I access those arguments in JavaScript? Somehow I was not able to find this information on the web.
The arguments are stored in process.argv
Here are the node docs on handling command line args:
process.argv
is an array containing the command line arguments. The first element will be 'node', the second element will be the name of the JavaScript file. The next elements will be any additional command line arguments.
// print process.argv
process.argv.forEach(function (val, index, array) {
console.log(index + ': ' + val);
});
This will generate:
$ node process-2.js one two=three four
0: node
1: /Users/mjr/work/node/process-2.js
2: one
3: two=three
4: four
Answered 2023-09-20 20:22:05
node [options] [ -e script | script.js ] [arguments]
or node debug script.js [arguments]
. for example: node --harmony script.js balala
or node --no-deprecation --enable-ssl2 script.js balala
, we can use process.execArgv with process.argv - anyone --harmony
in your example - anyone process.argv
. - anyone To normalize the arguments like a regular javascript function would receive, I do this in my node.js shell scripts:
var args = process.argv.slice(2);
Note that the first arg is usually the path to nodejs, and the second arg is the location of the script you're executing.
Answered 2023-09-20 20:22:05
for (let i=2; i<argv.length; i++) { switch (argv[i]) { case "--foo": foo=argv[++i]; break; case "--bar": bar=argv[++i]; break; default: throw new Error("unknown arg"); } }
- anyone The up-to-date right answer for this it to use the minimist library. We used to use node-optimist but it has since been deprecated.
Here is an example of how to use it taken straight from the minimist documentation:
var argv = require('minimist')(process.argv.slice(2));
console.dir(argv);
-
$ node example/parse.js -a beep -b boop
{ _: [], a: 'beep', b: 'boop' }
-
$ node example/parse.js -x 3 -y 4 -n5 -abc --beep=boop foo bar baz
{ _: [ 'foo', 'bar', 'baz' ],
x: 3,
y: 4,
n: 5,
a: true,
b: true,
c: true,
beep: 'boop' }
Answered 2023-09-20 20:22:05
process.argv.slice(2)
which is the answer for the actual question... - anyone const args = process.argv;
console.log(args);
This returns:
$ node server.js one two=three four
['node', '/home/server.js', 'one', 'two=three', 'four']
Minimist: For minimal argument parsing.
Commander.js: Most adopted module for argument parsing.
Meow: Lighter alternative to Commander.js
Yargs: More sophisticated argument parsing (heavy).
Vorpal.js: Mature / interactive command-line applications with argument parsing.
Answered 2023-09-20 20:22:05
Check out optimist library, it is much better than parsing command line options by hand.
Update
Optimist is deprecated. Try yargs which is an active fork of optimist.
Answered 2023-09-20 20:22:05
Several great answers here, but it all seems very complex. This is very similar to how bash scripts access argument values and it's already provided standard with node.js as MooGoo pointed out. (Just to make it understandable to somebody that's new to node.js)
Example:
$ node yourscript.js banana monkey
var program_name = process.argv[0]; //value will be "node"
var script_path = process.argv[1]; //value will be "yourscript.js"
var first_value = process.argv[2]; //value will be "banana"
var second_value = process.argv[3]; //value will be "monkey"
Answered 2023-09-20 20:22:05
function getArgs () {
const args = {};
process.argv
.slice(2, process.argv.length)
.forEach( arg => {
// long arg
if (arg.slice(0,2) === '--') {
const longArg = arg.split('=');
const longArgFlag = longArg[0].slice(2,longArg[0].length);
const longArgValue = longArg.length > 1 ? longArg[1] : true;
args[longArgFlag] = longArgValue;
}
// flags
else if (arg[0] === '-') {
const flags = arg.slice(1,arg.length).split('');
flags.forEach(flag => {
args[flag] = true;
});
}
});
return args;
}
const args = getArgs();
console.log(args);
input
node test.js -D --name=Hello
output
{ D: true, name: 'Hello' }
input
node config/build.js -lHRs --ip=$HOST --port=$PORT --env=dev
output
{
l: true,
H: true,
R: true,
s: true,
ip: '127.0.0.1',
port: '8080',
env: 'dev'
}
Answered 2023-09-20 20:22:05
.slice(2, process.argv.length)
isn't the second arg redundant? .slice()
goes to the end of string by default. - anyone Works great for defining your options, actions, and arguments. It also generates the help pages for you.
Works great for getting input from the user, if you like the callback approach.
Works great for getting input from the user, if you like the generator approach.
Answered 2023-09-20 20:22:05
The easiest way to parse command-line arguments in NodeJS is using the stdio module. Inspired by UNIX getopt
utility, it is as trivial as follows:
var stdio = require('stdio');
var ops = stdio.getopt({
'check': {key: 'c', args: 2, description: 'What this option means'},
'map': {key: 'm', description: 'Another description'},
'kaka': {args: 1, required: true},
'ooo': {key: 'o'}
});
If you run the previous code with this command:
node <your_script.js> -c 23 45 --map -k 23 file1 file2
Then ops
object will be as follows:
{ check: [ '23', '45' ],
args: [ 'file1', 'file2' ],
map: true,
kaka: '23' }
So you can use it as you want. For instance:
if (ops.kaka && ops.check) {
console.log(ops.kaka + ops.check[0]);
}
Grouped options are also supported, so you can write -om
instead of -o -m
.
Furthermore, stdio
can generate a help/usage output automatically. If you call ops.printHelp()
you'll get the following:
USAGE: node something.js [--check <ARG1> <ARG2>] [--kaka] [--ooo] [--map]
-c, --check <ARG1> <ARG2> What this option means (mandatory)
-k, --kaka (mandatory)
--map Another description
-o, --ooo
The previous message is shown also if a mandatory option is not given (preceded by the error message) or if it is mispecified (for instance, if you specify a single arg for an option and it needs 2).
You can install stdio module using NPM:
npm install stdio
Answered 2023-09-20 20:22:05
requires 1 arguments
for an argument that I've set required: false
. I even tried this with version 2 of the library. - anyone --help
behavior enough in your case? Help must be printed. - anyone ops
in <your_script.js>
? - anyone If your script is called myScript.js and you want to pass the first and last name, 'Sean Worthington', as arguments like below:
node myScript.js Sean Worthington
Then within your script you write:
var firstName = process.argv[2]; // Will be set to 'Sean'
var lastName = process.argv[3]; // Will be set to 'Worthington'
Answered 2023-09-20 20:22:05
const process = require( 'process' );
const argv = key => {
// Return true if the key exists and a value is defined
if ( process.argv.includes( `--${ key }` ) ) return true;
const value = process.argv.find( element => element.startsWith( `--${ key }=` ) );
// Return null if the key does not exist and a value is not defined
if ( !value ) return null;
return value.replace( `--${ key }=` , '' );
}
Output:
node app.js
then argv('foo')
will return null
node app.js --foo
then argv('foo')
will return true
node app.js --foo=
then argv('foo')
will return ''
node app.js --foo=bar
then argv('foo')
will return 'bar'
Answered 2023-09-20 20:22:05
if ( process.argv.includes( `--${ key }` ) )
not true
for --foo=bar
? I'm confused how it ever gets past that first conditional. - anyone includes
is testing matching values in the argv array, not substrings in each argv entry. So the value must be an exact match: i.e. Testing argv with includes for the --foo
element would not match --foo=bar
, which would be separate value in the array. The next line, process.argv.find
shows what the substring search looks like. - anyone command-line-args is worth a look!
You can set options using the main notation standards (learn more). These commands are all equivalent, setting the same values:
$ example --verbose --timeout=1000 --src one.js --src two.js
$ example --verbose --timeout 1000 --src one.js two.js
$ example -vt 1000 --src one.js two.js
$ example -vt 1000 one.js two.js
To access the values, first create a list of option definitions describing the options your application accepts. The type
property is a setter function (the value supplied is passed through this), giving you full control over the value received.
const optionDefinitions = [
{ name: 'verbose', alias: 'v', type: Boolean },
{ name: 'src', type: String, multiple: true, defaultOption: true },
{ name: 'timeout', alias: 't', type: Number }
]
Next, parse the options using commandLineArgs():
const commandLineArgs = require('command-line-args')
const options = commandLineArgs(optionDefinitions)
options
now looks like this:
{
src: [
'one.js',
'two.js'
],
verbose: true,
timeout: 1000
}
Beside the above typical usage, you can configure command-line-args to accept more advanced syntax forms.
Command-based syntax (git style) in the form:
$ executable <command> [options]
For example.
$ git commit --squash -m "This is my commit message"
Command and sub-command syntax (docker style) in the form:
$ executable <command> [options] <sub-command> [options]
For example.
$ docker run --detached --image centos bash -c yum install -y httpd
A usage guide (typically printed when --help
is set) can be generated using command-line-usage. See the examples below and read the documentation for instructions how to create them.
A typical usage guide example.
The polymer-cli usage guide is a good real-life example.
There is plenty more to learn, please see the wiki for examples and documentation.
Answered 2023-09-20 20:22:05
Here's my 0-dep solution for named arguments:
const args = process.argv
.slice(2)
.map(arg => arg.split('='))
.reduce((args, [value, key]) => {
args[value] = key;
return args;
}, {});
console.log(args.foo)
console.log(args.fizz)
Example:
$ node test.js foo=bar fizz=buzz
bar
buzz
Note: Naturally this will fail when the argument contains a =
. This is only for very simple usage.
Answered 2023-09-20 20:22:05
There's an app for that. Well, module. Well, more than one, probably hundreds.
Yargs is one of the fun ones, its docs are cool to read.
Here's an example from the github/npm page:
#!/usr/bin/env node
var argv = require('yargs').argv;
console.log('(%d,%d)', argv.x, argv.y);
console.log(argv._);
Output is here (it reads options with dashes etc, short and long, numeric etc).
$ ./nonopt.js -x 6.82 -y 3.35 rum
(6.82,3.35)
[ 'rum' ]
$ ./nonopt.js "me hearties" -x 0.54 yo -y 1.12 ho
(0.54,1.12)
[ 'me hearties', 'yo', 'ho' ]
Answered 2023-09-20 20:22:05
proj.js
for(var i=0;i<process.argv.length;i++){
console.log(process.argv[i]);
}
Terminal:
nodemon app.js "arg1" "arg2" "arg3"
Result:
0 'C:\\Program Files\\nodejs\\node.exe'
1 'C:\\Users\\Nouman\\Desktop\\Node\\camer nodejs\\proj.js'
2 'arg1' your first argument you passed.
3 'arg2' your second argument you passed.
4 'arg3' your third argument you passed.
Explaination:
C:\Program Files\nodejs\node.exe
)proj.js
)arg1
)arg2
)arg3
)your actual arguments start form second index of argv
array, that is process.argv[2]
.
Answered 2023-09-20 20:22:05
const args = process.argv.slice(2).reduce((acc, arg) => {
let [k, v = true] = arg.split('=')
acc[k] = v
return acc
}, {})
for this command node index.js count=2 print debug=false msg=hi
console.log(args) // { count: '2', print: true, debug: 'false', msg: 'hi' }
we can change
let [k, v = true] = arg.split('=')
acc[k] = v
by (much longer)
let [k, v] = arg.split('=')
acc[k] = v === undefined ? true : /true|false/.test(v) ? v === 'true' : /[\d|\.]+/.test(v) ? Number(v) : v
to auto parse Boolean & Number
console.log(args) // { count: 2, print: true, debug: false, msg: 'hi' }
Answered 2023-09-20 20:22:05
Parsing argument based on standard input ( --key=value
)
const argv = (() => {
const arguments = {};
process.argv.slice(2).map( (element) => {
const matches = element.match( '--([a-zA-Z0-9]+)=(.*)');
if ( matches ){
arguments[matches[1]] = matches[2]
.replace(/^['"]/, '').replace(/['"]$/, '');
}
});
return arguments;
})();
Command example
node app.js --name=stackoverflow --id=10 another-argument --text="Hello World"
Result of argv: console.log(argv)
{
name: "stackoverflow",
id: "10",
text: "Hello World"
}
Answered 2023-09-20 20:22:05
In the node code require the built in process lib.
const {argv} = require('process')
Run the program with their arguments.
$ node process-args.js one two=three four
argv is the array that follows:
argv[0] = /usr/bin/node
argv[1] = /home/user/process-args.js
argv[2] = one
argv[3] = two=three
argv[4] = four
Answered 2023-09-20 20:22:05
Passing,parsing arguments is an easy process. Node provides you with the process.argv property, which is an array of strings, which are the arguments that were used when Node was invoked. The first entry of the array is the Node executable, and the second entry is the name of your script.
If you run script with below atguments
$ node args.js arg1 arg2
File : args.js
console.log(process.argv)
You will get array like
['node','args.js','arg1','arg2']
Answered 2023-09-20 20:22:05
npm install ps-grab
node greeting.js --user Abdennour --website http://abdennoor.com
--
var grab=require('ps-grab');
grab('--username') // return 'Abdennour'
grab('--action') // return 'http://abdennoor.com'
Or something like :
node vbox.js -OS redhat -VM template-12332 ;
--
var grab=require('ps-grab');
grab('-OS') // return 'redhat'
grab('-VM') // return 'template-12332'
Answered 2023-09-20 20:22:05
Nodejs team added util.parseArgs
function in versions 18.3.0 and 16.17.0. So if you use these or higher versions of nodejs you can parse command line arguments with this native solution.
An example of usage from the documentation:
const {parseArgs} = require('node:util');
const args = process.argv;
const options = {
foo: {
type: 'boolean',
short: 'f'
},
bar: {
type: 'string'
}
};
const {
values,
positionals
} = parseArgs({ args, options, allowPositionals: true });
console.log(values);
console.log(positionals);
Output sample:
$ node parseargs.js -f --bar b
[Object: null prototype] { foo: true, bar: 'b' }
[
'/Users/mbelsky/.nvm/versions/node/v18.12.1/bin/node',
'/Users/mbelsky/parseargs.js'
]
Answered 2023-09-20 20:22:05
You can parse all arguments and check if they exist.
file: parse-cli-arguments.js:
module.exports = function(requiredArguments){
var arguments = {};
for (var index = 0; index < process.argv.length; index++) {
var re = new RegExp('--([A-Za-z0-9_]+)=([A/-Za-z0-9_]+)'),
matches = re.exec(process.argv[index]);
if(matches !== null) {
arguments[matches[1]] = matches[2];
}
}
for (var index = 0; index < requiredArguments.length; index++) {
if (arguments[requiredArguments[index]] === undefined) {
throw(requiredArguments[index] + ' not defined. Please add the argument with --' + requiredArguments[index]);
}
}
return arguments;
}
Than just do:
var arguments = require('./parse-cli-arguments')(['foo', 'bar', 'xpto']);
Answered 2023-09-20 20:22:05
You can reach command line arguments using system.args
. And i use the solution below to parse arguments into an object, so i can get which one i want by name.
var system = require('system');
var args = {};
system.args.map(function(x){return x.split("=")})
.map(function(y){args[y[0]]=y[1]});
now you don't need to know the index of the argument. use it like args.whatever
Note: you should use named arguments like
file.js x=1 y=2
to use this solution.
Answered 2023-09-20 20:22:05
Passing arguments is easy, and receiving them is just a matter of reading the process.argv array Node makes accessible from everywhere, basically. But you're sure to want to read them as key/value pairs, so you'll need a piece to script to interpret it.
Joseph Merdrignac posted a beautiful one using reduce, but it relied on a key=value
syntax instead of -k value
and --key value
. I rewrote it much uglier and longer to use that second standard, and I'll post it as an answer because it wouldn't fit as a commentary. But it does get the job done.
const args = process.argv.slice(2).reduce((acc,arg,cur,arr)=>{
if(arg.match(/^--/)){
acc[arg.substring(2)] = true
acc['_lastkey'] = arg.substring(2)
} else
if(arg.match(/^-[^-]/)){
for(key of arg.substring(1).split('')){
acc[key] = true
acc['_lastkey'] = key
}
} else
if(acc['_lastkey']){
acc[acc['_lastkey']] = arg
delete acc['_lastkey']
} else
acc[arg] = true
if(cur==arr.length-1)
delete acc['_lastkey']
return acc
},{})
With this code a command node script.js alpha beta -charlie delta --echo foxtrot
would give you the following object
args = {
"alpha":true,
"beta":true,
"c":true,
"h":true,
"a":true,
"r":true
"l":true,
"i":true,
"e":"delta",
"echo":"foxtrot"
}
Answered 2023-09-20 20:22:05
Although Above answers are perfect, and someone has already suggested yargs, using the package is really easy. This is a nice package which makes passing arguments to command line really easy.
npm i yargs
const yargs = require("yargs");
const argv = yargs.argv;
console.log(argv);
Please visit https://yargs.js.org/ for more info.
Answered 2023-09-20 20:22:05
TypeScript solution with no libraries:
interface IParams {
[key: string]: string
}
function parseCliParams(): IParams {
const args: IParams = {};
const rawArgs = process.argv.slice(2, process.argv.length);
rawArgs.forEach((arg: string, index) => {
// Long arguments with '--' flags:
if (arg.slice(0, 2).includes('--')) {
const longArgKey = arg.slice(2, arg.length);
const longArgValue = rawArgs[index + 1]; // Next value, e.g.: --connection connection_name
args[longArgKey] = longArgValue;
}
// Shot arguments with '-' flags:
else if (arg.slice(0, 1).includes('-')) {
const longArgKey = arg.slice(1, arg.length);
const longArgValue = rawArgs[index + 1]; // Next value, e.g.: -c connection_name
args[longArgKey] = longArgValue;
}
});
return args;
}
const params = parseCliParams();
console.log('params: ', params);
Input: ts-node index.js -p param --parameter parameter
Output: { p: 'param ', parameter: 'parameter' }
Answered 2023-09-20 20:22:05
If you want to do this in vanilla JS/ES6 you can use the following solution
worked only in NodeJS > 6
const args = process.argv
.slice(2)
.map((val, i)=>{
let object = {};
let [regexForProp, regexForVal] = (() => [new RegExp('^(.+?)='), new RegExp('\=(.*)')] )();
let [prop, value] = (() => [regexForProp.exec(val), regexForVal.exec(val)] )();
if(!prop){
object[val] = true;
return object;
} else {
object[prop[1]] = value[1] ;
return object
}
})
.reduce((obj, item) => {
let prop = Object.keys(item)[0];
obj[prop] = item[prop];
return obj;
}, {});
And this command
node index.js host=http://google.com port=8080 production
will produce the following result
console.log(args);//{ host:'http://google.com',port:'8080',production:true }
console.log(args.host);//http://google.com
console.log(args.port);//8080
console.log(args.production);//true
p.s. Please correct the code in map and reduce function if you find more elegant solution, thanks ;)
Answered 2023-09-20 20:22:05
let args = process.argv.slice(2).reduce((acc, arg) => { let [k, v] = arg.split('=') acc[k] = v return acc }, {})
- anyone The simplest way of retrieving arguments in Node.js is via the process.argv array. This is a global object that you can use without importing any additional libraries to use it. You simply need to pass arguments to a Node.js application, just like we showed earlier, and these arguments can be accessed within the application via the process.argv array.
The first element of the process.argv array will always be a file system path pointing to the node executable. The second element is the name of the JavaScript file that is being executed. And the third element is the first argument that was actually passed by the user.
'use strict';
for (let j = 0; j < process.argv.length; j++) {
console.log(j + ' -> ' + (process.argv[j]));
}
All this script does is loop through the process.argv array and prints the indexes, along with the elements stored in those indexes. It's very useful for debugging if you ever question what arguments you're receiving, and in what order.
You can also use libraries like yargs for working with commnadline arguments.
Answered 2023-09-20 20:22:05
You can get command-line information from process.argv()
And I don't want to limit the problem to node.js
. Instead, I want to turn it into how to parse the string as the argument.
console.log(ArgumentParser(`--debug --msg="Hello World" --title="Test" --desc=demo -open --level=5 --MyFloat=3.14`))
output
{
"debug": true,
"msg": "Hello World",
"title": "Test",
"desc": "demo",
"open": true,
"level": 5,
"MyFloat": 3.14
}
Pure javascript, no dependencies needed
// 👇 Below is Test
(() => {
window.onload = () => {
const testArray = [
`--debug --msg="Hello World" --title="Test" --desc=demo -open --level=5 --MyFloat=3.14`,
]
for (const testData of testArray) {
try {
const obj = ArgumentParser(testData)
console.log(obj)
} catch (e) {
console.error(e.message)
}
}
}
})()
// 👇 Script
class ParserError extends Error {
}
function Cursor(str, pos) {
this.str = str
this.pos = pos
this.MoveRight = (step = 1) => {
this.pos += step
}
this.MoveToNextPara = () => {
const curStr = this.str.substring(this.pos)
const match = /^(?<all> *--?(?<name>[a-zA-Z_][a-zA-Z0-9_]*)(=(?<value>[^-]*))?)/g.exec(curStr) // https://regex101.com/r/k004Gv/2
if (match) {
let {groups: {all, name, value}} = match
if (value !== undefined) {
value = value.trim()
if (value.slice(0, 1) === '"') { // string
if (value.slice(-1) !== '"') {
throw new ParserError(`Parsing error: '"' expected`)
}
value = value.slice(1, -1)
} else { // number or string (without '"')
value = isNaN(Number(value)) ? String(value) : Number(value)
}
}
this.MoveRight(all.length)
return [name, value ?? true] // If the value is undefined, then set it as ture.
}
throw new ParserError(`illegal format detected. ${curStr}`)
}
}
function ArgumentParser(str) {
const obj = {}
const cursor = new Cursor(str, 0)
while (1) {
const [name, value] = cursor.MoveToNextPara()
obj[name] = value
if (cursor.pos === str.length) {
return obj
}
}
}
Answered 2023-09-20 20:22:05
NodeJS exposes a global variable called process
.
we can use:
process.argv
to get the command line arguments passes to our script.
The output of process.argv
will be a list in the following order:
[
full-path-to-node-executable,
full-path-to-the-script-file
...additonal-arguments-we-provide
]
Answered 2023-09-20 20:22:05