5

Is there a way to override package.json scripts? I can't change package.json because it will change it for everyone. i.e in our package we have

"script": {
    "dev": "yarn dev:build" 
}

I would like to add extra memory for this step as it keeps crashing on my computer. i.e

"scripts":{
    "dev": "\"node --max-old-space-size=9000 yarn dev:build\""
}
Yasin Yaqoobi
  • 1,888
  • 3
  • 27
  • 38
  • Create one more script, like `dev:yasin` and specify it with the arguments. – 31piy Mar 26 '18 at 16:11
  • 1
    I can't change the original package.json because it will change this file for everyone. – Yasin Yaqoobi Mar 26 '18 at 16:26
  • it's ok that it will change the `package.json` for everyone, because only you and whoever else might need extra memory will be running that second script (see the @ChetanJadhavCD's answer). – i-- Mar 26 '18 at 20:31
  • Another option is to fix you `yarn` command to something else. For example, you could create `~/bin/yarn` with bash shebang and `node --max-old-space-size=9000 /path/to/yarn "$*"`. That way all commands that execute `yarn` from PATH will get the improved memory settings. – Mikko Rantalainen Sep 29 '20 at 13:16

2 Answers2

1

You can't "override" package.json because the filename is hardcoded in NPM. You can create another script entry like:

"scripts":{
    "dev": "yarn dev:build" 
    "devlocal": "\"node --max-old-space-size=9000 yarn dev:build\""
}

Exclude package.json while committing to whatever SCM you are using, and the modified file would remain local to your machine only.

Having said that, what you are asking for (having another file to do the work of package.json), is not possible.

Chetan Jadhav CD
  • 1,116
  • 8
  • 14
1

It's not technically overriding a script, but you can execute what would be a script without actually adding it to package.json by calling npm exec in your terminal:

npm exec --max-old-space-size=9000 yarn dev:build
Faust
  • 15,130
  • 9
  • 54
  • 111
  • Why installing `npx` when it (a) already comes together with npm by default and (b) it is now recommended to use `npm exec` instead of `npx`? https://stackoverflow.com/a/66604542/3233827 – ssc-hrep3 Jan 22 '22 at 19:41
  • 1
    ssc-hrep3, updated my asnwer. – Faust Jan 23 '22 at 02:52