Home > OS >  add parameters to node script
add parameters to node script

Time:03-24

I'm trying to add some custom scripts to package.json and I want to pass filepaths as parameters to the scripts for example i have this script

    "captures": "node ./interfaceVisualTesting/captures.js",

which requires json files:

const repjsonref = require('../json/report1.json');

I want my script to be like this

"captures": "node ./interfaceVisualTesting/captures.js ../json/report1.json

and to require the parameter in the javascript file how can i do that ?

EDIT

I realized that i didn't ask the right question what i meant is i want the path to be passed as a parameter while writing the script like this

`npm node-script captures <path of json>`

CodePudding user response:

You could pass the path value as an environment variable

package.json

"captures": "REPORT_PATH='../json/report1.json' node ./interfaceVisualTesting/captures.js",
const repjsonref = require(process.env.REPORT_PATH);

CodePudding user response:

I figured out how to add variables to the node script in this question How do I pass command line arguments to a Node.js program? but all i had to do was add the variables in the script when it was called

npm run-script captures <path1> <path2>

and then add them to the javascript like this

x=require(process.argv[2]);//path1
y=require(process.argv[3]);//path2
  • Related