2

Let's say I have in my package.json:

{
  "scripts": {
    "prebuild": "some-command",
    "build": "some-other-command"
  }
}

If I now run npm run build -- --some-args, the prebuild command some-command does not get the --some-args passed to it. How can I pass this to all the pre/post scripts as well?

Kousha
  • 32,871
  • 51
  • 172
  • 296

1 Answers1

0

You might be able to do it using environment variable:

"scripts": {                                                                  
    "prebuild": "echo $FLAG \"prebuild\"",                                
    "build": "echo \"testing\""                                                     
}

If you run npm run build:


> test@1.0.0 prebuild /test
> echo $FLAG "prebuild"

prebuild

> test@1.0.0 build /test
> echo "testing"

testing

And if you run FLAG=-n npm run build:

> test@1.0.0 prebuild /test
> echo $FLAG "prebuild"

prebuild
> test@1.0.0 build /test
> echo "testing"

testing

However I'm not sure if it will work with multiple flags

Mickael B.
  • 4,755
  • 4
  • 24
  • 48
  • 1
    You don't need to actually do the `echo $FLAG` environment variables are passed to pre/post scripts – Kousha Apr 29 '20 at 21:11