How to add paramater with npm scripts in package.json (NodeJS)

481 views Asked by At

I want to create a file in given folder from terminal using npm script. I know it works like adding custom script in scripts object of package.json file like:

"scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "nodemon app.js",
    "addstyle": "touch ./css/stylesheet.css"
},

and then run npm run addstyle from terminal.


But what if I want to pass custom name for my stylesheet from terminal?

I got few options like adding $variable in script:

"scripts": {
    ...
    "addstyle": "touch ./css/$filename.css"
},

and then add filename before the command i.e. filename=homepage npm run addstyle


Is there anyway, I can append a parameter in the end with - or -- like:
npm run addstyle --filename=homepage
. OR
npm run addstyle -filename homepage

1

There are 1 answers

1
Pushpendra Kumar On

Run this command and assign the $FILE_NAME

$FILE_NAME = 'homepage' npm run addstyle

"scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "nodemon app.js",
    "addstyle": "touch ./css/$FILE_NAME.css"
}