1

I remember using something before in node.js that would allow me to run a command like

node appname.js text goes here

and then read the "text goes here" part with something like

console.log(console.text)

I can't remember what it is, and can't find it in any searches. Was this a real thing, or just me dreaming?

2 Answers 2

2

Well there is lot's ways/packages around for reading from arguments.

the nodejs process is the base of it so check here

And also as i said lot's of packages there for parsing arguments.

yargs is one of them, minimist is also a populer one as far as i know.

If you don't want t use a package basicly it starts like this:

// inside node file

const args = process.argv.splice(2);
console.log(args);
// we are splice'ing from 2 cause 
// process.argv[0] is your node-path
// process.argv[1] is your js file's full path
// Most of the time we are not using those so :)

So hope these would work for you ☺

Sign up to request clarification or add additional context in comments.

Comments

1

You can use process.argv to console the input from command line.

If you run below command in terminal/command line:

node appname.js text goes here.

You can print the command line arguments by:

console.log(process.argv)

Output of above console will be:

['node',
'/home/user/path/to/appname.js',
'text',
'goes',
'here' ]

If you dont want first two text, you can use:

console.log(process.argv.slice(2))

Output of above console will be:

['text',
'goes',
'here' ]

Read this link for more info.

Hope this helps you out!!!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.