5

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

这是我写的一个小钩子脚本:

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”)似乎没有作为我的可执行挂钩的参数提供。

有没有办法从钩子中访问这些信息?

4

1 回答 1

4

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

#!/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);
于 2017-04-26T19:17:44.177 回答