是否存在任何npm选项来禁用postinstall脚本,同时安装包?或者从package.json重写任何字段?


当前回答

定义.npmrc并设置ignore-scripts=true。

其他回答

定义.npmrc并设置ignore-scripts=true。

不可能只禁用postinstall脚本。然而,你可以禁用所有脚本使用:

$ npm install --ignore-scripts

正如delbertooo在评论中提到的,这也会禁用依赖项的脚本。

对于你自己的库,我推荐一些简单的方法:

#!/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

然后用以下方法安装NPM:

your_pkg_skip_postinstall="yes" npm install

如果你正在使用NPM >= 8,你也可以暂时删除postinstall脚本:

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

或者在NPM 7中,像这样:

npm set-script postinstall ""
npm install

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

你也可以在npm配置文件中启用设置。

NPM配置设置ignore-scripts为true

注意:这将禁用所有NPM包的脚本。