2

I have npm script defined like this

"scripts": {
    "compress": "uglifyjs src/script.js -o src/script.js"
}

which I can run by the command npm run compress.

I want to make the name of the base folder i.e. 'src' dynamic, so I can pass the base folder name as param while running the script, something like

npm run compress --base_folder=src

to run script

"compress": "uglifyjs ${base_folder}/script.js -o ${base_folder}/script.js"
FarazShuja
  • 2,287
  • 2
  • 23
  • 34
  • Possible duplicate of [Sending command line arguments to npm script](http://stackoverflow.com/questions/11580961/sending-command-line-arguments-to-npm-script) – mic4ael Sep 05 '16 at 12:24
  • I checked that question, but I want to embed a param as string to my command in scripts – FarazShuja Sep 05 '16 at 12:25
  • See the answer under http://stackoverflow.com/questions/35221098/passing-arguments-to-npm-script-in-package-json – aleung Oct 22 '16 at 04:39

2 Answers2

1

You need npm-config. Use $npm_config_base_folder in package.json:

"scripts": {
    "compress": "uglifyjs $npm_config_base_folder/script.js -o $npm_config_base_folder/script.js"
}

Then execute npm run compress --base_folder=src.

hankchiutw
  • 1,546
  • 1
  • 12
  • 15
0

The best option will be to use environment variables because npm as far as now doesn't support such complicated logic. In other hand, environment variables offer similar functionality.

command:

base_folder=src npm run compress

package.json:

"compress": "uglifyjs $base_folder/script.js -o $base_folder/script.js"
G07cha
  • 4,009
  • 2
  • 22
  • 39