Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass parameter when executing a node.js script? [duplicate]

I wish to send some parameters when running a node script, using for instance

node myscript.js

from the terminal, so it can be read as below:

const fs = require('fs');

console.log(myParameter);
like image 664
Lothurius Mobile Developer Avatar asked Apr 28 '26 23:04

Lothurius Mobile Developer


1 Answers

You just need to call as many arguments as you want, like:

node myscript.js arg1 arg2 arg3

And you recover them like:

console.log(process.argv[2]);
like image 139
Rogério Pires Avatar answered Apr 30 '26 14:04

Rogério Pires



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!