Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

process.argv is undefined in node.js

Tags:

node.js

I can't seem to be running the most trivial of scripts in node:

Platform: windows 10

node -v: v10.7.0

file: "sample.js"

process.argv.forEach((val, index) => {
    console.log(`${index}: ${val}`);
  });

command line:

D:\temp>node sample.js 1 2 3
D:\temp\sample.js:4
process.argv.forEach((val, index) => {
             ^

TypeError: Cannot read property 'forEach' of undefined
    at Object.<anonymous> (D:\temp\sample.js:4:14)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
    at startup (internal/bootstrap/node.js:266:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:596:3)
like image 500
Jeff Saremi Avatar asked Oct 14 '18 22:10

Jeff Saremi


People also ask

What is process argv in node JS?

The process. argv() method is used for returning all the command-line arguments that were passed when the Node. js process was being launched. The first element will always contains the same value as process. execPath.

How do you handle uncaught exception in node JS?

The 'uncaughtException' is an event of class Process within the process module which is emitted when an uncaught JavaScript exception bubbles all the way back to the event loop. Parameters: This event does not accept any argument as a parameter.

How do I pause a NodeJS process?

Simpler, you can call process. kill('SIGSTOP') to pause the process and and process. kill('SIGCONT') to resume it.


1 Answers

Solution

I figured out, that I defined the function process below, that's why process object was overridden by my own.

So, to resolve:

  1. Check that you are not overriding "process" object with a function

  2. Put it initialization higher line by line and find what code is overriding it

Old suggestion:

In my case, it was "redefined" even if I put it on the first line. "Process" was a function.

To retrieve Object I used explicit call:

const process = require('process');

like image 196
quento Avatar answered Oct 01 '22 22:10

quento