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/