Node安装钩子允许在安装新npm模块(包括节点模块和外部模块)时添加自定义代码。
preInstall
在运行npm install
安装npm模块之前调用。
该钩子接收一个InstallEvent
对象,其中包含有关待安装模块的信息。
{
"module": "<npm module name>",
"version": "<version to be installed>",
"url": "<optional url to install from>",
"dir": "<directory to run the install in>",
"isExisting": "<boolean> this is a module we already know about",
"isUpgrade": "<boolean> this is an upgrade rather than new install",
"args": [ "an array", "of the args", "that will be passed to npm"]
}
该钩子可以修改InstallEvent来改变npm的运行方式。例如,
可以修改args
数组来改变传递给npm
的参数。
如果钩子返回false
,将跳过npm install
步骤,并继续执行后续流程,就像已经运行过安装一样。这允许使用某些替代机制——只要最终结果是将模块安装到预期的node_modules
目录下即可。
如果钩子抛出错误,安装将干净利落地失败。
RED.hooks.add("preInstall", (installEvent) => {
console.log(`About to install ${installEvent.module}@${installEvent.version}`);
});
postInstall
在npm install
完成安装npm模块后调用。
注意 如果 preInstall
钩子返回了 false
,则不会运行 npm install
,但这个钩子仍会被调用。
该钩子可用于运行任何所需的安装后活动。
例如,在Electron环境中运行时,需要重新构建模块:
RED.hooks.add("postInstall", (installEvent, done) => {
child_process.exec("npm run rebuild " + installEvent.module,
{cwd: installEvent.dir},
(err, stdout, stderr) => {
done();
}
);
});
如果钩子抛出错误,安装将干净利落地失败。
如果前面的npm install
返回了错误,则不会调用此钩子。
preUninstall
在运行npm remove
卸载npm模块之前调用。
该钩子函数接收一个UninstallEvent
对象,其中包含有关待移除模块的信息。
{
"module": "<npm module name>",
"dir": "<directory to run the remove in>",
"args": [ "an array", "of the args" , "we will pass to npm"]
}
该钩子可以修改UninstallEvent来改变npm的运行方式。例如,
可以修改args
数组来改变传递给npm
的参数。
如果钩子返回false
,则会跳过npm remove
操作,流程会继续执行,就像已经运行过该操作一样。这允许使用某些替代机制。
如果钩子抛出错误,卸载将干净利落地失败。
RED.hooks.add("preUninstall", (uninstallEvent) => {
console.log(`About to remove ${uninstallEvent.module}`);
});
postUninstall
在npm remove
完成移除npm模块后调用。
注意 如果 preUninstall
钩子返回了 false
,则不会执行 npm remove
命令,但这个钩子仍会被调用。
该钩子可用于运行任何所需的卸载后活动。
如果钩子抛出错误,它将被记录,但卸载仍会顺利完成,因为在npm remove
完成后我们无法回滚操作。
RED.hooks.add("postUninstall", (uninstallEvent) => {
console.log(`Removed ${uninstallEvent.module}`);
});
InstallEvent
对象{
"module": "<npm module name>",
"version": "<version to be installed>",
"url": "<optional url to install from>",
"dir": "<directory to run the install in>",
"isExisting": "<boolean> this is a module we already know about",
"isUpgrade": "<boolean> this is an upgrade rather than new install",
"args": [ "an array", "of the args", "we will pass to npm"]
}
UninstallEvent
对象{
"module": "<npm module name>",
"dir": "<directory to run the remove in>",
"args": [ "an array", "of the args", "we will pass to npm"]
}
Node-RED: 面向事件驱动应用的低代码编程平台。
版权所有 OpenJS基金会 及 Node-RED 贡献者。保留所有权利。OpenJS基金会 拥有注册商标并使用商标。有关 OpenJS基金会 的商标列表,请参阅我们的 商标政策 和 商标列表。未在 OpenJS基金会商标列表 中标注的商标™或注册®商标归其各自持有人所有。使用这些商标并不意味着与它们有任何关联或获得其认可。
The OpenJS Foundation | 使用条款 | 隐私政策 | OpenJS基金会章程 | 商标政策 | 商标列表 | Cookie政策