0

I have the following functionality that locally loads some products out of stock depending on the command run in the terminal:

const updates = process.env.option === "oos" ? oosUpdates : insUpdates
log.info("updates: ", JSON.stringify(updates))
const result = await loadUpdateVariants(cache, updates as UpdateType[])

In my package.json I have:

"update:oos": "ts-node src/loaders/mock/update-variants.ts --option oos",
"update:ins": "ts-node src/loaders/mock/update-variants.ts --option ins"

however when I run yarn update:oos I am getting the instock products meaning that this process.env.option === "oos" is not working out correctly.

Aessandro
  • 5,517
  • 21
  • 66
  • 139
  • Does this answer your question? [Sending command line arguments to npm script](https://stackoverflow.com/questions/11580961/sending-command-line-arguments-to-npm-script) – callmemath Apr 01 '21 at 09:53
  • Doesn't `--option` go on `process.argv` and not `process.env`? – evolutionxbox Apr 01 '21 at 10:13

1 Answers1

1

process.env gives you access to environment variables. If you want to use environment variables, you can set them like this:

"update:oos": "OPTION=oos ts-node src/loaders/mock/update-variants.ts",
"update:ins": "OPTION=ins ts-node src/loaders/mock/update-variants.ts"

This is the "vanilla" (without any extra dependencies) approach - but may encounter issues on Windows. If you want your command to work well cross-platform, you might consider using cross-env

Bill
  • 3,478
  • 23
  • 42
  • 2
    Don't set environment variables like that, it isn't supported properly cross platform. Use `cross-env` instead. – Quentin Apr 01 '21 at 09:57
  • Good point, thanks, I have added in a note to the answer about that. – Bill Apr 01 '21 at 10:00