1

How I can pass opts/flags which I pass to npm install command to postinstall scripts?

I write npm install X --some-param=some-value command. X package has postinstall script: ./scripts/postinstall.js. How I can pass some-param to postinstall script?

I try process.argv but it does not contain anything which I use as npm install params.

Sharikov Vladislav
  • 6,566
  • 5
  • 44
  • 84

2 Answers2

0

I don't think there's a way to do that, since npm install assumes all arguments it doesn't recognize are package names. What works, and is a common solution, is to use environment variables instead. When you do this:

$ SOME_PARAM=some-value npm install

...you can then access the value of SOME_PARAM with process.env.SOME_PARAM in your postinstall.js script.

Jordan Running
  • 97,653
  • 15
  • 175
  • 173
0

You can access the args with process.env.npm_config_argv.

Running yarn install --test-arg will give a value of {"remain":[],"cooked":["install"],"original":["install","--test-arg"]}.

amcquaid
  • 439
  • 2
  • 8