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.

Sign up to request clarification or add additional context in comments.

Comments

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 Comments

@FarazShuja are you using Linux/macOS?
what happens if you run following command TEST=foo && echo $TEST?
I am using git shell on windows 10, the command output foo for me.
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?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.