How to pass command line arguments to Node.js and get them in code?
Hello everyone! I'm new to Node.js and trying to write a simple utility that takes a username and greets them. I run the script via terminal like this: node app.js Ivan. But how do I get this name inside the code? I tried reading the documentation, but it's complicated.\n\nCurrently I have this code:\n\n```javascript\n// app.js\nconsole.log("Hello, world!");\n```\n\nI want that when running `node app.js Ivan` it outputs "Hello, Ivan!". If I run without arguments, it should output "Hello, guest!".\n\nAlso I'm interested in how to pass multiple arguments, for example, name and age: `node app.js Ivan 25`. How to separate and process them? And what to do if an argument contains spaces, for example, a full name "Ivan Petrov"? I can put it in quotes in the command line, but how to properly parse it?
No answers yet.
Log in to leave an answer