223

Is it any npm option exist to disable postinstall script while installing package? Or for rewriting any field from package.json?

BuZZ-dEE
  • 6,075
  • 12
  • 66
  • 96
farwayer
  • 3,872
  • 3
  • 21
  • 23
  • 2
    The question is very clear but...is by any chance _this_ your end-goal? https://github.com/zloirock/core-js#postinstall-message ;-) – conny Apr 11 '20 at 10:20

6 Answers6

412

It's not possible to disable only postinstall script. However, you can disable all scripts using:

$ npm install --ignore-scripts

As delbertooo mentioned in the comments, this also disables the scripts of the dependencies.

Yashu Mittal
  • 1,478
  • 3
  • 14
  • 32
Gergo Erdosi
  • 40,904
  • 21
  • 118
  • 94
  • 2
    Thx. This is exactly what I need. Where can I find such options? This is not described in 'npm help install', 'npm help config' and 'npm help scripts'. – farwayer May 06 '14 at 23:55
  • 3
    I checked the code directly: https://github.com/npm/npm/blob/master/lib/build.js#L179 – Gergo Erdosi May 06 '14 at 23:58
  • 112
    Caution! The option `--ignore-scripts` disables ALL scripts - even from your dependencies. If your dependencies need to run scripts to e.g. install some binaries they may break / be incomplete. – delbertooo Nov 12 '15 at 17:53
  • 10
    So is there a technique then to install node_modules (and all their scripts) but ignore only the main package's scripts ? – Derrick Nov 30 '17 at 14:41
  • 6
    Not only this, but `--ignore-scripts`, or the associated config option (which I set globally) will also mean that `npm run start` will silently do nothing and report success. Amaze. – Ash Berlin-Taylor Jan 24 '19 at 12:27
43

You can also enable the settings in npm configuration file.

npm config set ignore-scripts true

Note: This will disable scripts for all NPM packages.

RoboMex
  • 828
  • 12
  • 13
23

I wanted to disable postinstall script for my project but wanted all scripts of my project's dependencies to run when I do npm install. This is what I ended up doing.

  1. Create a script ./scripts/skip.js
if (process.env.SKIP_BUILD) {
    process.exit(0);
} else {
    process.exit(1);
}
  1. In your package.json file
 "scripts": {
  ...
  "postinstall": "node ./scripts/skip.js || npm run build",
  ...
 }

now just set the environment variable SKIP_BUILD=1 to prevent your package from building and your dependencies will build just fine

SKIP_BUILD=1 npm install
Atul
  • 2,170
  • 23
  • 24
  • 3
    Nice! I'd probably just do "postinstall": "node process.env.RUN_POSTINSTALL && npm run build" though – ihor.eth Dec 14 '20 at 21:23
  • I attempted the solution in the comment based on RUN_POSTINSTALL and I couldn't get it to work. The original answer worked for me. – Reece Daniels Dec 27 '22 at 17:34
13

To do this for your own library, I recommend something simple like:

#!/usr/bin/env bash

## this is your postinstall.sh script:

set -e;

if [ "$your_pkg_skip_postinstall" == "yes" ]; then
  echo "skipping your package's postinstall routine.";
  exit 0;
fi

then do your npm install with:

your_pkg_skip_postinstall="yes" npm install
Alexander Mills
  • 90,741
  • 139
  • 482
  • 817
  • 2
    Thanks! This works great if you're just trying to disable your own script but not ALL scripts in all dependencies. – Brian Neisler Nov 30 '18 at 23:07
  • 1
    @BrianNeisler yer very welcome, always feels good to help other people. By the way, I think double brackets [[ ]] might be better than single brackets for most use cases, not sure tho. – Alexander Mills Dec 01 '18 at 01:56
12

If you're using NPM >= 8, you can also remove the postinstall script temporarily:

npm pkg set scripts.postinstall="echo no-postinstall"
npm install

Or with NPM 7, like so:

npm set-script postinstall ""
npm install

Source: https://docs.npmjs.com/cli/v7/commands/npm-set-script/

mesqueeb
  • 5,277
  • 5
  • 44
  • 77
Vahid
  • 6,639
  • 5
  • 37
  • 61
0

Define .npmrc and set ignore-scripts=true.

brillout
  • 7,804
  • 11
  • 72
  • 84