Passing arguments to npm scripts


Passing arguments to a Node.js script is pretty straightforward:

// index.js
console.log(process.argv.slice(2));
node index.js --arg
[ '--arg' ]

But how do we pass our arguments to an npm script?

Given the following package.json:

{
  "scripts": {
    "main": "node index.js"
  }
}

We pass our arguments after the end of options delimiter (--):

npm run main -- --arg
[ '--arg' ]

This holds true even for nested npm scripts:

{
  "scripts": {
    "main": "node index.js",
    "nested": "npm run main"
  }
}
npm run nested -- -- --arg
[ '--arg' ]


Please support this site and join our Discord!