'package.json - how to set default value of variable in windows

In Linux you can use the following syntax to set the default value of a variable in the package.json file: try=${myEnvVariable:"defaultValue"}

How do you do this using the syntax for window %varaibelName% ? This does not work: try=%myEnvVariable:"defaultValue"%



Solution 1:[1]

You can define a script in the scripts section of the package.json like this

"scripts": {
  "set-myenv": "npm config set myEnvVariable <yourDefuaultValue>"
}

To use this in another script, you can chain scripts with &&

"scripts": {
  "set-myenv-and-execute": "npm config set myEnvVariable <yourDefuaultValue> && another command --some-option %npm_config_myEnvVariable%"
}

Here, the second script (after &&) has the --some-option option set with the default value of myEnvVariable through %npm_config_myEnvVariable%

Then, you can execute this script in the command line either by providing a value to myEnvVariable

> npm run set-myenv-and-execute --myEnvVariable=someValue

or without providing a value

> npm run set-myenv-and-execute

Here it uses the default value.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Nadun Liyanage