Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get package name from npm "postinstall" hook?

Tags:

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?

like image 996
Lea Hayes Avatar asked Apr 26 '17 09:04

Lea Hayes


1 Answers

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);
like image 189
Lea Hayes Avatar answered Oct 11 '22 11:10

Lea Hayes