简体   繁体   中英

command line arguments to Node.js

I've read several related questions here but still can't figure out what I'm doing wrong. Here's my script (simple.js):

    var x = process.argv[2]
    console.log(x);

now when I type in the REPL

    .load simple.js Hello

the REPL answers

 Failed to load:simple.js Hello

but I've done this (simple2.js):

    console.log(process.argv);

and when I .load this file in REPL it answers

    > console.log(process.argv);
    [ 'C:\\Program Files\\nodejs\\node.exe' ]
    undefined

You can't pass command line args to .load . The second case didn't fail because you didn't pass one.

If you want to use cmdline args, just use the OS terminal and enter node simple.js Hello .

.load inside the Node REPL just loads a file. From the REPL docs it looks like the primary use case of .load is to restore a REPL session that was saved using .save .

In your succesful example its simply showing what process.argv is set to when you ran node in your terminal

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM