How to get custom parameters of npm script

such as

 "scripts": {
    "dev": "webpack-dev-server --open  --abc=11111111"
  },

how do I get the value of this abc in JS

Nov.07,2021

webpack supports export in the configuration file webpack.config.js , which accepts two parameters

  • env : environment
  • argv : parameter

all the parameters you pass in are in argv , so you can do this

.
// webpack.config.js
module.exports = (env, argv) => {
    let abc=argv.abc
    return {
    // ...webpack config
    }
}

then start it like this

webpack-dev-server --open  --abc=11111111

if you want to use it in your code, you can use DefinePlugin

.
new webpack.DefinePlugin({
            ABC            : JSON.stringify(abc)
        })

in code

if (ABC==='11111111'){
    alert(abc)
}

const json = require('../../package.json')
console.log(json.scripts)
Menu