node.js npm:禁用包的安装后脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23505318/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 17:17:35  来源:igfitidea点击:

npm: disable postinstall script for package

node.jsnpm

提问by farwayer

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

在安装软件包时是否有任何 npm 选项可以禁用 postinstall 脚本?或者重写 package.json 中的任何字段?

回答by Gergo Erdosi

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

不可能只禁用postinstall脚本。但是,您可以使用以下方法禁用所有脚本:

$ npm install --ignore-scripts

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

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

回答by RoboMex

You can also enable the settings in npm configuration file.

您还可以启用 npm 配置文件中的设置。

npm config set ignore-scripts true

npm config set ignore-scripts true

Note: This will disable scripts for all NPM packages.

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

回答by Alexander Mills

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:

然后使用以下命令进行 npm 安装:

your_pkg_skip_postinstall="yes" npm install

回答by Atul

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.

我想为我的项目禁用安装后脚本,但希望我的项目依赖项的所有脚本在我执行npm install. 这就是我最终要做的。

  1. Create a script ./scripts/skip.js
  1. 创建脚本 ./scripts/skip.js
if (process.env.SKIP_BUILD) {
    process.exit(0);
} else {
    process.exit(1);
}
  1. In your package.json file
  1. 在你的 package.json 文件中
 "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 以防止您的包构建,您的依赖项将构建得很好

SKIP_BUILD=1 npm install