覆盖 NPM 项目的 `npm install` 脚本 [英] Override `npm install` script for NPM project

查看:107
本文介绍了覆盖 NPM 项目的 `npm install` 脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 NPM 项目,当 npm install 运行时,我想运行一个自定义脚本.

I have an NPM project, when npm install is run, I'd like to run a custom script.

我尝试在 package.json 中使用它:

I tried using this in package.json:

"scripts": {
    "ng": "ng",
    "start": "ng serve",
    "install": "./scripts/install.sh",   // <<<<
 },

但这实际上只是导致了一个无限循环.我寻找这个的原因是因为有些工具只是调用 npm install,所以我无法控制它们运行的​​内容.否则,如果我有控制权,我只会自己调用 ./scripts/install.sh.

but that actually just resulted in an infinite loop. The reason I am looking for this, is because there are tools that simply call npm install, so I can't control what they run. Otherwise, if I had control, I would just call ./scripts/install.sh myself instead.

请注意,这可能不是最好的主意,只是好奇是否可行.

Note that this is probably not the best idea, just curious if it's possible.

注意我的安装脚本如下所示:

Note my install script looks something like this:

#!/usr/bin/env bash

export FOO="bar";
export PUPPETEER_SKIP_CHROMIUM_DOWNLOAD="true";

npm install

推荐答案

使用 preinstallnpm install 之前运行代码.不要尝试以这种方式覆盖 npm install,否则您最终会无限循环调用 npm install.

Use preinstall to run code before npm install. Don't try to override npm install in this fashion where you would end up with an infinite loop of calls to npm install.

您还可以使用 package.jsonconfig 属性设置环境变量.请参阅文档了解详情

You can also set environment variables using the config property of package.json. See docs for details

这篇关于覆盖 NPM 项目的 `npm install` 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆