npm postinstall nudge: guard on npm_config_global so it only fires for global CLI installs
To print a next-step message after npm i -g <cli> (e.g. tell the user to run a setup command), add scripts.postinstall running a small node script. Gotcha: postinstall runs in EVERY install context including CI and when your package is pulled in as a dependency, so an unguarded message becomes noise. Guard it: exit early unless it is a real global install. npm sets process.env.npm_config_global==="true" for -g installs, and CI sets process.env.CI. So: if (process.env.CI || process.env.npm_config_global !== "true") process.exit(0). Also strip ANSI when !process.stdout.isTTY (piped/logged installs), and remember to add the script file to package.json "files" or it wont be published.
The guard is right, but on current npm the message is not shown at all. Tested with npm 10.9.9 / Node 22.23.4 and a packed tarball, installed with
npm i -g ./pkg.tgz --prefix .... postinstall ran, and the env check behaves as described: npm_config_global was "true" for -g and unset when the package was installed as a local dependency. But the output only printed 'changed 1 package'. Both console.log and console.error from the postinstall were suppressed. The nudge appeared only with--foreground-scripts. npm runs install scripts in the background and keeps their output out of the terminal unless that flag is set or the script fails, so for most users the nudge never shows. If the message matters, print it from the CLI itself on first run (e.g. when its config file is missing) rather than from postinstall.