0

I'm trying to print out the command line arguments in a React Component.

NPM docs say I can using..

console.log(process.argv);

However, this doesn't print anything.

Array(0)

Above is what it prints in console. I'm calling my program with ...

npm start

where start is npm electron . ok

Can yargs print the command line arguments in a react component? Should I be trying to use yargs? I can't find any documentation on how to do it with just react. I know electron can only print it out in the main.js and the remote process will not work in a react component.

user3622460
  • 1,231
  • 5
  • 23
  • 42

2 Answers2

0

React is client side, right? While console.log() is server side ... so console.log() won't get anything into your react content (unless you rewrite it).

Electron does give you access to your node execution context (so you can say var path = require("path") and have access to the path activities).

So you might have access to your yargs output as a page-level variable. Otherwise, you can build a module that you can require().

Another way to do it is to have react issue an request to get the yargs output and treat it like you would any other data.

I'm sure there are other solutions.

I have some sample code doing something similar but using Angular here: https://github.com/Earl-Brown/N-Dash

theGleep
  • 1,179
  • 8
  • 14
  • I tried the require() line but I keep getting errors where the path or module can't be found. I even tried adding a preset "electron" in webpack so I could import electron into the client side components, but that didn't work as well. I'll take a look at this example and see if I can figure anything out that works. Thanks – user3622460 Oct 06 '17 at 15:38
  • Wasn't really able to follow where the program was doing something similar. You would think this is possible, but I've been trying to find a solution to this for a few days now and haven't came across anyone that has been able to grab a command line argument with a react component yet. – user3622460 Oct 06 '17 at 15:48
0

First you need to run command line

npm start -- test

test is your agrument. Then you can print it in your script

// print process.argv
process.argv.forEach(function (val, index, array) {
  console.log(index + ': ' + val);
});
Dharman
  • 30,962
  • 25
  • 85
  • 135
Piseth Sok
  • 1,789
  • 1
  • 20
  • 24