'How to pass file file path as arguement to the script?

I want to send the data from

package.json:

 "scripts": {
    "start": "node list.js",
}

list.js:

const serviceAccount = require(path);

when I execute command npm start ./style.json I wanted to pass the path to to path variable in list.js

How can I do that?



Solution 1:[1]

Basically, you need to use process.argv, the third item is ./style.json. inside list.js use this:

const path = process.argv[2]
const serviceAccount = require(path);
console.log(serviceAccount)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 sina.ce