Here is some code.
I have tried using -- and passing as an env variable i.e. --script=myscript.js. The code I have linked is getting me very close but I need to remove the space between the script name.
"scripts": {
"script": "nodemon --exec babel-node ./scripts/${*}",
}
then I run in console:
npm run script populateVehicleData.js.
That results in this
[nodemon] 1.18.7
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `babel-node ./scripts/ populateVehicleData.js`
internal/modules/cjs/loader.js:657
throw err;
^
Error: Cannot find module '/Users/jakeneels/work/api/scripts/'
Notice that the script is executing 'babel-node ./scripts/ populateVehicleData.js'. Why is the space there? how do I get rid of it?
I expect to execute babel-node ./scripts/populateVehicleData.js and run my script whatever its name might be. instead i get babel-node ./scripts/ populateVehicleData.js causing npm to not find the file due to the space between scripts/ and populateVehicleData.js.
${1}to reference the filename (i.e. the first positional parameter/arg) passed. For instance change your npm script to:"script": "func() { nodemon --exec babel-node ./scripts/${1}; }; func",- then run following command:npm run script populateVehicleData.js.Or runnpm run script -- populateVehicleData.js.(Note the double hyphens--used in 2nd invocation, i.e.getoptto delimit end of options)."script": "func() { nodemon --exec babel-node ./scripts/${1:-somefile.js}; }; func",- Now if you runnpm run script(i.e. without a filename argument) it will default to./scripts/somefile.js. However, if you runnpm run script populateVehicleData.jsit will use the file at./scripts/populateVehicleData.jsinstead.