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.

1

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.