0

*package.json**

{
  "name": "test-project",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "node build build_name"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {

  }
}

build.js

console.log("Building Code. Output File Name:", process.env.build_name);

Command Line

$ npm run build build_name="web"

I want to pass a parameter build_name from the command line while executing build script. I will use that param inside my build script. Can someone tell me how to achieve this? Also if I did not pass build_name from the command line, can we send a default value from here "build": "node build build_name" instead of build_name.

lch
  • 4,569
  • 13
  • 42
  • 75

1 Answers1

2

Use the "yargs" module.

Step 1:

 npm install yargs

Step 2:

 vi test.js

press i and copy the below code and paste it.

'use strict';

const args = require('yargs').argv;

console.log('Name: ' + args.name);  
console.log('Age: ' + args.age); 

Step 3 : Execution

node test.js --name=jacob --age=45

Step 4: output

Name: jacob
Age: 45

Let me know if it helps.