npm: disable postinstall script for package

node.jsNpm

node.js Problem Overview


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

node.js Solutions


Solution 1 - node.js

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

$ npm install --ignore-scripts

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

Solution 2 - node.js

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.

Solution 3 - node.js

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

Solution 4 - node.js

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

Solution 5 - node.js

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

npm set-script postinstall ""
npm install

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionfarwayerView Question on Stackoverflow
Solution 1 - node.jsGergo ErdosiView Answer on Stackoverflow
Solution 2 - node.jsRoboMexView Answer on Stackoverflow
Solution 3 - node.jsAtulView Answer on Stackoverflow
Solution 4 - node.jsAlexander MillsView Answer on Stackoverflow
Solution 5 - node.jsVahidView Answer on Stackoverflow