Try Live
Add Docs
Rankings
Pricing
Docs
Install
Theme
Install
Docs
Pricing
More...
More...
Try Live
Rankings
Enterprise
Create API Key
Add Docs
Postinstall Postinstall
https://github.com/ds300/postinstall-postinstall
Admin
A utility that runs your app's postinstall npm script during this package's postinstall, ensuring
...
Tokens:
1,075
Snippets:
9
Trust Score:
9.8
Update:
2 weeks ago
Context
Skills
Chat
Benchmark
60.6
Suggestions
Latest
Show doc for...
Code
Info
Show Results
Context Summary (auto-generated)
Raw
Copy
Link
# Postinstall Postinstall Postinstall-postinstall is an npm utility package that ensures your project's `postinstall` script runs after `yarn remove <package>` operations. By default, Yarn executes the `postinstall` hook after `yarn`, `yarn install`, and `yarn add <package>`, but not after removing packages. This package fills that gap by triggering your postinstall script during its own postinstall phase. The package automatically detects whether you're using Yarn or npm and executes your project's postinstall script accordingly. It reads your project's `package.json` to find the postinstall script and runs it in your project's root directory. Note that your postinstall script must be idempotent since it will run twice during `yarn`, `yarn install`, and `yarn add <package>` operations. ## Installation Add the package to your project's dependencies to enable automatic postinstall execution after package removal. ```bash # Using yarn yarn add postinstall-postinstall # Using npm npm install postinstall-postinstall ``` ## How It Works The package runs automatically during its postinstall phase and checks for your project's postinstall script, executing it if found. ```javascript // Internal execution flow (run.js) // 1. Locates your project's package.json by finding the parent of node_modules const appPath = path.normalize(cwd.slice(0, cwd.lastIndexOf('node_modules'))) const packageJsonPath = path.join(appPath, 'package.json') // 2. Reads your package.json and checks for a postinstall script if (fs.existsSync(packageJsonPath)) { const packageJson = JSON.parse(fs.readFileSync(packageJsonPath)) if (packageJson.scripts && packageJson.scripts.postinstall) { // 3. Detects package manager (yarn vs npm) and runs your postinstall const pkgManager = shouldUseYarn() ? 'yarn' : 'npm'; exec(`${pkgManager} run postinstall`, {cwd: appPath}) } } ``` ## Usage Example Configure your project with an idempotent postinstall script that handles being run multiple times. ```json // package.json { "name": "my-project", "version": "1.0.0", "scripts": { "postinstall": "patch-package" }, "dependencies": { "postinstall-postinstall": "^2.0.0", "patch-package": "^6.0.0" } } ``` ```bash # After adding this package, your postinstall runs on: yarn # postinstall runs (normal behavior + this package) yarn install # postinstall runs (normal behavior + this package) yarn add some-package # postinstall runs (normal behavior + this package) yarn remove some-package # postinstall runs (via this package only) ``` ## Summary The primary use case for postinstall-postinstall is maintaining consistent project state when using Yarn, particularly for tools like `patch-package` that apply patches to dependencies. Without this package, removing a dependency with `yarn remove` would skip the postinstall hook, potentially leaving your project in an inconsistent state where patches aren't applied to remaining dependencies. Integration is straightforward: simply add the package as a dependency and ensure your existing postinstall script is idempotent (safe to run multiple times with the same result). The package handles all detection and execution automatically, supporting both Yarn and npm environments without any additional configuration required.