npm - 如何从 npm "postinstall" Hook 获取包名称?

标签 npm hook npm-install npm-scripts

Npm 提供了一种在安装每个包后执行自定义可执行文件或脚本的方法(请参阅 Hook Scripts )。

这是我编写的一个小钩子(Hook)脚本:

hook-test-npm/node_modules/.hooks/postinstall

#!/usr/bin/env node
console.log("postinstall...  " + process.argv.join("  "));

然后我以通常的方式安装了一个软件包:

$ npm install --save some-package

但是结果并不像我希望的那样:

> some-package@1.0.0 postinstall /Users/macuser/Desktop/hook-test-npm/node_modules/some-package
> /Users/macuser/Desktop/hook-test-npm/node_modules/.hooks/postinstall
postinstall...  /usr/local/bin/node  /Users/macuser/Desktop/hook-test-npm/node_modules/.hooks/postinstall

刚刚安装的包的名称(“some-package”)似乎没有作为参数提供给我的可执行 Hook 。

有没有办法从钩子(Hook)内访问这些信息?

最佳答案

经过进一步的实验,我发现了以下两个环境变量,它们似乎包含我正在寻找的信息。我不知道这些是否应该直接使用;但他们肯定会暂时为我解决问题:

#!/usr/bin/env node

console.log("postinstall...");

// Print out the name of the package that was just installed.
console.log("    " + process.env.npm_package_name);

// Print out the directory of the package that was just installed.
console.log("    " + process.env.PWD);

关于npm - 如何从 npm "postinstall" Hook 获取包名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43629596/

相关文章:

Python,如何设置钩子(Hook)来跟踪 I/O 事件

npm 下载多个版本的包

json - 有没有办法可以阻止默认图标出现并阻止它被单击?

npm - 运行 npm install 时的 mocha 和 chai 依赖项

node.js - 无法为 Node.js 安装 clean-webpack-plugin

python - openpyxl 不能作为 EXE 运行

c - 如何在较新的 Linux 内核中挂接 sys_clone

node.js - 请求 https ://registry. npmjs.org/co 失败

npm 安装失败 -- 错误消息 : enoent ENOENT: no such file or directory

laravel - npm run watch 和 npm run watch-poll 之间的区别