Npm provides a way to execute a custom executable or script after each package is installed (see Hook Scripts).
Here is a little hook script that I wrote:
hook-test-npm/node_modules/.hooks/postinstall
#!/usr/bin/env node
console.log("postinstall... " + process.argv.join(" "));
I then installed a package in the usual way:
$ npm install --save some-package
However the results weren't quite as I had hoped:
> [email protected] 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
The name of the package that was just installed ("some-package") does not seem to be provided as an argument to my executable hook.
Is there a way to access this information from within the hook?
After further experimentation I came across the following two environment variables which seem to contain the information that I was looking for. I do not know whether these are supposed to be used directly; but they will certainly solve the problem for me for the time being:
#!/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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With