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" 
3

2 Answers 2

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.

0
    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" 
    4
    • @FarazShuja are you using Linux/macOS?
      – G07cha
      CommentedSep 5, 2016 at 18:09
    • what happens if you run following command TEST=foo && echo $TEST?
      – G07cha
      CommentedSep 5, 2016 at 18:15
    • I am using git shell on windows 10, the command output foo for me.CommentedSep 5, 2016 at 18:40
    • So seems like enviroment variables is working fine for you, maybe you really missing file $base_folder/script.js where $base_file is replaced with value that you're using?
      – G07cha
      CommentedSep 5, 2016 at 18:49

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.