Might be a duplicate but is there a way to set variables from an npm script that are accessible in javascript via something like process.env.CUSTOM_VAR?
In my package.json in the scripts section I've added "start": "react-native start --reset-cache" which works fine but now I'd like to do something like this "inspect": "INSPECT=1 react-native start --reset-cache" and have it accessible in javascript. I've googled around everything seems to point to adding it like above but it doesn't seem to be working.
Any help is appreciated!
You can have a program to read the process.argv and do things based on that list(process.argv includes the flags that the script started on)
argvToEnv.js
//you can set the prefix to your liking
const prefix="*" //the start script may have many flags
//if any flag starts with a prefix, it will turn it into an env variable
let envList={} //soon to be list of things env variables
for(let i=0;i<process.argv.length;i++){
let flag=process.argv[i]
if(flag.startsWith(prefix)){
envList[process.argv[i++].substr(prefix.length)]=process.argv[i]
}
}
Object.keys(envList).forEach(key=>{
process.env[key]=envList[key]
})
index.js
require('path/to/argvToEnv.js')
//whatever else you do in your main js file
start script
"start": "react-native start --reset-cache *CUSTOM_VAR \"success hehe\""